最接近的匹配用于全文搜索
-
19-08-2019 - |
题
我试图实施一个内部搜索我的网站,可以用户指在正确的方向的情况下输错一个词, 有点像你的意思是 :在谷歌搜索。
没有任何人有一个想法是如何这样的搜索可以做什么?我们如何能够建立相关性的单词或短语,我们假定户旨在搜索?
- 我用asp.net 和sql服务器2005年FTS(fullTextSearch)
谢谢你
解决方案
你可以使用一种算法,用于确定串的相似性以及然后建议其他串从你的搜索引起一定的差异。
这个算法的 Levenshtein distance.
但是,不要忘记,寻找现有的解决方案。我认为如 分类:设 有能力搜索类似的字符串。
顺便说一句,这是一个相关的后关于这一主题: 如何谷歌"你是什么意思?" 算法的工作?
其他提示
这样做是查询通过经常表达的最接近的关键词匹配的短语。
在这里, 是一个伟大的文章,可能会帮助你。
T-SQL可以使用 SOUNDEX
功能比较的话发音。
如果你把用户输入,然后将其与其他的话在你的数据库通过soundex码,你应该能想出了一个列表中的'做你的意思是'?话。
E.g。
select SOUNDEX('andrew')
select SOUNDEX('androo')
都将产生相同的输出(A536).
有更好的算法,这些天,但soundex是建立成sql服务器。
最简单的方法我可以想到的是写一个函数,返回的程度之间的不匹配两个词,并通过所有的话,找到最好的。
我已经做到了这一分支和束缚方法。让我挖了代码:
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;
}
你为什么不用电?, 你可以使用其服务的建议
在这里, 是一个例子在c#
不隶属于 StackOverflow