5

我想實現一個內部搜索我的網站,可以指向用戶在正確的方向,以防萬一輸入一個單詞,類似的東西你的意思是:在谷歌搜索。全文搜索的最近匹配

有沒有人有一個想法如何可以做這樣的搜索?我們如何建立單詞的相關性或我們假設用戶想要搜索的短語?

  • 我使用asp.net和SQL Server 2005與FTS(fullTextSearch)

謝謝

回答

0

我能想到的最簡單的方法是編寫返回錯配程度的功能在兩個單詞之間,你循環所有單詞並找到最好的單詞。

我已經用分支定界方法做了這個。讓我挖起代碼:

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是一個偉大的文章,可能會幫助你。

+0

確實是一個非常不錯的文章爲例。 +1 – 2009-01-15 22:39:58

+0

+1。但我認爲這不是要求的。 =)這個功能更像是「你是指Jon Skeet?」當有人搜索「大師」時。 – PEZ 2009-01-15 22:40:20

0

用T-SQL可以使用SOUNDEX功能從語音比較的話。

如果您將用戶輸入,然後通過soundex代碼與數據庫中的其他單詞進行比較,您應該能夠想出一個'你是不是指'的列表?話。

E.g.

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

將產生相同的輸出(A536)。

這些日子有更好的算法,但soundex內置到sql server。

0

你爲什麼不使用谷歌動力?你可以消耗他們的建議服務

here是C#