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

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