我有相似的字符串,因为它们包含相同的关键字。
如果多个字符串具有三个或更多相同的关键字,那么我只想返回一个字符串。
示例:
Ex Machina is an example of female artificial intelligence.
Ex Machina, why are most artificial intelligence represented as female?
Will Artificial Intelligence be represented females like in Ex Machina?
我想过滤 3 个关键字的所有字符串,然后只保留一个字符串。
不确定我是否可以单独使用正则表达式或需要 JavaScript。
帮忙?
最佳答案
简单地循环所有句子并删除标点符号,并将单词保留在 map 中怎么样?聚合计数,然后循环哈希以查找出现次数超过 3 的单词
function filter() {
var sentences = [
"Ex Machina is an example of female artificial intelligence.",
"Ex Machina, why are most artificial intelligence represented as female?",
"Will Artificial Intelligence be represented females like in Ex Machina?"
];
var word_map = {};
for (var i = 0; i < sentences.length; i++) {
var cur_sentence = sentences[i];
cur_sentence = remove_punctuations(cur_sentence).toLowerCase().split(' ');
cur_sentence.forEach(function(w) {
if (word_map.hasOwnProperty(w)) {
word_map[w] += 1;
} else {
word_map[w] = 1;
}
});
}
console.log(word_map);
}
function remove_punctuations(w) {
return w.replace(/[\.?,]/g, '');
}
关于javascript - 如何过滤掉具有相同关键词的不同标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29816199/