From 3894db0824bbb0023bce8402f1c4f578504fb8ce Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Mon, 15 Apr 2024 18:41:27 +0300 Subject: refactoring routes; added weighted exact and partial match routines --- utils/match-util.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'utils/match-util.js') diff --git a/utils/match-util.js b/utils/match-util.js index a13fddb..b6b9fc0 100644 --- a/utils/match-util.js +++ b/utils/match-util.js @@ -64,7 +64,6 @@ const resemblance = (a, b, n) => { /** is_exact_match - * * check if a searching string -> query (string/latin in kb-format) * matches exactly an item of the array of synonyms -> chkArr (array of utf-8/strings) * @@ -84,9 +83,46 @@ function partial( query, chkArr ) { return found; } +/** is exact match + weight rating + * @returns {float} weight rates depth of array when a match is found + */ +function weighted_exact( query, chkArr ) { + let weight = 0; // closer to left/begin rating + let len = chkArr.length; + for(let i = 0; i < len ; i++) { + if (chkArr[i] == query) { + // rating weights array depth + weight = (len - i + 1.0) / len; + // console.log(i, weight, query); + break; + } + } + return weight; +} + + +/** is partial match + weight rating + * @returns {float} weight rates both match position and depth of match + */ +function weighted_partial( query, chkArr ) { + let rate = 0; + let weight = 0; + let len = chkArr.length; + for(let i = 0; i < len ; i++) { + let chk = chkArr[i].indexOf(query) + if (chk != -1) { + rate = (len -i +1.0) / (len + 2.0 * chk); + weight = rate > weight ? rate : weight; + } + } + return weight; +} + module.exports = { exact, partial, + weighted_exact, + weighted_partial, similarity, resemblance } -- cgit v1.2.3