From: Vsevolod Stakhov Date: Sat, 15 Oct 2016 12:20:29 +0000 (+0100) Subject: [Feature] Try to improve normalization function for bayes X-Git-Tag: 1.4.0~259 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a903f7be21746c3a803d0a47dad4c0716584eaa7;p=thirdparty%2Frspamd.git [Feature] Try to improve normalization function for bayes --- diff --git a/src/libutil/util.c b/src/libutil/util.c index 5380d8c944..d1332d5047 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2549,17 +2549,16 @@ rspamd_shmem_xmap (const char *fname, guint mode, * C = -7 * D = 3 * y = 32(x - 0.5)^4 - 6(x - 0.5)^3 - 7(x - 0.5)^2 + 3(x - 0.5) + * + * New approach: + * y = ((x - bias)*2)^8 */ gdouble rspamd_normalize_probability (gdouble x, gdouble bias) { - const gdouble a = 32, b = -6, c = -7, d = 3; - gdouble xx, x2, x3, x4; + gdouble xx; - xx = x - bias; - x2 = xx * xx; - x3 = x2 * xx; - x4 = x3 * xx; + xx = (x - bias) * 2.0; - return a*x4 + b*x3 + c*x2 + d*xx; + return pow (xx, 8); } diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index cb24cb983b..8aec3a88d8 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -352,6 +352,16 @@ LUA_FUNCTION_DEF (util, zstd_compress); */ LUA_FUNCTION_DEF (util, zstd_decompress); +/*** + * @function util.normalize_prob(prob, [bias = 0.5]) + * Normalize probabilities using polynom + * + * @param {number} prob probability param + * @param {number} bias number to subtract for making the final sollution + * @return {number} normalized number + */ +LUA_FUNCTION_DEF (util, normalize_prob); + /*** * @function util.pack(fmt, ...) * @@ -463,6 +473,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, random_hex), LUA_INTERFACE_DEF (util, zstd_compress), LUA_INTERFACE_DEF (util, zstd_decompress), + LUA_INTERFACE_DEF (util, normalize_prob), LUA_INTERFACE_DEF (util, pack), LUA_INTERFACE_DEF (util, unpack), LUA_INTERFACE_DEF (util, packsize), @@ -1724,6 +1735,23 @@ lua_util_zstd_decompress (lua_State *L) return 2; } +static gint +lua_util_normalize_prob (lua_State *L) +{ + gdouble x, bias = 0.5; + + x = lua_tonumber (L, 1); + + if (lua_type (L, 2) == LUA_TNUMBER) { + bias = lua_tonumber (L, 2); + } + + lua_pushnumber (L, rspamd_normalize_probability (x, bias)); + + return 1; +} + + /* Backport from Lua 5.3 */ /******************************************************************************