compromise
Version:
natural language processing in the browser
37 lines (35 loc) • 655 B
JavaScript
;
//
const topk = function (n) {
//count occurance
let count = {};
this.list.forEach((ts) => {
let str = ts.normal()
count[str] = count[str] || 0;
count[str] += 1;
});
//turn into an array
let all = [];
Object.keys(count).forEach((k) => {
all.push({
normal: k,
count: count[k],
});
});
//add percentage
all.forEach((o) => {
o.percent = ((o.count / all.length) * 100).toFixed(2);
});
//sort by freq
all = all.sort((a, b) => {
if (a.count > b.count) {
return -1;
}
return 1;
});
if (n) {
all = all.splice(0, n);
}
return all;
};
module.exports = topk;