How to do a search from a list with non-prefix keywords

北城余情 提交于 2019-12-07 11:57:01

问题


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?


回答1:


Using LINQ you could write code like this:

var resultSet = products

    // filter products by category
    .Where(product => product.Category == "strings")

    // filter products by origin
    .Where(product => product.Origin == "italy")

    // filter products whose name contains a word starting with "guit"
    .Where(product => (" " + product.Name).Contains(" guit"))

    // limit the result set to the first 30 matching products
    .Take(30);

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.




回答2:


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.




回答3:


static List<string> GetItemsWithWordsStartingWithSubstring(List<string> list, string substring)
{
    var query = from str in list
                from item in str.Split(' ')
                where item.StartsWith(substring, StringComparison.InvariantCultureIgnoreCase)
                select str;

    return query.ToList();
}

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:

"abcdef","defabc","def abc","xyz"

A search on "abc" will find "abcdef" and "def abc", but not "defabc".



来源:https://stackoverflow.com/questions/2615218/how-to-do-a-search-from-a-list-with-non-prefix-keywords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!