diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2024-04-10 03:41:05 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2024-04-10 03:41:05 +0300 |
| commit | 0c65dee04506567ee50537ff2fbb83998b8fef0f (patch) | |
| tree | ddeeaf79671b81254dc8960006adfb58016dfc25 /pieces/match-util.js | |
| parent | ed92bae9b662beba81889a62e29828302503fe8f (diff) | |
| download | oseine-0c65dee04506567ee50537ff2fbb83998b8fef0f.tar.gz oseine-0c65dee04506567ee50537ff2fbb83998b8fef0f.tar.bz2 oseine-0c65dee04506567ee50537ff2fbb83998b8fef0f.zip | |
add methods in kb-util module; intoduce match-util module
Diffstat (limited to 'pieces/match-util.js')
| -rw-r--r-- | pieces/match-util.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/pieces/match-util.js b/pieces/match-util.js new file mode 100644 index 0000000..1841d15 --- /dev/null +++ b/pieces/match-util.js @@ -0,0 +1,65 @@ +/** Ngram fuzzy match algorithm + * (simple and fast) + */ +const createNgram = (word, n) => { // Ngram creation + if (word.length <3) return word; + const vector = []; + for (let i = 0; i < word.length-n+1; ++i) { + vector.push(word.slice(i, i + n)); + } + return vector; +}; + +/** check similarity between 2 words + * based on Ngram matches of N = n letters + * @param {string} a : first word + * @param {string} b : second word + * @param {int} n : Ngram base + * @returns {float} : match percentage as a float in [0, 1] + */ +const similarity = (a, b, n) => { // Ngram match score + if (a.length > 0 && b.length > 0) { + const aNgram = createNgram(a, n); + const bNgram = createNgram(b, n); + let hits = 0; + for (let x = 0; x < aNgram.length; ++x) { + for (let y = 0; y < bNgram.length; ++y) { + if (aNgram[x] === bNgram[y]) { + hits += 1; + } + } + } + if (hits > 0) { + const union = aNgram.length + bNgram.length; + return (2.0 * hits) / union; + } + } + return 0; +}; + +/** 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) + * + * @param query (string): searching string; string/latin in kb-format + * @param chkArr (array): array of synonyms; (array of utf-8/strings) + * @return (boolean): true|false + */ +function exact( query, chkArr ) { + found = false; + chkArr.forEach( w => { if (w == query) found = true }); + return found; +} + +function partial( query, chkArr ) { + found = false; + chkArr.forEach( w => { if (w.includes(query)) found = true }); + return found; +} + +module.exports = { + exact, + partial, + similarity, +};
\ No newline at end of file |
