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

Javascript word frequency bookmarklet omits some letters in the output

本文关键字:中省 词频 bookmarklet 输出 Javascript      更新时间:2023-09-26

我找不到Chrome的词频扩展,它列出了一个单词在页面上的使用次数(我需要一个按使用频率排列的至少100个结果的列表),所以我复制了一个javascript书签,并对其进行了一点调整,以过滤掉常见的单词。

然而,原始代码和修改后的代码都输出了一个列表,其中省略了一些单词的第一个字母,例如,用"property"代替"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;