如何从具有非前缀关键字的列表中进行搜索
我正在编写一个程序来从列表中搜索名称,即使关键字不在名称前面(这就是我的意思是非前缀)
例如,如果我的列表, 我也需要找到它们是乐器,我在搜索文本框中输入“guit”。
它应该找到名称“Guitar, Guitarrón, Acoustic Guitar, Bass Guitar, ...”
或类似 Longdo Dictionary 的 搜索建议。
这是我的简单而愚蠢的算法(这就是我能做的)
const int SEARCHROWLIMIT = 30;
private string[] DoSearch(string Input, string[] ListToSearch)
{
List<string> FoundNames = new List<string>();
int max = 0;
bool over = false;
for (int k = 0; !over; k++)
{
foreach (string item in ListToSearch)
{
max = (max > item.Length) ? max : item.Length;
if (k > item.Length) continue;
if (k >= max) { over = true; break; }
if (!Input.Equals("Search")
&& item.Substring(k, item.Length - k).StartsWith(Input, StringComparison.OrdinalIgnoreCase))
{
bool exist = false;
int i = 0;
while (!exist && i < FoundNames.Count)
{
if (item.Equals(FoundNames[i]))
{
exist = true;
break;
}
i++;
}
if (!exist && FoundNames.Count < SEARCHROWLIMIT)
FoundNames.Add(item);
else if (FoundNames.Count >= SEARCHROWLIMIT) over = true;
}
}
}
return FoundNames.ToArray();
}
我认为这个算法对于大量的名字来说太慢了,经过几次试验和错误,我决定添加 SEARCHROWLIMIT 来打破操作 我还认为有一些现成的方法可以做到这一点。
另一个问题是我需要按弦乐、打击乐器等类别以及原产国来搜索乐器。所以我需要按类型和国家/地区过滤器搜索它们。
我怎样才能实现这个目标?
I am programming a program to search the name from the list and I need to find them even if the keyword is not in front of the names (that's what I mean non-prefix)
e.g. if I my list is the music instruments and I type "guit" to the search textbox.
It should find the names "Guitar, Guitarrón, Acoustic Guitar, Bass Guitar, ..."
or something like this Longdo Dictionary's search suggestion.
here is my simple and stupid algorithm (that's all I can do)
const int SEARCHROWLIMIT = 30;
private string[] DoSearch(string Input, string[] ListToSearch)
{
List<string> FoundNames = new List<string>();
int max = 0;
bool over = false;
for (int k = 0; !over; k++)
{
foreach (string item in ListToSearch)
{
max = (max > item.Length) ? max : item.Length;
if (k > item.Length) continue;
if (k >= max) { over = true; break; }
if (!Input.Equals("Search")
&& item.Substring(k, item.Length - k).StartsWith(Input, StringComparison.OrdinalIgnoreCase))
{
bool exist = false;
int i = 0;
while (!exist && i < FoundNames.Count)
{
if (item.Equals(FoundNames[i]))
{
exist = true;
break;
}
i++;
}
if (!exist && FoundNames.Count < SEARCHROWLIMIT)
FoundNames.Add(item);
else if (FoundNames.Count >= SEARCHROWLIMIT) over = true;
}
}
}
return FoundNames.ToArray();
}
I think this algorithm is too slow for a large number of names and after several trial-and-error, I decided to add SEARCHROWLIMIT to breaks the operation
And I also think there're some readymade methods that can do that.
And another problem is I need to search music instruments by a category like strings, percussions, ... and by the country of origins. So I need to search them with filter by type and country.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 LINQ,您可以编写如下代码:
如果您的产品集相当小,则可以使用 LINQ-to-Objects。否则,您应该使用数据库并查看 LINQ-to-SQL。
Using LINQ you could write code like this:
If your sets of products is reasonably small, you can use LINQ-to-Objects. Otherwise you should use a database and have a look at LINQ-to-SQL.
一句话。数据库!
说真的,如果您想要执行所有这些不同的搜索,请考虑将数据放入具有简化您遇到的分类问题的架构的数据库中。 Sql Server Express 现在支持全文搜索,这对于您尝试执行的搜索类型。
有一篇不错的博客文章 此处了解如何将 FTS 与 Linq-to-Sql 结合使用。
One word. Database!
Seriously, if you want to do all these different searches, consider placing your data into a database with a schema that simplifies the categorization issues you are having. Sql Server Express now supports full text search which would be very useful for the kind of search you are trying to perform.
There's a nice blog post here about using FTS with Linq-to-Sql.
我希望我已经正确阅读了你最初的问题。此函数将返回列表中包含以您的子字符串开头的单词的任何项目。可以在分割参数中添加更多标点符号。给定一个包含以下内容的列表:
对“abc”的搜索将找到“abcdef”和“def abc”,但找不到“def abc”。
I hope I have read your intiial question properly. This function will return any item from the list that contains a word starting with your substring. More punctuation could be added to the split parameters. Given a list with the following contents:
A search on "abc" will find "abcdef" and "def abc", but not "defabc".