Javascript词频书签在输出中省略了一些字母

标签 javascript frequency bookmarklet cpu-word

我找不到 Chrome 的词频扩展程序,该扩展程序列出了页面上某个单词的使用次数(我需要一个按使用频率排列的至少 100 个结果的列表),因此我求助于复制 JavaScript小书签并对其进行一些调整以过滤掉常见单词。

但是,原始代码和修改后的代码都会输出一个列表,其中省略了某些单词的首字母,例如“roperty”而不是“property”,“ubversion”而不是“subversion”等。可能会是什么造成这个?

这里是原始代码的链接:https://gist.github.com/RonnyO/3004194

这是我稍微调整后的代码:

javascript: (function () {
            var settings = {
                           listLength: 100,
                           ignore: ['the', 'be', 'to', 'of', 'and', 'in', 'that', 'have', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at', 'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her', 'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there', 'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time', 'no', 'just', 'him', 'know', 'fake', 'people', 'into', 'year', 'your', 'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then', 'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also', 'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well', 'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give', 'day', 'most', 'us']
                    },
                    w, s;
            function getBodyText() {
                    var doc = document,
                            body = doc.body,
                            selection, range, bodyText;
            if (body.createTextRange) {
                            return body.createTextRange().text;
            } else if (getSelection) {
                            selection = getSelection();
                            range = doc.createRange();
                            range.selectNodeContents(body);
                            selection.addRange(range);
                            bodyText = selection.toString();
                            selection.removeAllRanges();
                            return bodyText;
            }
     }

     var punctuation = /[\/\.\*\+\+\?\|\(\)\[\]\{\}\^\\,:;-`~!@#$%&_]+/g;
     var words = getBodyText().trim().replace(punctuation, ' ').replace(/\s+/g, ' ').split(' '),
               count = {},
               sorted = [];

    for (w in words) {if (words.hasOwnProperty(w) && settings.ignore.indexOf(words[w]) == -1) {
        var word = words[w];
        count[word] = count[word] ? count[word] + 1 : 1;
    }
}

for (w in count) if (count.hasOwnProperty(w)) {
    sorted.push([w, count[w]]);
}

s = sorted.sort(function (a, b) {
    return b[1] - a[1];
});

var output = '<title>word frequency</title><ul style="direction: ltr; text-align: left; font-family: sans-serif; line-height: 130%;">';
for (s in sorted.slice(0, settings.listLength)) {
    var c = sorted[s];
    output += '<li>' + c[1] + ': ' + c[0] + '</li>';
}
output += '</ul>';

with(open().document){
    write(output);
    close();
}
})();

抱歉,缩进太糟糕了..

最佳答案

更改标点符号以避开连字符。

var punctuation = /[\/\.\*\+\+\?\|\(\)\[\]\{\}\^\\,:;\-`~!@#$%&_]+/g;

关于Javascript词频书签在输出中省略了一些字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33564442/

相关文章:

audio - 使用 RealFFT 改变频率幅度,闪烁的声音

javascript - 获取当前 url 但没有 http ://part bookmarklet!

javascript - 绕过 Chrome 的 CORB 功能

javascript - .js 和无尽的旋转器

javascript - selectize - Uncaught TypeError : $(. ..).selectize 不是函数

C 程序中的编译错误返回整数输入序列中出现次数最多的整数

python - 获取列表中给定项目的计数(不使用 numpy)

javascript - 以递减顺序显示的 angularjs 循环

javascript - 根据屏幕大小自动调整图像大小

javascript - 在 javascript 中创建图像的缩略图方 block (不丢失纵横比)