Split searching and listing
This commit is contained in:
@@ -24,78 +24,81 @@
|
|||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterList(text) {
|
function getMatchingExamples(text) {
|
||||||
let examples;
|
|
||||||
if (text.length < 2) {
|
if (text.length < 2) {
|
||||||
examples = info.examples;
|
return info.examples;
|
||||||
} else {
|
}
|
||||||
const words = text.split(/\W+/);
|
const words = text.split(/\W+/);
|
||||||
const scores = {};
|
const scores = {};
|
||||||
for (let i = 0; i < words.length; ++i) {
|
for (let i = 0; i < words.length; ++i) {
|
||||||
const word = words[i].toLowerCase();
|
const word = words[i].toLowerCase();
|
||||||
let dict = info.index[word];
|
let dict = info.index[word];
|
||||||
const updateScores = function () {
|
const updateScores = function () {
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
for (let exIndex in dict) {
|
for (let exIndex in dict) {
|
||||||
const count = dict[exIndex];
|
const count = dict[exIndex];
|
||||||
if (scores[exIndex]) {
|
if (scores[exIndex]) {
|
||||||
if (scores[exIndex][word]) {
|
if (scores[exIndex][word]) {
|
||||||
scores[exIndex][word] += count;
|
scores[exIndex][word] += count;
|
||||||
} else {
|
|
||||||
scores[exIndex][word] = count;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
scores[exIndex] = {};
|
|
||||||
scores[exIndex][word] = count;
|
scores[exIndex][word] = count;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
scores[exIndex] = {};
|
||||||
|
scores[exIndex][word] = count;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
if (dict) {
|
};
|
||||||
updateScores();
|
if (dict) {
|
||||||
} else {
|
updateScores();
|
||||||
const r = new RegExp(word);
|
} else {
|
||||||
// eslint-disable-next-line prefer-const
|
const r = new RegExp(word);
|
||||||
for (let idx in info.index) {
|
// eslint-disable-next-line prefer-const
|
||||||
if (r.test(idx)) {
|
for (let idx in info.index) {
|
||||||
dict = info.index[idx];
|
if (r.test(idx)) {
|
||||||
updateScores();
|
dict = info.index[idx];
|
||||||
}
|
updateScores();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
examples = [];
|
|
||||||
// eslint-disable-next-line prefer-const
|
|
||||||
for (let j in scores) {
|
|
||||||
const ex = info.examples[j];
|
|
||||||
ex.score = scores[j];
|
|
||||||
examples.push(ex);
|
|
||||||
}
|
|
||||||
// sort examples by first by number of words matched, then
|
|
||||||
// by word frequency
|
|
||||||
examples.sort(function (a, b) {
|
|
||||||
let cmp;
|
|
||||||
let aWords = 0,
|
|
||||||
bWords = 0;
|
|
||||||
let aScore = 0,
|
|
||||||
bScore = 0;
|
|
||||||
// eslint-disable-next-line prefer-const
|
|
||||||
for (let i in a.score) {
|
|
||||||
aScore += a.score[i];
|
|
||||||
aWords += 1;
|
|
||||||
}
|
|
||||||
// eslint-disable-next-line prefer-const
|
|
||||||
for (let j in b.score) {
|
|
||||||
bScore += b.score[j];
|
|
||||||
bWords += 1;
|
|
||||||
}
|
|
||||||
if (aWords == bWords) {
|
|
||||||
cmp = bScore - aScore;
|
|
||||||
} else {
|
|
||||||
cmp = bWords - aWords;
|
|
||||||
}
|
|
||||||
return cmp;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
const examples = [];
|
||||||
|
// eslint-disable-next-line prefer-const
|
||||||
|
for (let j in scores) {
|
||||||
|
const ex = info.examples[j];
|
||||||
|
ex.score = scores[j];
|
||||||
|
examples.push(ex);
|
||||||
|
}
|
||||||
|
// sort examples by first by number of words matched, then
|
||||||
|
// by word frequency
|
||||||
|
examples.sort(function (a, b) {
|
||||||
|
let cmp;
|
||||||
|
let aWords = 0,
|
||||||
|
bWords = 0;
|
||||||
|
let aScore = 0,
|
||||||
|
bScore = 0;
|
||||||
|
// eslint-disable-next-line prefer-const
|
||||||
|
for (let i in a.score) {
|
||||||
|
aScore += a.score[i];
|
||||||
|
aWords += 1;
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line prefer-const
|
||||||
|
for (let j in b.score) {
|
||||||
|
bScore += b.score[j];
|
||||||
|
bWords += 1;
|
||||||
|
}
|
||||||
|
if (aWords == bWords) {
|
||||||
|
cmp = bScore - aScore;
|
||||||
|
} else {
|
||||||
|
cmp = bWords - aWords;
|
||||||
|
}
|
||||||
|
return cmp;
|
||||||
|
});
|
||||||
|
return examples;
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterList(text) {
|
||||||
|
const examples = getMatchingExamples(text);
|
||||||
listExamples(examples);
|
listExamples(examples);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user