2

我们如何找到并计算以下句子中出现的次数?

sentences = [
    'black im yello'
    'green black and white'
    'green green green green green greenana'
    'i have no color'
]

find_char = [
    'green',
    'black',
    'white'
]

应该返回

output = {
   'sentence 1' : 1 // just black
   'sentence 2' : 3 // green black white
   'sentence 3' : 6 // 5 green and green in 'green'ana
   'sentence 4' : 0
}

这是我当前的代码

var output = {};

for (var i = sentences.length - 1; i >= 0; i--) {
    var output_tmp = {}; 
    var occurance = 0; 
    for (x = 0; x < find_char.length; x++) {        
        j = 0;
        output_tmp[find_char[x]] = 0;
        // search if sentences then -1, if > -1 then there is a match
        while ((j = sentences[i].indexOf(find_char[x], j)) > -1) { 
            output_tmp[find_char[x]]++; 
            j++ 
        }
        occurance += output_tmp[find_char[x]];
    }
    output[i] = { 'sentence_no': i,  'occurance' : occurance};
}

有没有更好的方法来做到这一点?

4

2 回答 2

3

将所有单词组合成一个正则表达式并将其应用于每个句子:

sentences = [
    'black im yello',
    'green black and white',
    'green green green green green greenana',
    'i have no color'
]

words = [
    'green',
    'black',
    'white'
]

re = new RegExp(words.join('|'), 'gi')

counts = sentences.map(function(sent) {
    return (sent.match(re) || []).length
})

这将在控制台中打印:

[1, 3, 6, 0]
于 2012-07-12T21:17:32.673 回答
2

您可以使用 Javascript 的match 方法来优化您的代码。例如,要查找句子中出现的“green”的数量,您可以使用表达式

sentences[i].match(/green/g).length)

所以你的代码看起来像

for( s in sentences ){
  var occurences = 0;
  for( f in find_char )
    occurences += (sentences[s].match(new RegExp(find_char[f], "g")) || []).length
  output[s] = { 'sentence_no': s,  'occurance' : occurences};
}
于 2012-07-12T21:06:57.313 回答