我想实现一个内部搜索我的网站,可以指向用户在正确的方向,以防万一输入一个单词,类似的东西你的意思是:在谷歌搜索。全文搜索的最近匹配
有没有人有一个想法如何可以做这样的搜索?我们如何建立单词的相关性或我们假设用户想要搜索的短语?
- 我使用asp.net和SQL Server 2005与FTS(fullTextSearch)
谢谢
我想实现一个内部搜索我的网站,可以指向用户在正确的方向,以防万一输入一个单词,类似的东西你的意思是:在谷歌搜索。全文搜索的最近匹配
有没有人有一个想法如何可以做这样的搜索?我们如何建立单词的相关性或我们假设用户想要搜索的短语?
谢谢
您可以使用一种算法来确定字符串相似度,然后从您的搜索索引建议其他字符串,直到某个区别。
其中一种算法是Levenshtein distance。
但是,不要忘记搜索现有解决方案。我想,例如Lucene有能力搜索相似的字符串。
顺便说一句,这里有关于这个主题相关的职位:How does the Google “Did you mean?” Algorithm work?
我能想到的最简单的方法是编写返回错配程度的功能在两个单词之间,你循环所有单词并找到最好的单词。
我已经用分支定界方法做了这个。让我挖起代码:
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;
}
这是通过正则表达式查询与该短语匹配的最接近的关键字。
Here是一个伟大的文章,可能会帮助你。
用T-SQL可以使用SOUNDEX
功能从语音比较的话。
如果您将用户输入,然后通过soundex代码与数据库中的其他单词进行比较,您应该能够想出一个'你是不是指'的列表?话。
E.g.
select SOUNDEX('andrew')
select SOUNDEX('androo')
将产生相同的输出(A536)。
这些日子有更好的算法,但soundex内置到sql server。
你为什么不使用谷歌动力?你可以消耗他们的建议服务
here是C#
确实是一个非常不错的文章为例。 +1 – 2009-01-15 22:39:58
+1。但我认为这不是要求的。 =)这个功能更像是“你是指Jon Skeet?”当有人搜索“大师”时。 – PEZ 2009-01-15 22:40:20