如何将单词放入变量并检查它是否与数据库中的数据匹配

发布于 2024-11-07 13:48:31 字数 1668 浏览 0 评论 0原文

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

如何将句子放入变量中并单独搜索每个变量?或者有什么更快的方法吗?

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

I am using MSSQL and C# to write a search engine. I got a bunch of stop words in the database. In my default.aspx page, I got a textbox and a search button. Users can type in a whole sentence into the textbox.

I wrote the code for searching a single keyword but not a whole sentence. This is the function in 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";
        }

    }
}

and for the checkstopword function

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
    }

}

my problem is, what if the user type in a whole sentence?

For example, if he types in " The tree" in the textbox, but "The" is a stop word, so I will just ignore the "the" and just search the image database for "tree".

How can I put the sentence into variables and search each of them individually? or are there any faster way?

Hope somebody can give me some help. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月染过的梦 2024-11-14 13:48:31

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

You need something like string[] words = sentence.Split(' '); then check each entry in the words array.

苏辞 2024-11-14 13:48:31

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

希望这会有所帮助。

Before you search in the database, you may first split the sentence, and then check each word is a stop word using a loop (ex: while-loop),

Hope this helps.

葬心 2024-11-14 13:48:31

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

希望它对某人有帮助!

I have a similar function in c#. It Will accept a string or sentence ex:
INPUT: File access Mr Brown
OUTPUT: "'File' and 'access' and 'Mr' and 'Brown'"
Transleted to SQL: '"File" and "access" and "Mr" and "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;
    }

Hope it helps somebody!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文