# Compare two
strings for a partial match
import string
def wordCmp(word1, word2):
# pad word if unequal length
word2 = '%s%s' %
(word2, ' '*(len(word1)-len(word2)))
word1 = '%s%s' %
(word1, ' '*(len(word2)-len(word1)))
num = 0
for i, letter in
enumerate(word1):
if not cmp(letter,
word2[i]):
num += 1
return num, max(len(word1),
len(word2))
class StrCompare(object):
def __init__(self, base, amt=70, ignore_case=True, ignore_punct=True):
self.amt =
float(amt)
self.icas = ignore_case
self.ipun = ignore_punct
self.base =
base
self.baseList
= self.__strList__(base, ignore_case)
def __spun__(self, s):
# Strip punctuation from each word if self.ipun == True
if self.ipun:
return s.strip(string.punctuation)
return s
def __strList__(self,s,icas):
# Always strip the whitespace
from the ends of the string
s = s.strip()
if icas:
return [self.__spun__(word.lower()) for word in s.split()]
return s.split()
def partialmatch(self,
other, amt=False):
if not amt:
amt = self.amt
self.otherList
= self.__strList__(other, self.icas)
lenTup = (sum([len(word) for word
in self.baseList]), sum([len(word)
for word in self.otherList]))
self.den =
float(max(lenTup))
self.num = 0
for i, word
in enumerate(self.otherList):
# This avoids possible indexError if baseList is shorter
than otherList
try:
if not cmp(word,
self.baseList[i]):
# print 'Word match: %s' %
word
self.num
+= len(word)
except IndexError,
e:
self.error
= 'The number of words do not match'
if (self.num/self.den)*100
> amt:
return True
return False
a = StrCompare(' The
coyote ran across the road and into the field \n')
print a.partialmatch('The coyote ran across the road and beyond
the field, Timmy')
print a.partialmatch('The coyote ran across the road and beyond
the field, Timmy', 85)
'''
>>> True
False
>>>
'''