How to compare text?

Guenter Kruschina G.Kruschina at gmx.de
Mon Dec 3 15:27:02 EST 2001


From:           	"A" <printers at sendme.cz>
To:             	tutor at python.org, activepython at listserv.ActiveState.com,
  	python-list at python.org
Subject:        	How to compare text?
Send reply to:  	printers at sendme.cz
Priority:       	normal
Date sent:      	Mon, 3 Dec 2001 11:06:04 +0100

> 
> Hello,
> How can I compare of one  parragraph of text with another
> paragraph?Each paragraph can have about 100 words.
> For example I have the first paragraph
> 
> I want to be very good at Python programming. Better than in Perl.
> 
> THe second paragraph might look loke this:
> 
> She works all day long to master Perl.
> 
> All that I need is to find out if any of word from the second is in the 
> first paragraph. For the example above I should find out word
> 
> Perl
> 
> 
> What is the best and quickest way?
> Thank you for help.
> Ladislav
> 
> 
> _______________________________________________
> ActivePython mailing list
> ActivePython at listserv.ActiveState.com
> http://listserv.ActiveState.com/mailman/listinfo/activepython
> 


Hallo Ladislav, I have written a small progam, which will work as you expect. I 
hope so. I think this is a fast way to compare two paragraphs.

wbg
Günter


-------------- next part --------------


def CreateDict(par):
            
   
   #Remove some chars
   for char in ('.',';',','):
        par = par.replace(char,"")
                                
    
                                
   words = par.split(' ')                                   
   dPar = {}
    
   for word in words:
       dPar[word] = 1

   return dPar       


def Diff(par1,par2):
    
    dPar1 = CreateDict(par1) 
    dPar2 = CreateDict(par2)
     
    lCommon = [] 
    for word in dPar2.keys():
        if dPar1.has_key(word):
            lCommon.append(word)  
            
    return lCommon    
        
    


 
 
 
 
def main(): 
    lCommonWords = Diff("I want to be very good at Python programming. Better than in Perl.",
        "She works all day long to master Perl.")        
        
    print "Common Words: ", lCommonWords        
    
 
main()    


More information about the Python-list mailing list