How to search from a list with keywords without a prefix

I am programming a program to search for a name from a list, and I need to find them even if the keyword is not in front of the names (which I do not mean the prefix)

for example , if I use a list of musical instruments and I type “guit” in the search text box. He should find the names "Guitar, Guitarrón, Acoustic Guitar, Bass Guitar, ..."
or something like this sentence of the Longdo dictionary .

here is my simple and dumb 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();
    }

, , SEARCHROWLIMIT, , , .

, , , ,... . .

?

+3
3

LINQ, :

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);

, LINQ-to-Objects. LINQ-to-SQL.

+6

. !

, , , , , . Sql Server Express , , .

FTS Linq-to-Sql.

+2
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();
}

, . , , . ​​ . :

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

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

0
source

Source: https://habr.com/ru/post/1740610/


All Articles