2011-05-16 41 views
1

我正在使用MSSQL和C#編寫搜索引擎。我在數據庫中遇到了一堆停用詞。在我的default.aspx頁面中,我有一個文本框和一個搜索按鈕。用戶可以在文本框中輸入整個句子。如何將單詞放入變量並檢查它是否與數據庫中的數據匹配

我寫了搜索單個關鍵字但不是整個句子的代碼。這是default.aspx.cs

private void ImageSearch() 
{ 


    if (txtSearch.Text.Trim().Equals("")) 
    { 
     //when types nothing 
     txtSearch.Focus(); 
     Warning.Text = "Please enter the keyword to search"; 
     //display message 
     return; 
    } 
    else 
    { 
     int check = Process.CheckStopWord(txtSearch.Text.Trim()); 
     //check if the keyword is stop word 
     if (check == 0) 
     { 
      txtSearch.Focus(); 
      Warning.Text = "No stop word found"; 
     } 
     else 
     { 
      txtSearch.Focus(); 
      Warning.Text = "Found Stop word"; 
     } 

    } 
} 

,爲checkstopword功能

public static int CheckStopWord(string keyword) 
{ 
    string check = "0"; 
    string query = "SELECT COUNT (stopword) FROM stopwordtb WHERE [stopword] like '" + keyword + "'"; 
    //count how many stop word matches the keyword entered, return a string of a number 
    accessDB dbaccess = new accessDB(); 
    DataSet ds = dbaccess.queryData(query); 
    DataTable dt = ds.Tables[0]; 
    if (dt.Rows[0][0].ToString() == check) 
    { 
     return 0; 
     //keyword != stop word in stopwordtb 
     // begin search in image database - not implemented yet 

    } 
    else 
    { 
     return 1; 
     //keyword = stop word in stopwordtb 
    } 

} 

我的問題是功能,如果在整個句子的用戶類型?例如,如果他在文本框中鍵入「The tree」,但「The」是停用詞,那麼我將忽略「the」並只搜索圖像數據庫中的「tree」。

如何將句子放入變量並分別搜索每個變量?或者有沒有更快的方法?

希望有人能給我一些幫助。謝謝

回答

0

之前您在數據庫中搜索,你可以先拆分句子,然後檢查每個字是使用一個循環(例如:while循環)停止字,

希望這有助於。

3

你需要像string[] words = sentence.Split(' ');之類的東西,然後檢查單詞數組中的每個條目。

0

我在c#中有一個類似的功能。它將接受一個字符串或句子,例如: 輸入:文件訪問先生布朗 輸出:「'文件'和'訪問'和'先生'和'布朗'」 轉換爲SQL:'「文件」和「訪問」和「先生」和「布朗」'

private static string formatStringToSQLForFullTEXTSEARCH(string subject) 
    { 
       string[] subArray = subject.Trim().Split(' '); 
       string newSubject = "";    

       newSubject = ""; 
       if (subArray != null && subArray.Length > 0) 
       { 
        newSubject = "'" + subArray[0] + "'"; 
        for (int i = 1; i < subArray.Length; i++) 
        { 
         newSubject += " and '" + subArray[i]+"'"; 
        } 
       } 
    return newSubject; 
    } 

希望它有助於某人!

相關問題