javascript - 如何过滤掉具有相同关键词的不同标题

标签 javascript regex

我有相似的字符串,因为它们包含相同的关键字。

如果多个字符串具有三个或更多相同的关键字,那么我只想返回一个字符串。

示例:

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/

相关文章:

javascript - 尝试使用 Angular 从 REST 调用填充下拉列表

regex - Grep 在 OpenBSD 和 Linux 中的行为不同。无法使命令工作

c# - 用换行符提取文本

java - Jersey 的路径匹配 "sandbox"或空字符串

javascript - 如何使用 Jquery/Ajax 将带有文件的 webform 发布到 webmethod?

JavaScript:使用 'this.' 调用函数不引用类中的方法

regex - perl:在全局范围内匹配,保存和替换正则表达式的最佳方法

java - 如何忽略引号中的 "dots"

javascript - 如何在 Bootstrap 中的图像顶部嵌入输入文本框?

javascript - 用实体名称在Vue中生成标题