views:

111

answers:

3

The Problem is Solved. Thanks for every answers.
First of all, sorry if my english or my post got any mistakes.

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.

please help me.

P.S. Me and my friends are just student from Thailand and developing the project to compete in Microsoft Imagine Cup 2010 and please become fan on our facebook page [KRATIB][3]. And we're so sorry we don't have much information in English but you can talk to us in English.

+2  A: 

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.

spender
Great. Downvote with no explanation. Why?
spender
+5  A: 

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.

dtb
Be careful. Apparently reccomending a DB is not appreciated!
spender
A: 
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".

Anthony Pegram
Thanks so much for all the answers.now I found the best search without inventing any algorithms.
aNui