1

我正在寻找类似的东西:

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

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

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

我已经看到各种数据库引擎支持这种类型的文本搜索(例如 Mongodb),但我需要一种简单的 JavaScript 方法来测试字符串。我可以建造它,但它感觉就像某处必须已经存在的东西。

格雷格

4

2 回答 2

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");
}
于 2013-07-31T04:10:35.737 回答
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");
于 2013-07-31T04:26:33.977 回答