我们怎样才能找到并计算以下句子中的事件?
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};
}
有没有更好的方法呢?
答案 0 :(得分: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]
答案 1 :(得分:2)
您可以使用Javascript的match method来优化您的代码。例如,要查找句子中出现的“绿色”数,可以使用表达式
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};
}