【问题标题】:how to put words into a variables and check if it matches the data in database如何将单词放入变量并检查它是否与数据库中的数据匹配
【发布时间】:2011-08-26 03:16:06
【问题描述】:

我正在使用 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”。

如何将句子放入变量中并单独搜索每个变量?还是有更快的方法?

希望有人能给我一些帮助。谢谢

【问题讨论】:

    标签: c# sql-server database search


    【解决方案1】:

    我在 c# 中有类似的功能。它将接受一个字符串或句子例如: 输入:文件访问布朗先生 输出:“'文件'和'访问'和'先生'和'布朗'” 翻译成 SQL:'"File" 和 "access" 和 "Mr" 和 "Brown"'

        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;
        }
    

    希望对某人有所帮助!

    【讨论】:

      【解决方案2】:

      在数据库中搜索之前,可以先拆分句子,然后使用循环(例如:while-loop)检查每个单词是否为停用词,

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        您需要类似string[] words = sentence.Split(' '); 的内容,然后检查单词数组中的每个条目。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多