Dictonary

I am creating a simple word find. The users selects tiles that have letters on them.

After a word is selected, I need to compare it to a dictionary list of words.

The list is in csv format. There is no key value, just a single stream of words.

I have googled until I am blue in the face, I do not understand how to load a dictionary. Can anyone send me a link to an example that works.

You can load a csv with System.IO. Once you have the data loaded, you can move it into an array with string.Split. I would then suggest adding it to a HashSet for fast searching.

Dictionary has a specific meaning in C#. You don’t actually want to use a dictionary. So drop that off your search terms.

1 Like

If it’s just a stream of words, separated by a comma or space, you can use a stream reader, and use comma or space as a delimiter.

1 Like

Can you change your list from CSV to each word being on a single line? If so, you could use the code below that I’m using to read in a word list.

Also, in case this helps, there’s some good info on this page: http://wiki.unity3d.com/index.php/Choosing_the_right_collection_type

    void LoadWordList(string filename, int minLength=2)
    {
        allWords = new List<string>();
       
        using (StreamReader r = new StreamReader(filename))
        {
            string oneLine = "";
            while ((oneLine = r.ReadLine()) != null)
            {
                // don't grab words below a minimum length
                if (oneLine.Length > minLength)
                {
                    allWords.Add(oneLine);
                }
            }
        }
    }

That’s modified from an example I found on StackOverflow. In my Awake() function I load it like this:

LoadWordList ("Assets/Resources/ospd.txt");

(You’d need to change the location of your word list, of course.)

And then when I want to see if the chosen word is actually in the list I do this:

if (allWords.Contains(chosenWord))
    //do something here

Oh yeah, you can also pass in an optional “minimum length” when you load the word list – that way if you don’t want 1 or 2-letter words you just don’t load them into the list.

I hope that helps.

Jay

This worked great. Thank you! Although after importing system.io I had to explicitly call unitygame.random. not a big deal.
For some reason I thought Unity would handle it differently. Thank you everyone!