Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
else {
// Updating word frequency
wordFrequencies[word] = wordFrequencies[word] || 0;
wordFrequencies[word]++;
// Adding the word to the current keyword
keyword.push(word);
}
}
}
// Now we need to score the keywords and retrieve the best one
const heap = new Heap(comparator),
T = (candidateKeywords.size / 3) | 0;
candidateKeywords.forEach(keyword => {
const words = keyword.split(HASH_DELIMITER);
let score = 0;
for (let i = 0, l = words.length; i < l; i++) {
const word = words[i];
score += wordDegrees[word] / wordFrequencies[word];
}
heap.push({score, keyword: words});
if (heap.size > T)
heap.pop();
});
nearestNeighbors(k, query) {
const neighbors = new Heap(COMPARATOR),
queue = [this.root];
let tau = Infinity;
while (queue.length > 0) {
const node = queue.pop();
const d = this.distance(query, node.vantage);
if (d < tau) {
neighbors.push({distance: d, item: node.vantage});
// Trimming
if (neighbors.size > k)
neighbors.pop();