Javascript 如何筛选出具有相同关键字的各种标题
我有相似的字符串,因为它们包含相同的关键字 如果多个字符串具有三个或更多相同的关键字,那么我只希望返回一个字符串 例如: 我想为3个关键字过滤所有字符串,然后我只想保留一个字符串 不确定我是否可以单独使用正则表达式或需要JavaScriptJavascript 如何筛选出具有相同关键字的各种标题,javascript,regex,Javascript,Regex,我有相似的字符串,因为它们包含相同的关键字 如果多个字符串具有三个或更多相同的关键字,那么我只希望返回一个字符串 例如: 我想为3个关键字过滤所有字符串,然后我只想保留一个字符串 不确定我是否可以单独使用正则表达式或需要JavaScript 帮助?把所有的句子循环一遍,去掉标点符号,把单词放在地图上怎么样。聚合计数,然后在哈希上循环,以查找出现的单词超过3个 function filter() { var sentences = [ "Ex Machina is an e
帮助?把所有的句子循环一遍,去掉标点符号,把单词放在地图上怎么样。聚合计数,然后在哈希上循环,以查找出现的单词超过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, '');
}
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, '');
}