









"""Levenshtein function is to define number of inserts, deletions and
substitutions needed to morph one string into another.
"""
def levenshtein(a,b):
n, m = len(a), len(b)
if n > m:
"""Make sure n <= m, to use O(min(n,m)) space
"""
a, b = b, a
n, m = m, n
current = range(n + 1)
for i in range(1, m + 1):
previous, current = current, [i] + [0] * n
for j in range(1, n + 1):
add, delete = previous[j] + 1, current[j - 1] + 1
change = previous[j - 1]
if a[j - 1] != b[i - 1]:
change = change + 1
current[j] = min(add, delete, change)
return current[n]
def levenshtein_distance(first, second):
"""Find the Levenshtein distance between two strings."""
if len(first) > len(second):
first, second = second, first
if len(second) == 0:
return len(first)
first_length = len(first) + 1
second_length = len(second) + 1
distance_matrix = [[0] * second_length for x in range(first_length)]
for i in range(first_length):
distance_matrix[i][0] = i
for j in range(second_length):
distance_matrix[0][j]=j
for i in xrange(1, first_length):
for j in range(1, second_length):
deletion = distance_matrix[i-1][j] + 1
insertion = distance_matrix[i][j-1] + 1
substitution = distance_matrix[i-1][j-1]
if first[i-1] != second[j-1]:
substitution += 1
distance_matrix[i][j] = min(insertion, deletion, substitution)
return distance_matrix[first_length-1][second_length-1]
"""
Compute the Damerau-Levenshtein distance between two given
strings (s1 and s2)
"""
def DamerauLevenshteinDistance(s1, s2):
d = {}
lenstr1 = len(s1)
lenstr2 = len(s2)
for i in xrange(-1,lenstr1+1):
d[(i,-1)] = i+1
for j in xrange(-1,lenstr2+1):
d[(-1,j)] = j+1
for i in xrange(0,lenstr1):
for j in xrange(0,lenstr2):
if s1[i] == s2[j]:
cost = 0
else:
cost = 1
d[(i,j)] = min(
d[(i-1,j)] + 1, # deletion
d[(i,j-1)] + 1, # insertion
d[(i-1,j-1)] + cost, # substitution
)
if i>1 and j>1 and s1[i]==s2[j-1] and s1[i-1] == s2[j]:
d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + cost) # transposition
return d[lenstr1-1,lenstr2-1]
empty_tags = soup.findAll(lambda tag: tag.name == 'p' and not tag.contents and (tag.string is None or not tag.string.strip()))
[empty_tag.extract() for empty_tag in empty_tags]
>++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>
+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>
]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<
>+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>
![]() | You are viewing Log in Create a LiveJournal Account Learn more |