Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 编写一个计算词频的bookmarklet_Javascript_Bookmarklet_Word Frequency - Fatal编程技术网

Javascript 编写一个计算词频的bookmarklet

Javascript 编写一个计算词频的bookmarklet,javascript,bookmarklet,word-frequency,Javascript,Bookmarklet,Word Frequency,我想创建一个bookmarklet,对网页上的所有文本进行计数,然后在绝对位置的div中从大部分到最少显示结果 我做的每一次谷歌搜索都在谈论计算表单、文本区域或已知div id中的单词总数。这不是我想要的。我想要每个/w在整个网页上出现的次数 我知道足够多的javascript,知道我不知道如何做到这一点 像这样的方法应该会奏效: function countWordFrequency() { var freq={}; // Traverse the DOM looking for te

我想创建一个bookmarklet,对网页上的所有文本进行计数,然后在绝对位置的div中从大部分到最少显示结果

我做的每一次谷歌搜索都在谈论计算表单、文本区域或已知div id中的单词总数。这不是我想要的。我想要每个/w在整个网页上出现的次数


我知道足够多的javascript,知道我不知道如何做到这一点

像这样的方法应该会奏效:

function countWordFrequency() {
  var freq={};
  // Traverse the DOM looking for text nodes.
  recurseTextNodes(function(textNode) {
    // Split the text into words, removing punctuation.
    var words = textNode.data.replace(/[^\w\s]/g, '').split(/\s+/)
      , len = words.length;
    // Count the word frequency.
    for (var i=0; i<len; i++) {
      // if (freq[words[i]]) { bug if one of the words is "constructor"!
      if (typeof freq[words[i]] === 'number') {
        freq[words[i]] += 1;
      } else  {
        freq[words[i]] = 1;
      }
    }
  });
  return freq;
}
函数countWordFrequency(){
var freq={};
//遍历DOM以查找文本节点。
recurseTextNodes(函数(textNode){
//将文本拆分为单词,删除标点符号。
var words=textNode.data.replace(/[^\w\s]/g',).split(/\s+/)
,len=words.length;
//数一数单词的频率。

对于(var i=0;iYou可能还想看看——那里有一个词频bookmarklet,效果很好。你的google fu让你失望了。搜索
bookmarklet词频
,可能还有
seo bookmarklet词频
。我看到了很多不错的点击率。嗨,你的代码几乎完美,但如果文本包含s、 例如,“构造器”一词。请参见Douglass Crockford的解释: