C# checking if a word is in an English dictionary?

14,988

Solution 1

You can use NetSpell library for checking this. It can be installed through Nuget Console easily with the following command

PM> Install-Package NetSpell

Then, loop through the words and check them using the library

NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary(); 

oDict.DictionaryFile = "en-US.dic"; 
oDict.Initialize();
string wordToCheck = "door";
NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling(); 

oSpell.Dictionary = oDict; 
if(!oSpell.TestWord(wordToCheck))
{
    //Word does not exist in dictionary
    ...
}

Solution 2

Since you're looking specifically for valid Scrabble words, there are a few APIs that validate words for Scrabble. If you use anything that's not for that intended purpose then it's likely going to leave out some words that are valid.

Here's one, here's another, and here's a separate question that lists available APIs.

So that I can add some value beyond just pasting links, I'd recommend wrapping this in your own interface so that you can swap these out in case one or another is unavailable (since they're all free services.)

public interface IScrabbleWordValidator
{
    bool IsValidScrabbleWord(string word);
}

Make sure your code only depends on that interface, and then write implementations of it that call whatever APIs you use.

Share:
14,988
pyjamas
Author by

pyjamas

Updated on June 14, 2022

Comments

  • pyjamas
    pyjamas almost 2 years

    I am trying to go through a list of words and for each one determine if it is a valid English word (for Scrabble). I'm not sure how to approach this, do I have to go find a text file of all English words and then use file reading and parsing methods to manually build a data structure like a trie or hashmap - or can I find those premade somewhere? What is the simplest way to go about this?

  • pyjamas
    pyjamas almost 8 years
    I'm not sure if using those online APIs would work because my list is actually very big. What I did is generate 500k random 7 letter words, and I'm just curious to see how many of those would be valid Scrabble words (because I was playing scrabble today and pulled a 7 letter word out of the bag :P).