Update scoring / indexing code

Add dict / word as parameters to updateScore function so it
does not need to be created in each loop, and shorten it a bit.
This commit is contained in:
Maximilian Krög
2020-07-12 17:11:59 +02:00
parent b6404fb766
commit 557563f331
2 changed files with 20 additions and 34 deletions

View File

@@ -29,40 +29,33 @@
if (text.length === 0) { if (text.length === 0) {
return info.examples; return info.examples;
} }
const words = text.split(/\W+/); const words = text.toLowerCase().split(/\W+/);
const scores = {}; const scores = {};
for (let i = 0; i < words.length; ++i) { const updateScores = function (dict, word) {
const word = words[i].toLowerCase(); // eslint-disable-next-line prefer-const
let dict = info.index[word]; for (let exIndex in dict) {
const updateScores = function () { let exScore = scores[exIndex];
// eslint-disable-next-line prefer-const if (!exScore) {
for (let exIndex in dict) { exScore = {};
const count = dict[exIndex]; scores[exIndex] = exScore;
if (scores[exIndex]) {
if (scores[exIndex][word]) {
scores[exIndex][word] += count;
} else {
scores[exIndex][word] = count;
}
} else {
scores[exIndex] = {};
scores[exIndex][word] = count;
}
} }
}; exScore[word] = (exScore[word] || 0) + dict[exIndex];
}
};
words.forEach(function (word) {
const dict = info.index[word];
if (dict) { if (dict) {
updateScores(); updateScores(dict, word);
} else { } else {
const r = new RegExp(word); const r = new RegExp(word);
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
for (let idx in info.index) { for (let idx in info.index) {
if (r.test(idx)) { if (r.test(idx)) {
dict = info.index[idx]; updateScores(info.index[idx], word);
updateScores();
} }
} }
} }
} });
const examples = []; const examples = [];
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
for (let exIndex in scores) { for (let exIndex in scores) {

View File

@@ -42,26 +42,19 @@ function createWordIndex(exampleData) {
const keys = ['shortdesc', 'title', 'tags']; const keys = ['shortdesc', 'title', 'tags'];
exampleData.forEach((data, i) => { exampleData.forEach((data, i) => {
keys.forEach((key) => { keys.forEach((key) => {
let text = data[key]; let text = data[key] || '';
if (Array.isArray(text)) { if (Array.isArray(text)) {
text = text.join(' '); text = text.join(' ');
} }
const words = text ? text.split(/\W+/) : []; const words = text.toLowerCase().split(/\W+/);
words.forEach((word) => { words.forEach((word) => {
if (word) { if (word) {
word = word.toLowerCase();
let counts = index[word]; let counts = index[word];
if (counts) { if (!counts) {
if (index in counts) {
counts[i] += 1;
} else {
counts[i] = 1;
}
} else {
counts = {}; counts = {};
counts[i] = 1;
index[word] = counts; index[word] = counts;
} }
counts[i] = (counts[i] || 0) + 1;
} }
}); });
}); });