Browse Source

Remove synonyms, hint and append

CLDC-1049-autocomplete-improvements
Kat 2 years ago
parent
commit
1348fac952
  1. 40
      app/frontend/modules/search.js

40
app/frontend/modules/search.js

@ -12,13 +12,9 @@ const clean = (text) =>
.toLowerCase(); .toLowerCase();
const cleanseOption = (option) => { const cleanseOption = (option) => {
const synonyms = (option.synonyms || []).map(clean);
option.clean = { option.clean = {
name: clean(option.name), name: clean(option.name),
nameWithoutStopWords: removeStopWords(option.name), nameWithoutStopWords: removeStopWords(option.name),
synonyms: synonyms,
synonymsWithoutStopWords: synonyms.map(removeStopWords),
boost: option.boost || 1, boost: option.boost || 1,
}; };
@ -45,42 +41,19 @@ const startsWith = (word, query) => word.search(startsWithRegExp(query)) === 0;
const wordsStartsWithQuery = (word, regExps) => const wordsStartsWithQuery = (word, regExps) =>
regExps.every((regExp) => word.search(regExp) >= 0); regExps.every((regExp) => word.search(regExp) >= 0);
const anyMatch = (words, query, evaluatorFunc) => const calculateWeight = ({ name, nameWithoutStopWords }, query) => {
words.some((word) => evaluatorFunc(word, query));
const synonymsExactMatch = (synonyms, query) =>
anyMatch(synonyms, query, exactMatch);
const synonymsStartsWith = (synonyms, query) =>
anyMatch(synonyms, query, startsWith);
const wordInSynonymStartsWithQuery = (synonyms, startsWithQueryWordsRegexes) =>
anyMatch(synonyms, startsWithQueryWordsRegexes, wordsStartsWithQuery);
const calculateWeight = (
{ name, synonyms, nameWithoutStopWords, synonymsWithoutStopWords },
query
) => {
const queryWithoutStopWords = removeStopWords(query); const queryWithoutStopWords = removeStopWords(query);
if (exactMatch(name, query)) return 100; if (exactMatch(name, query)) return 100;
if (exactMatch(nameWithoutStopWords, queryWithoutStopWords)) return 95; if (exactMatch(nameWithoutStopWords, queryWithoutStopWords)) return 95;
if (synonymsExactMatch(synonyms, query)) return 75;
if (synonymsExactMatch(synonymsWithoutStopWords, queryWithoutStopWords))
return 70;
if (startsWith(name, query)) return 60; if (startsWith(name, query)) return 60;
if (startsWith(nameWithoutStopWords, queryWithoutStopWords)) return 55; if (startsWith(nameWithoutStopWords, queryWithoutStopWords)) return 55;
if (synonymsStartsWith(synonyms, query)) return 50;
if (synonymsStartsWith(synonyms, queryWithoutStopWords)) return 40;
const startsWithRegExps = queryWithoutStopWords const startsWithRegExps = queryWithoutStopWords
.split(/\s+/) .split(/\s+/)
.map(startsWithRegExp); .map(startsWithRegExp);
if (wordsStartsWithQuery(nameWithoutStopWords, startsWithRegExps)) return 25; if (wordsStartsWithQuery(nameWithoutStopWords, startsWithRegExps)) return 25;
if (wordInSynonymStartsWithQuery(synonymsWithoutStopWords, startsWithRegExps))
return 10;
return 0; return 0;
}; };
@ -118,10 +91,8 @@ export const sort = (query, options) => {
export const suggestion = (value, options) => { export const suggestion = (value, options) => {
const option = options.find((o) => o.name === value); const option = options.find((o) => o.name === value);
if (option) { if (option) {
const html = option.append const html = `<span>${value}</span>`;
? `<span>${value}</span> ${option.append}` return html;
: `<span>${value}</span>`;
return option.hint ? `${html}<br>${option.hint}` : html;
} else { } else {
return `<span>No results found</span>`; return `<span>No results found</span>`;
} }
@ -130,11 +101,6 @@ export const suggestion = (value, options) => {
export const enhanceOption = (option) => { export const enhanceOption = (option) => {
return { return {
name: option.label, name: option.label,
synonyms: option.getAttribute("data-synonyms")
? option.getAttribute("data-synonyms").split("|")
: [],
append: option.getAttribute("data-append"),
hint: option.getAttribute("data-hint"),
boost: parseFloat(option.getAttribute("data-boost")) || 1, boost: parseFloat(option.getAttribute("data-boost")) || 1,
}; };
}; };

Loading…
Cancel
Save