summaryrefslogtreecommitdiff
path: root/utils/match-util.js
diff options
context:
space:
mode:
authorGeo Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2024-04-15 18:41:27 +0300
committerGeo Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2024-04-15 18:41:27 +0300
commit3894db0824bbb0023bce8402f1c4f578504fb8ce (patch)
treed74e86ea41fb83ede379429e7b9bc3e4408f96ba /utils/match-util.js
parentb122279ece06a9381e805c6087f130b41616fe3d (diff)
downloadoseine-3894db0824bbb0023bce8402f1c4f578504fb8ce.tar.gz
oseine-3894db0824bbb0023bce8402f1c4f578504fb8ce.tar.bz2
oseine-3894db0824bbb0023bce8402f1c4f578504fb8ce.zip
refactoring routes; added weighted exact and partial match routines
Diffstat (limited to 'utils/match-util.js')
-rw-r--r--utils/match-util.js38
1 files changed, 37 insertions, 1 deletions
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
}