diff options
| author | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2024-04-11 19:16:31 +0300 |
|---|---|---|
| committer | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2024-04-11 19:16:31 +0300 |
| commit | 762d85c95690f510bec4dd8c37c05454ae1be4fc (patch) | |
| tree | bb2283602195f299530b862fbe761ddbf4e821ce /pieces/match-util.js | |
| parent | f6682b3a1d90ae4f0b86edebb28ebb17a811b57a (diff) | |
| download | oseine-762d85c95690f510bec4dd8c37c05454ae1be4fc.tar.gz oseine-762d85c95690f510bec4dd8c37c05454ae1be4fc.tar.bz2 oseine-762d85c95690f510bec4dd8c37c05454ae1be4fc.zip | |
introduce resemblance; compare match implementations; retro-search in progress
Diffstat (limited to 'pieces/match-util.js')
| -rw-r--r-- | pieces/match-util.js | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/pieces/match-util.js b/pieces/match-util.js index 1841d15..a13fddb 100644 --- a/pieces/match-util.js +++ b/pieces/match-util.js @@ -10,8 +10,11 @@ const createNgram = (word, n) => { // Ngram creation return vector; }; -/** check similarity between 2 words - * based on Ngram matches of N = n letters +/** similarity + * rates similarity between 2 words + * based on Ngram matches of N = n letters; + * implements a 2-dim check (all a-Ngrams vs all all b-Ngrams) + * * @param {string} a : first word * @param {string} b : second word * @param {int} n : Ngram base @@ -37,6 +40,29 @@ const similarity = (a, b, n) => { // Ngram match score return 0; }; +/** resemblance + * is an alternative similarity rating; + * implements an 1-dim Ngram similarity check + * and it's much faster than similarity() + */ +const resemblance = (a, b, n) => { + if (a.length > n && b.length >= a.length) { + const aNgram = createNgram(a, n); + let hits = 0; + for (let i = 0; i < aNgram.length; ++i) { + if (b.includes(aNgram[i])) { + hits++; + } + } + if (hits > 0) { + // rate resemblance based on hits and length-similarity + return (hits / aNgram.length) * (a.length / b.length); + } + } + return 0; +} + + /** is_exact_match * * check if a searching string -> query (string/latin in kb-format) @@ -62,4 +88,5 @@ module.exports = { exact, partial, similarity, -};
\ No newline at end of file + resemblance +} |
