Javascript 如何获取和显示对象中每个单词的计数?
如何获取并显示字符串中每个单词的计数 我可以从最常见的单词中进行排序,但无法显示计数。对象:频率最初显示计数(键) 我知道Object.keys和map()方法可能对我有所帮助,但我不确定如何将其包括在我的代码中的map方法中 非常感谢您的帮助Javascript 如何获取和显示对象中每个单词的计数?,javascript,html,Javascript,Html,如何获取并显示字符串中每个单词的计数 我可以从最常见的单词中进行排序,但无法显示计数。对象:频率最初显示计数(键) 我知道Object.keys和map()方法可能对我有所帮助,但我不确定如何将其包括在我的代码中的map方法中 非常感谢您的帮助 var stringtest = "Example: This is a string. I've excluded stopwords from becoming most frequent words. This is a string of wor
var stringtest = "Example: This is a string. I've excluded stopwords from becoming most frequent words. This is a string of words.";
function getFreq(string, cutOff){
var refineStr = string.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").toLowerCase().replace(/ to\b| is\b| for\b| you\b| the\b|your\b|an\b| on\b| by\b|this\b|will\b| of\b| a\b/g, ""),
words = refineStr.split(" "),
frequencies = {},
word, frequency, i;
for(i = 0; i < words.length; i++){
word = words[i];
frequencies[" " + words[i]] = (frequencies[" " + words[i]] || 0) + 1;
//frequencies[word]++;
}
words = Object.keys(frequencies);
return words.sort(function (a,b)
{
return frequencies[b] - frequencies[a];}).slice(0, cutOff).toString();
}
document.getElementById("wordlist").innerHTML = getFreq(stringtest, 20);
var stringtest=“示例:这是一个字符串。我已将stopwords排除在最常见的单词之外。这是一个字符串。”;
函数getFreq(字符串、截止){
var refineStr=string.replace(/[\,-\/\\!$%^&*:{}=\-\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,
words=refineStr.split(“”),
频率={},
字,频率,我;
对于(i=0;i
“严格使用”;
let test='示例:这是一个字符串。我已经排除了停止词成为最常见的词。这是一串字;
测试=测试。替换(/[,:]/g');
test=test.replace(/to\b | is\b | for\b | you\b | the\b | your\b | an\b | on\b | by\b | this\b | will\b | of\b | a\b/g“);
测试=测试分割(“”);
设obj={};
for(设i=0,max=test.length;i关键点长度){
maxAmount=keys.length;
}
for(设i=0;i
“严格使用”;
let test='示例:这是一个字符串。我已经排除了停止词成为最常见的词。这是一串字;
测试=测试。替换(/[,:]/g');
test=test.replace(/to\b | is\b | for\b | you\b | the\b | your\b | an\b | on\b | by\b | this\b | will\b | of\b | a\b/g“);
测试=测试分割(“”);
设obj={};
for(设i=0,max=test.length;i关键点长度){
maxAmount=keys.length;
}
for(设i=0;i
有几件事我个人会做得不同。如果将排除项保存在单独的数组中,则可能更容易读取和更新。此外,您不需要为它们使用正则表达式,只需使用一个。至于打印结果,一个简单的循环就可以了
const write=msg=>{
document.body.appendChild(document.createElement('div')).innerHTML=msg;
};
const input=“示例:这是一个字符串。我已将stopwords排除在最常见的单词之外。这是一个字符串。”;
const exclude=[“to”,“is”,“for”,“you”,“the”,“your”,“an”,“on”,“by”,“this”,“will”,“of”,“a”];
const lowerAlphaNum=input.replace(/[\,-\/\\\!$%\^&*;:{}=\-\-\`~(())/g,”)。toLowerCase();
const filtered=lowerAlphaNum.split(“”).filter(word=>exclude.indexOf(word)=-1);
常数频率={};
过滤的。forEach(word=>{
频率[字]=(频率[字]| 0)+1;
});
const sortedar=Object.keys(frequencies).map(word=>({
字:字,
频率:频率[字]
})).排序((a,b)=>b.频率-a.频率);
const trimmedArr=分拣机切片(0,5);
trimmedArr.forEach(项目=>{
写(item.word+':'+item.frequency);
});代码>我个人会做一些不同的事情。如果将排除项保存在单独的数组中,则可能更容易读取和更新。此外,您不需要为它们使用正则表达式,只需使用一个。
至于打印结果,一个简单的循环就可以了
const write=msg=>{
document.body.appendChild(document.createElement('div')).innerHTML=msg;
};
const input=“示例:这是一个字符串。我已将stopwords排除在最常见的单词之外。这是一个字符串。”;
const exclude=[“to”,“is”,“for”,“you”,“the”,“your”,“an”,“on”,“by”,“this”,“will”,“of”,“a”];
const lowerAlphaNum=input.replace(/[\,-\/\\\!$%\^&*;:{}=\-\-\`~(())/g,”)。toLowerCase();
const filtered=lowerAlphaNum.split(“”).filter(word=>exclude.indexOf(word)=-1);
常数频率={};
过滤的。forEach(word=>{
频率[字]=(频率[字]| 0)+1;
});
const sortedar=Object.keys(frequencies).map(word=>({
字:字,
频率:频率[字]
})).排序((a,b)=>b.频率-a.频率);
const trimmedArr=分拣机切片(0,5);
trimmedArr.forEach(项目=>{
写(item.word+':'+item.frequency);
});代码>使用以下代码:
var-stringtest=
“示例:这是一个字符串。我已将stopWord排除在最常见的单词之外。这是一个字符串。”;
函数stringCount(字符串){
变量模式=/(?=\S*['-])([a-zA-Z'-]+)|\w+/g,
匹配词=s