aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-08-20 14:20:12 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-08-20 14:20:12 +0100
commit4d6343b8fcbf8fb99d5e384fe84119841a48d81c (patch)
treec91e4274fedbc4ebb06f31ef410a8b4c6559debd /src
parenta696a5e1c89118c44ff88ee15c3a662d07015119 (diff)
downloadrspamd-4d6343b8fcbf8fb99d5e384fe84119841a48d81c.tar.gz
rspamd-4d6343b8fcbf8fb99d5e384fe84119841a48d81c.zip
Add lua bindings for levenshtein distance
Diffstat (limited to 'src')
-rw-r--r--src/lua/lua_util.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index b17eef9ae..57b93d12f 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -86,6 +86,15 @@ LUA_FUNCTION_DEF (util, tanh);
*/
LUA_FUNCTION_DEF (util, parse_html);
+/***
+ * @function util.levenshtein_distance(s1, s2)
+ * Returns levenstein distance between two strings
+ * @param {string} s1 the first string
+ * @param {string} s2 the second string
+ * @return {number} number of differences in two strings
+ */
+LUA_FUNCTION_DEF (util, levenshtein_distance);
+
static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, create_event_base),
LUA_INTERFACE_DEF (util, load_rspamd_config),
@@ -96,6 +105,7 @@ static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, tokenize_text),
LUA_INTERFACE_DEF (util, tanh),
LUA_INTERFACE_DEF (util, parse_html),
+ LUA_INTERFACE_DEF (util, levenshtein_distance),
{NULL, NULL}
};
@@ -501,6 +511,25 @@ lua_util_parse_html (lua_State *L)
}
static gint
+lua_util_levenshtein_distance (lua_State *L)
+{
+ const gchar *s1, *s2;
+ gsize s1len, s2len;
+ gint dist = 0;
+
+ s1 = luaL_checklstring (L, 1, &s1len);
+ s2 = luaL_checklstring (L, 2, &s2len);
+
+ if (s1 && s2) {
+ dist = rspamd_strings_levenshtein_distance (s1, s1len, s2, s2len);
+ }
+
+ lua_pushnumber (L, dist);
+
+ return 1;
+}
+
+static gint
lua_load_util (lua_State * L)
{
lua_newtable (L);