Adding and referencing an actual dictionary

Hi guys,

first of all I’m sorry if I posted this in the wrong section of the forum. This however seemed to me as the most appropriate place.

I’ll be making a sort of scrabble game for tablets. In order to check if the words the user tried to input however, I need a way to check if these are real words, and honestly, I have no idea how to do this. I tried searching for an Unity Dictionary library, but the only things which showed up are questions about the dictionary function. I feel like there is something out there however, because adding an entire dictionary by hand is obviously very time consuming. So do you guys know any library’s or handy functions? Anything is welcome!

Well, honestly C# has a Dictionary object. It does probably more than what you want to do. You could make a dictionary by letter. that would help. So say you have a dictionary like this:

Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();

Then you fill it with 26 entries from A-Z and each list will contain words from that letter set. Thus when you do an Exists on any of the lists you are not going through every word in the real dictionary.

Also, a good idea that you make all of your entries lower case (or upper case) so that when you do a search against it, the processor doesn’t have to make everything lower case. (or upper) to check against it.

What you need to do is allocate a giant array of strings, then scan the array for matching words. Like this:

// Fill this with all the words you would like to support.
// If this is too much work then you could fill it at runtime by parsing a text file.
string[] allWords = new string[]
{
    "apple",
    "pear",
    "pencil",
    "bottle"
};
bool IsWord(string input)
{
    for (int i = 0; i < allWords.Length; ++i)
        if (input == allWords[i])
            return true;
    return false;
}

@Daniel: You’d be much better off using a Dictionary instead of an array (for performance reasons), but if you did use an array, there’s no reason to make your own search function when arrays have the IndexOf function already (and generic Lists have Contains).

But the question isn’t about really about arrays, it’s about a word database. If you have OS X (or Linux I assume), you’re in luck, since it includes some word databases already. Look in /usr/share/dict, specifically the web2 file, which is all 235K works from Webster’s second edition…slightly out of date since it’s from 1934, but hey, no copyright. Offhand I’d read it with ReadAllText, split on newlines so you get an array, then convert that to a Dictionary.

–Eric

@Eric5h5: Oops, you’re right, my bad.

In C# a Dictionary is a key-value hashmap. From what I can tell, you want a collection of words with the ability to quickly look up whether a word is in the collection. For this you should use a HashSet instead.

Apart from that, follow Eric’s suggestion.