如何将单词放入变量并检查它是否与数据库中的数据匹配
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要类似
string[]words=sentence.Split(' ');
的内容,然后检查单词数组中的每个条目。You need something like
string[] words = sentence.Split(' ');
then check each entry in the words array.在数据库中搜索之前,您可以先拆分句子,然后使用循环(例如: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.
我在c#中有类似的功能。它将接受一个字符串或句子,例如:
输入:文件访问布朗先生
输出:“‘文件’和‘访问’以及‘先生’和‘布朗’”
转换为 SQL:'“文件”和“访问”和“先生”和“布朗”'
希望它对某人有帮助!
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"'
Hope it helps somebody!