Package: pyrsdameraulevenshtein
Rust implementation of the Damerau-Levenshtein distanceDamerau-Levenshteinimplementation in Rust as
Python package.
You should use this package if you need to calculate a distance metric for lists of integers or strings, and you need
high-performance.
If you only need to check the distance between two strings
checkouteditdistanceorjellyfish.InstallpipinstallpyrsdameraulevenshteinUseimportpyrsdameraulevenshteinasdldistance=dl.distance_int([1,2,3],[1,3])# distance = 1normalized_distance=dl.normalized_distance_int([1,2,3],[1,3])# normalized_distance = 0.33similarity=dl.similarity_int([1,2,3],[1,3])# similarity = 0.66distance=dl.distance_str(["A","B","C"],["A","C"])# distance = 1normalized_distance=dl.normalized_distance_str(["A","B","C"],["A","C"])# normalized_distance = 0.33similarity=dl.similarity_str(["A","B","C"],["A","C"])# similarity = 0.66distance=dl.distance_unicode("ABC","AC")# distance = 1normalized_distance=dl.normalized_distance_unicode("ABC","AC")# normalized_distance = 0.33similarity=dl.similarity_unicode("ABC","AC")# similarity = 0.66Get startedFirst, create a virtual python environment.Install packagespip install -r requirements.txtCreate the Rust binaryFull performance:maturin build --releaseandpip install target/wheels/*.whlDevelop version:maturin developRun the testspython tests/DamerauLevenshteinTest.pyPerformanceTests are executed on a Mac Mini with M1 chip with Python 3.10.
Redo these tests intests/DamerauLevenshteinTest.py.List comparisonsimportrandomimporttimeimportpyrsdameraulevenshteinfromfastDamerauLevenshteinimportdamerauLevenshteinfrompyxdameraulevenshteinimportdamerau_levenshtein_distancen=100000x=10print("Int lists:")a_lists=[random.sample(list(range(x)),k=x,counts=[xforiinrange(x)])foriinrange(n)]b_lists=[random.sample(list(range(x)),k=x,counts=[xforiinrange(x)])foriinrange(n)]tic=time.perf_counter()fora,binzip(a_lists,b_lists):result=pyrsdameraulevenshtein.distance_int(a,b)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, THIS implementation")# 0.0847 seconds, THIS implementation <<< BEST PERFORMANCEtic=time.perf_counter()fora,binzip(a_lists,b_lists):result=damerau_levenshtein_distance(a,b)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, pyxdameraulevenshtein")# 0.3073 seconds, pyxdameraulevenshteintic=time.perf_counter()fora,binzip(a_lists,b_lists):result=damerauLevenshtein(a,b,similarity=False)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, fastDamerauLevenshtein")# 0.1257 seconds, fastDamerauLevenshteinString comparisonsimportrandomimporttimeimportjellyfishimporttextdistanceimportpyrsdameraulevenshteinfromfastDamerauLevenshteinimportdamerauLevenshteinfrompyxdameraulevenshteinimportdamerau_levenshtein_distancen=100000x=10print("Strings:")a_strings=["".join(random.sample(list(chr(ord("A")+i)foriinrange(x)),k=x,counts=[xforiinrange(x)]))foryinrange(n)]b_strings=["".join(random.sample(list(chr(ord("A")+i)foriinrange(x)),k=x,counts=[xforiinrange(x)]))foryinrange(n)]tic=time.perf_counter()fora,binzip(a_strings,b_strings):result=pyrsdameraulevenshtein.distance_unicode(a,b)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, THIS implementation")# 0.0764 seconds, THIS implementationtic=time.perf_counter()fora,binzip(a_strings,b_strings):result=damerau_levenshtein_distance(a,b)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, pyxdameraulevenshtein")# 0.3925 seconds, pyxdameraulevenshteintic=time.perf_counter()fora,binzip(a_strings,b_strings):result=damerauLevenshtein(a,b,similarity=False)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, fastDamerauLevenshtein")# 0.1275 seconds, fastDamerauLevenshteintic=time.perf_counter()fora,binzip(a_strings,b_strings):result=jellyfish.damerau_levenshtein_distance(a,b)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, jellyfish.damerau_levenshtein_distance")# 0.0546 seconds, jellyfish.damerau_levenshtein_distancetic=time.perf_counter()fora,binzip(a_strings,b_strings):result=textdistance.DamerauLevenshtein(a,b)toc=time.perf_counter()print(f"{toc-tic:0.4f}seconds, textdistance.DamerauLevenshtein")# 0.0191 seconds, textdistance.DamerauLevenshtein <<< BEST PERFORMANCE