使用JavaScript忽略“a”,“the”等的字符串搜索

时间:2013-07-31 04:00:05

标签: javascript node.js

我正在寻找类似的东西:

if(magicSearch("hot pizza","We sell pizza that is really hot") {//Found!}

如果两个单词出现(任何顺序),我只想要一个“真”,我希望它忽略要搜索的文本中的“a”,“和”,“the”等内容。

(我确信有更好的术语来解释我在寻找什么)

我见过各种数据库引擎支持这种类型的文本搜索(例如Mongodb),但我需要一种简单的JavaScript方式来测试字符串。我可以建立它,但感觉就像某些东西必须已经出现在那里。

格雷格

2 个答案:

答案 0 :(得分:2)

您可以在一个简单的循环中使用indexOf函数。

//Use this if you don't need to find exact matches of words
function magicSearch(needles, haystack) {
    var searchTerms = (needles instanceof Array) ? needles : [needles];
    for(var i = 0; i < searchTerms.length; i++) {
        if(haystack.indexOf(searchTerms[i]) === -1) {
            return false;
        }
    }
    return true;
}

//Use this if you want to find exact matches of words
function magicSearch2(needles, haystack) {
    var searchTerms = (needles instanceof Array) ? needles : [needles],
        haystackArray = haystack.split(' '),
        index, 
        found = 0;

    for(var i = 0; i < haystackArray.length; i++) {
        index = searchTerms.indexOf(haystackArray[i]);
        if(index !== -1) {
            delete searchTerms[i];
            found++;
            if(found = searchTerms.length) {
                return true;
            }
        }
    }
    return false;
}
if(magicSearch(["hot", "pizza"],"We sell pizza that is really hot") {
    console.log("FOUND");
}
if(magicSearch2(["hot", "pizza"],"We sell pizza that is really hot")) {     
    console.log("FOUND");
}

答案 1 :(得分:1)

如果你使用 underscore.js ,你可以有一些相对简单的实现

var stopwords_set = ["a", "and", "the" ];
var magicSearch = function (keywords, str) {
    var content_set = {};
    var keywords_set = {};

    _.each(keywords.split(' '), function(item){
        keywords_set[item.toLowerCase()] = 1;
    });

    _.each(str.split(' '), function(item){
        content_set[item.toLowerCase()] = 1;
    });

    //convert input to 2 sets excluding stop words
    content_set = _.without(_.keys(content_set), stopwords_set);
    keywords_set = _.without(_.keys(keywords_set), stopwords_set);

    //check the intersecion
    var value = _.intersection(content_set, keywords_set).length == keywords_set.length
    return value;
}

magicSearch("hot pizza","We sell pizza that is really hot");