我正在尝试使用单词频率,并且在打印单词频率方面有一些工作要做。我现在想要按升序排序输出。
var paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?";
un_puncutated_paragraph = paragraph.replace(/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,"");
let wordsHolder = un_puncutated_paragraph.split(' ');
let frequencyMap = {};
wordsHolder.forEach((word) => {
if (!frequencyMap[word]) {
frequencyMap[word] = 0;
}
frequencyMap[word] += 1;
});
此外,有人可以更好地解释这个逻辑,不完全确定发生了什么?
if (!frequencyMap[word]) {
frequencyMap[word] = 0;
}
frequencyMap[word] += 1;
谢谢!
答案 0 :(得分:0)
let result = `${integer}${fraction}`;
let numberResult = parseFloat(result); // now should be ok.
答案 1 :(得分:0)
if (!frequencyMap[word]) { // If word does not exists as key in array...
frequencyMap[word] = 0; // ...add key in array
}
frequencyMap[word] += 1; // if existing or just created increment word count
答案 2 :(得分:0)
按照您可以尝试的每个单词的频率对最终的frequencyMap
对象进行排序:
Object.keys(frequencyMap)
.sort()
.reduce((a, v) => {
a[v] = frequencyMap[v];
return a; }, {});