Matt McManis
Matt McManis

Reputation: 4675

Check for word match count in random letters

I have a string of 500 random letters and a 10,000 dictionary word list.

I want to check the letters for word matches.

If there are 5 matches or greater I want it to return the list of matched words.

However this foreach and Contains.() doesn't seem to work correctly or return correct matches. It is also returning partial matches and single letters.


// 500 Random Letters
string letters = "bliduuwfhbgphwhsyzjnlfyizbjfeeepsbpgplpbhaegyepqcjhhotovnzdtlracxrwggbcmjiglasjvmscvxwazmutqiwppzcjhijjbguxfnduuphhsoffaqwtmhmensqmyicnciaoczumjzyaaowbtwjqlpxuuqknxqvmnueknqcbvkkmildyvosczlbnlgumohosemnfkmndtiubfkminlriytmbtrzhwqmovrivxxojbpirqahatmydqgulammsnfgcvgfncqkpxhgikulsjynjrjypxwvlkvwvigvjvuydbjfizmbfbtjprxkmiqpfuyebllzezbxozkiidpplvqkqlgdlvjbfeticedwomxgawuphocisaejeonqehoipzsjgbfdatbzykkurrwwtajeajeornrhyoqadljfjyizzfluetynlrpoqojxxqmmbuaktjqghqmusjfvxkkyoewgyckpbmismwyfebaucsfueuwgio"

// Dictionary Words List
string[] words = File.ReadAllText(@"C:\dictionarywords.txt").Split('\n');

// Word Matches List
List<string> matches = new List<string>();


// Check for Word matches in Letters
foreach (var x in words)
{
    // Add to list if match
    if (letters.Contains(x))
    {
        matches.Add(x);
    }
}

// Return Matched Words if 5 or greater
if (matches.Count() >= 5)
{
    textBox.Text = string.Join("\n", matches);
}

Examples

Word matches found by eye:

Code Match Returns:

Upvotes: 1

Views: 116

Answers (1)

Horkrine
Horkrine

Reputation: 1407

Your code is working as intended. It IS finding those words, but it's also finding additional words. I'd suggest taking out the words you don't want to show up in a search. For example, a lot of people use this in a profanity filter. So if a sentence contains a curse word, it omits the word because it found it in the dictionary of curse words. Give it a try with a much smaller list with words you've put in yourself and test the results. Try changing those words to other words?

Upvotes: 2

Related Questions