Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

c# - Find Nearly Matching Phrase from a List of String

I want to search from a list of string and my lookup phrase has only let's say sometimes 70% matching from the list but still I want to treat it as found. In the following code my lookup phrase is "xxx in the middle sample xxx". If I use Contains or Any this yields no result. From my lookup word I want to search for matches containing words "in the middle" (not case sensitive). I would prefer 3 or more matching consecutive words for example "in the middle". Please help.

C#

  static void Main(string[] args)
    {
        var wordListToLookUp = new List<string> {"In the middle","This is a sample text in the middle","There is secret in the middle of the forest","None of your business"};
        var lookupWord = "xxx in the middle sample xxx";
        foreach(var word in wordListToLookUp)
        {
            var exist = word.Contains(lookupWord);
            //even if my look up has only 70% match or nearly match, I would like to consider them as found
            Console.WriteLine("Found match: {0}", exist);
        }
        Console.ReadLine();
    }

Output

Found match: False
Found match: False
Found match: False
Found match: False

Expected output

Found match: True
Found match: True
Found match: True
Found match: False
question from:https://stackoverflow.com/questions/65919930/find-nearly-matching-phrase-from-a-list-of-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I have found a solution for you however, the code is not optimized. But, you can optimize it. This code exact result what you ask for. Here is my code =>

static void Main(string[] args)
{
            bool exist = false;
            var wordListToLookUp = new List<string> { "In the middle", "This is a sample text in the middle", "There is secret in the middle of the forest", "None of your business" };
            var lookupWord = "xxx in the middle sample xxx";
            List<string> checkerarrary = lookupWord.ToLower().Split(' ').ToList();
            foreach (var word in wordListToLookUp)
            {
                exist = false;
                List<string> currentStringarrary = word.ToLower().Split(' ').ToList();
                
                if (checkerarrary.Count >= 3 && currentStringarrary.Count>=3)
                {
                    for(int i=0; i<= checkerarrary.Count-3;i++)
                    {
                        for (int c = 0; c <= currentStringarrary.Count - 3; c++)
                        {
                            if(checkerarrary[i]== currentStringarrary[c] 
                                && checkerarrary[i+1] == currentStringarrary[c+1]
                                && checkerarrary[i + 2] == currentStringarrary[c+2])
                            {
                                exist = true;
                            }
                        }
                    }
                }
                Console.WriteLine("Found match: {0}", exist);
            }
}

Note: I have used the search text at least 3 word .You can adjust it as per you requirements. Please check the code and let me know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...