diff --git a/picard/util/astrcmp.c b/picard/util/_astrcmp.c similarity index 99% rename from picard/util/astrcmp.c rename to picard/util/_astrcmp.c index 95641b4dc..7b8dd1803 100644 --- a/picard/util/astrcmp.c +++ b/picard/util/_astrcmp.c @@ -180,7 +180,7 @@ static struct PyModuleDef AstrcmpModule = }; PyMODINIT_FUNC -PyInit_astrcmp(void) +PyInit__astrcmp(void) { return PyModule_Create(&AstrcmpModule); -} \ No newline at end of file +} diff --git a/picard/util/astrcmp.py b/picard/util/astrcmp.py new file mode 100644 index 000000000..ff07cae6a --- /dev/null +++ b/picard/util/astrcmp.py @@ -0,0 +1,36 @@ +# http://hetland.org/coding/python/levenshtein.py + +# This is a straightforward implementation of a well-known algorithm, and thus +# probably shouldn't be covered by copyright to begin with. But in case it is, +# the author (Magnus Lie Hetland) has, to the extent possible under law, +# dedicated all copyright and related and neighboring rights to this software +# to the public domain worldwide, by distributing it under the CC0 license, +# version 1.0. This software is distributed without any warranty. For more +# information, see + + +def astrcmp_py(a,b): + "Calculates the Levenshtein distance between a and 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 1.0 - float(current[n]) / max(m, n) + + +try: + from picard.util._astrcmp import astrcmp +except ImportError: + astrcmp = astrcmp_py diff --git a/setup.py b/setup.py index 2687c8818..a537e1975 100755 --- a/setup.py +++ b/setup.py @@ -35,7 +35,7 @@ from distutils.dist import Distribution from distutils.spawn import find_executable ext_modules = [ - Extension('picard.util.astrcmp', sources=['picard/util/astrcmp.c']), + Extension('picard.util._astrcmp', sources=['picard/util/_astrcmp.c']), ] py2app_exclude_modules = [