【问题标题】:I would like to make an advanced search with javascript. How?我想用 javascript 进行高级搜索。如何?
【发布时间】:2012-12-13 15:45:54
【问题描述】:

我使用下面的代码进行了基本的、区分大小写的、特定于术语的搜索。它现在可以工作,但我想要一些东西(按重要性顺序):

1:忽略大小写(即“hi”和“Hi”都是一样的。toLowerCase不是一个选项,也不是一回事)

2:例如,如果搜索查询是“搜索词”并且搜索的字符串是“搜索词”,则会产生命中。

3:即使在找到更多匹配项后也搜索整个字符串。

目的是在带有特定id<p> 标记中搜索术语。如果有,则显示它。最终,我将在一个循环中使用它,该循环将搜索许多 <p> 标签并显示有命中的标签并隐藏没有命中的标签。

代码:

<!DOCTYPE html>
<html>
    <body>
        <p id="demo">Click the button to locate where in the string a specifed value occurs.</p>
        <p id="demo1" style="display:none;">Hello world, welcome to the universe.</p>
        <button onclick="myFunction()">Try it</button>

        <script>
            function myFunction() {
                var x = document.getElementById("demo1")
                var str = x.innerHTML.toString();
                var n = str.indexOf("welcome");
                if (n != -1) {
                    x.style.display = 'inline';
                } else {
                    x.innerHTML = 'Negative';
                    x.style.display = 'inline';
                }
            }
        </script>

    </body>
</html>

【问题讨论】:

  • 这不是 JavaScript 的任务。为了做你想做的事,你需要使用自然语言处理。我将从标记您的输入字符串并删除后缀开始。从那里,您可以尝试搜索您的数据库。
  • 我懂一些java。小程序能处理这个吗?
  • 这类东西通常不会在客户端完成。 Java 有很多不错的自然语言处理库可供您使用,但它们的学习曲线确实很陡峭。
  • “toLowerCase 不是一个选项,也不是一回事”为什么不呢?
  • toLowerCase 更改了搜索词,这是无用的,因为搜索字符串的大小写是未知的。 toLowerCase 是对文本进行规范化。

标签: javascript search


【解决方案1】:

我将首先标记您的输入字符串:

function tokenize(input) {
    return input.toLowerCase().replace(/[^a-z0-9_\s]/g, '').split(/\s+/g)
}

这对您的搜索字词有什么影响:

> tokenize("I'm your search string.")
["im", "your", "search", "string"]

接下来,去掉后缀(我什至不会尝试处理这不起作用的情况。这就是 NLP 的用途):

function remove_suffix(token) {
    return token.replace(/(ing|s)$/, '');
}

它将对每个令牌执行此操作:

> remove_suffix('searching')
"search"
> remove_suffix('terms')
"term"

所以对于每个查询字符串,可以构造一个关键字列表:

function get_keywords(query) {
    var tokens = tokenize(query);
    var keywords = tokens.map(remove_suffix);
    keywords.sort();

    return keywords;
}

它会将您的查询转换为关键字:

> get_keywords('searching terms')
["search", "term"]
> get_keywords('term search')
["search", "term"]

现在,您只需检查查询字符串的关键字是否包含在搜索字符串的关键字中。

这是一个非常简单的示例,不会处理大量极端情况,但至少您了解了如何使用关键字进行搜索。

【讨论】:

  • 只是举一个可能出错的例子:remove_suffix('string') --> "str"。不过,+1,因为它是其余的一个很好的答案。
  • @Cerbrus:还有'running' -&gt; 'runn'。例外的列表还在继续。
【解决方案2】:

我相信,经过一些调整,这应该可以满足您的要求。 不过,在后端执行此操作可能会更好 =)。

// returns the indices of the found searchStr within str, case sensitive if needed
function getIndicesOf(searchStr, str, caseSensitive) {
    var startIndex = 0, searchStrLen = searchStr.length;
    var index, indices = [];
    if (!caseSensitive) {
        str = str.toLowerCase();
        searchStr = searchStr.toLowerCase();
    }
    while ((index = str.indexOf(searchStr, startIndex)) > -1) {
        indices.push(index);
        startIndex = index + searchStrLen;
    }
    return indices;
}

// this splits the search string in an array of search strings
var myStringArray = mySearchString.split("\\s+");
var result = true;
// loop over all the split search strings, and search each seperately
for (var i = 0; i < myStringArray.length; i++) {
    var indices = getIndicesOf(myStringArray[i], "I learned to play the Ukulele in Lebanon.", false);
    if(indices && indices.length>0){
        // do something with the indices of the found string
    } else {
        result = false;
    }
}
// result will be false here if one of the search terms was not found.

借自here

【讨论】:

  • 我不仅需要这台服务器,还需要在磁盘上。我会仔细看看这个。谢谢
【解决方案3】:

看看正则表达式引擎。这需要一些时间来学习,但是一旦你知道了,你可能会在这里实现你的目标。

这是一个:link

希望对你有帮助

【讨论】:

  • 到目前为止,我可以看到如何不区分大小写。我一定会使用它。看看我如何解决原始帖子中的第 2 项。不太明白如何解决原始帖子中的第 3 项。
  • 简而言之-您可以使用组和全局搜索(标签/g)。如需更多答案,您可以阅读以下内容:stackoverflow.com/questions/520611/…
猜你喜欢
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 2021-10-05
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多