Last active
November 23, 2018 15:46
-
-
Save david-batranu/56acf8760d466f4bf581d426cad0e508 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python2 setup.py build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Python.h> | |
int min(int a, int b, int c) { | |
int m = a; | |
if (b < a) { | |
m = b; | |
}; | |
if (c < m) { | |
m = c; | |
}; | |
return m; | |
} | |
static PyObject * | |
lev_levenshtein(PyObject *self, PyObject *args) | |
{ | |
const char *encoding = "utf-8"; | |
char *s1, *s2; | |
if(!PyArg_ParseTuple(args, "eses", encoding, &s1, encoding, &s2)) { | |
return NULL; | |
}; | |
int len_s1 = strlen(s1), len_s2 = strlen(s2); | |
int v0[len_s2], v1[len_s2]; | |
char c1, c2; | |
int i, j, x; | |
for (x = 0; x <= len_s2; x++) { | |
v1[x] = x; | |
}; | |
for (i = 0; i < len_s1; i++) { | |
c1 = s1[i]; | |
for (x = 0; x <= len_s2; x++) { | |
v0[x] = v1[x]; | |
}; | |
v1[0]++; | |
for (j = 1; j <= len_s2; j++) { | |
c2 = s2[j - 1]; | |
if (c1 == c2) { | |
v1[j] = v0[j - 1]; | |
} | |
else { | |
v1[j] = 1 + min(v0[j - 1], v0[j], v1[j - 1]); | |
} | |
} | |
} | |
return Py_BuildValue("i", v1[len_s2]); | |
}; | |
static PyMethodDef LevMethods[] = { | |
{ | |
"levenshtein", lev_levenshtein, METH_VARARGS, | |
"Calculate Levenshtein distance between given strings" | |
}, | |
{ NULL, NULL, 0, NULL } | |
}; | |
PyMODINIT_FUNC | |
initlev(void) { | |
(void) Py_InitModule("lev", LevMethods); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup, Extension | |
module1 = Extension('lev', sources = ['lev.c']) | |
setup (name = 'PackageName', | |
version = '1.0', | |
description = 'This is a demo package', | |
ext_modules = [module1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment