A
This issue is a continuation of the question. https://ru.stackoverflow.com/questions/1187249/%D0%9A%D0%B0%D0%BA-%D0%BD%D0%B0%D0%B9%D1%82%D0%B8-%D1%81%D0%B0%D0%BC%D1%8B%D0%B9-%D0%BF%D0%BE%D1%85%D0%BE%D0%B6%D0%B8%D0%B9-string-%D0%B2-array This and the answer to it is a continuation of the reply.In the past, I showed how to use it. https://ru.wikipedia.org/wiki/%D0%A0%D0%B0%D1%81%D1%81%D1%82%D0%BE%D1%8F%D0%BD%D0%B8%D0%B5_%D0%9B%D0%B5%D0%B2%D0%B5%D0%BD%D1%88%D1%82%D0%B5%D0%B9%D0%BD%D0%B0 (not the author) and https://ru.wikipedia.org/wiki/%D0%A1%D1%85%D0%BE%D0%B4%D1%81%D1%82%D0%B2%D0%BE_%D0%94%D0%B6%D0%B0%D1%80%D0%BE_%E2%80%94_%D0%92%D0%B8%D0%BD%D0%BA%D0%BB%D0%B5%D1%80%D0%B0 ♪I'll show you how to use it. https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D0%B5%D0%BC%D0%BC%D0%B8%D0%BD%D0%B3 to make it easier to find coincidences (Stemmer is a thing that turns "Bota," "bottom" into "bottom." Well, if that word is in his dictionary, or if he's guessing the rules on which to find the "found" word) https://ru.wikipedia.org/wiki/%D0%9A%D0%BE%D1%8D%D1%84%D1%84%D0%B8%D1%86%D0%B8%D0%B5%D0%BD%D1%82_%D0%A1%D1%91%D1%80%D0%B5%D0%BD%D1%81%D0%B5%D0%BD%D0%B0 Plus, as compared to the previous reply, the search for a maximum overlap is now one way, no sorting.const natural = require('natural');
natural.PorterStemmerRu.attach();
const userMessage = 'пешу в ппаддержку11! бот-заебот! надоели баги вашего ббота!';
const arr = userMessage.tokenizeAndStem(); // ["пеш","паддержку11","бот","заебот","надоел","баг","ваш","ббот"]
['поддержка', 'бот', 'ытот'].forEach((str) => {
console.log('[d]', str, '->', compareDiceCoefficient(str, arr, 0.5));
console.log('[j]', str, '->', compareJaroWinkler(str, arr, 0.7));
});
function compareDiceCoefficient(str, arr, lowerLimit = 0.05) {
// arr.forEach((x) => console.log('[d]', x, natural.DiceCoefficient(str, x)));
const reduced = arr.reduce((acc, x) => {
const dt = natural.DiceCoefficient(str, x);
if (acc.dt < dt && dt > lowerLimit) {
acc.dt = dt;
acc.w = x;
}
return acc;
}, {dt: -Infinity, w: null});
return reduced.w;
}
function compareJaroWinkler(str, arr, lowerLimit = 0.05) {
// arr.forEach((x) => console.log('[j]', x, natural.JaroWinklerDistance(str, x, undefined, true)));
const reduced = arr.reduce((acc, x) => {
const dt = natural.JaroWinklerDistance(str, x, undefined, true);
if (acc.dt < dt && dt > lowerLimit) {
acc.dt = dt;
acc.w = x;
}
return acc;
}, {dt: -Infinity, w: null});
return reduced.w;
}
[d] поддержка -> ппаддержку11
[j] поддержка -> ппаддержку11
[d] бот -> бот
[j] бот -> бот
[d] ытот -> null
[j] ытот -> null