【问题标题】:Closest match for Full Text Search全文搜索的最接近匹配
【发布时间】:2010-10-01 17:06:38
【问题描述】:

我正在尝试对我的网站进行内部搜索,以防万一输入错误的单词,类似于你的意思:在谷歌搜索中。

有人知道如何进行这样的搜索吗?我们如何确定我们假设用户打算搜索的单词或短语的相关性?

  • 我使用带有 FTS (fullTextSearch) 的 asp.net 和 sql server 2005

谢谢

【问题讨论】:

    标签: asp.net sql-server-2005 full-text-search design-patterns string-matching


    【解决方案1】:

    我能想到的最简单的方法是编写一个函数,返回两个单词之间的不匹配程度,然后循环遍历所有单词并找到最佳单词。

    我使用分支定界方法完成了这项工作。让我挖掘代码:

    bool matchWithinBound(char* a, char* b, int bound){
      // skip over matching characters
      while(*a && *b && *a == *b){a++; b++;}
      if (*a==0 && *b==0) return true;
      // if bound too low, quit
      if (bound <= 0) return false;
      // try assuming a has an extra character
      if (*a && matchWithinBound(a+1, b, bound-1)) return true;
      // try assuming a had a letter deleted
      if (*b && matchWithinBound(a, b+1, bound-1)) return true;
      // try assuming a had a letter replaced
      if (*a && *b && matchWithinBound(a+1, b+1, bound-1)) return true;
      // try assuming a had two adjacent letters swapped
      if (a[0] && a[1]){
        char temp;
        int success;
        temp = a[0]; a[0] = a[1]; a[1] = temp;
        success = matchWithinBounds(a, b, bound-1);
        temp = a[0]; a[0] = a[1]; a[1] = temp;
        if (success) return true;
      }
      // can try other modifications
      return false;
    }
    
    int DistanceBetweenWords(char* a, char* b){
      int bound = 0;
      for (bound = 0; bound < 10; bound++){
        if (matchWithinBounds(a, b, bound)) return bound;
      }
      return 1000;
    }
    

    【讨论】:

      【解决方案2】:

      这是通过正则表达式查询与短语匹配的最接近的关键字。

      Here 是一篇可能对您有所帮助的好文章。

      【讨论】:

      • 文章+1。但我认为这不是我们所要求的。 =) 有问题的功能更像是“你是说 Jon Skeet 吗?”当有人搜索“大师”时。
      • 我从stackoverflow.com/questions/305223#306973 那里听到了这个笑话。我的意思是,在键入时完成和纠正拼写是不同的。
      • 确实是一篇好文章——很遗憾,如果用户输入了错误的字符,你怎么知道?假设用户输入“Skatch”而不是“Sketch”,一些搜索引擎是否会运行 Levenstein 距离等算法来计算最接近的匹配?
      【解决方案3】:

      您可以使用一种算法来确定字符串相似性,然后从您的搜索索引中建议其他字符串,直至达到一定的差异。

      其中一种算法是Levenshtein distance

      但是,不要忘记搜索现有的解决方案。我认为例如Lucene 具有搜索相似字符串的能力。

      顺便说一句,这里有一篇关于这个主题的相关帖子:How does the Google “Did you mean?” Algorithm work?

      【讨论】:

        【解决方案4】:

        使用 T-SQL 可以使用SOUNDEX 函数对单词进行拼音比较。

        如果您获取用户输入,然后通过 soundex 代码将其与数据库中的其他单词进行比较,您应该能够得出一个“你的意思是”的列表吗?单词。

        例如

        select SOUNDEX('andrew')
        select SOUNDEX('androo')
        

        都将产生相同的输出 (A536)。

        现在有更好的算法,但是 soundex 是内置在 sql server 中的。

        【讨论】:

          【解决方案5】:

          你为什么不用google power?,你可以使用他们的推荐服务

          here是c#上的一个例子

          【讨论】:

            猜你喜欢
            • 2014-03-24
            • 2011-04-24
            • 1970-01-01
            • 2019-09-22
            • 1970-01-01
            • 2012-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多