Permalink
Browse files

Merge pull request #87 from transifex/levenshtein

Drop custom implementation of Levenshtein distance.
  • Loading branch information...
diegobz committed May 24, 2012
2 parents c87dec1 + 58974ba commit 648a8c99a746f39778c510cfd1a0f924dc2618b9
Showing with 3 additions and 24 deletions.
  1. +1 −0 requirements.txt
  2. +2 −24 transifex/resources/formats/utils/string_utils.py
View
@@ -37,6 +37,7 @@ django-staticfiles==1.2.1
-e git+git://github.com/jezdez/django_compressor.git@ee16af610d677a0a9b63602da6558f601f66287c#egg=django_compressor
-e git+git://github.com/toastdriven/django-haystack.git@3289ab8bb410321a94608a13e2e527ddbd8f1a7e#egg=django-haystack
-e git+git://github.com/mpessas/django-bulk.git#egg=django-bulk
+-e git+git://github.com/mpessas/python-Levenshtein.git#egg=Levenshtein
-e git+git://github.com/mpessas/django-tagging.git#egg=django-tagging
-e git+git://github.com/jkal/django-notification.git#egg=django-notification
-e git+git://github.com/jkal/django-pagination.git#egg=django-pagination
@@ -1,32 +1,10 @@
# -*- coding: utf-8 -*-
-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]
-
+from Levenshtein import distance
def percent_diff(a, b):
try:
- return 100*levenshtein_distance(a, b) / float(max(len(a), len(b)))
+ return 100 * distance(a, b) / float(max(len(a), len(b)))
except ZeroDivisionError:
if len(a)==len(b): return 0
else: return 100

0 comments on commit 648a8c9

Please sign in to comment.