计算字符串中单词出现次数的算法

时间:2016-06-19 12:57:48

标签: javascript algorithm

我正在尝试构建一个算法来查看哪些词在评论中最常出现。

因此我想出了这个(用Javascript):

var analyze = function(comments){
    var detectedWords = [];
    var result = {};
    comments.forEach(function(comment){
        var words = comment.message.split(" ");
        words.forEach(function(word){
            word = word.toLowerCase();
            if(word !== ""){
                if(detectedWords.indexOf(word) === -1){
                    detectedWords.push(word);
                    result[detectedWords.indexOf(word)] = {"name":word,"count":1};
                }else{
                    result[detectedWords.indexOf(word)].count++;
                }
            }
        });
    });

    return _.orderBy(result, ['count'], ['desc']);
}

算法可以进一步优化吗? (内循环外的toLowerCase()?

在下一步中,我将定义一个"黑名单"或者像#34这样没有意思的词;是,我,我,是......,"

1 个答案:

答案 0 :(得分:1)

您可以使用哈希表来引用数组项,以便更快地访问count对象。 result现在是一个数组,现在可以排序。

var analyze = function (comments) {
    var result = [],
        hash = {};
    comments.forEach(function (comment) {
        var words = comment.message.split(" ");
        words.forEach(function (word) {
            word = word.toLowerCase();
            if (word !== "") {
                if (!hash[word]) {
                    hash[word] = { name: word, count: 0 };
                    result.push(hash[word]);
                }
                hash[word].count++;
            }
        });
    });

    return result.sort(function (a, b) { return b.count - a.count;});
    //return _.orderBy(result, ['count'], ['desc']);
}

console.log(analyze([{ message: 'a b c d a v d e f g q' }]));