Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Dictonary

Discussion in 'Scripting' started by Phocus, Jul 6, 2015.

  1. Phocus

    Phocus

    Joined:
    Jul 6, 2015
    Posts:
    18
    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.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    Phocus likes this.
  3. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Kiwasi likes this.
  4. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    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

    Code (CSharp):
    1.     void LoadWordList(string filename, int minLength=2)
    2.     {
    3.         allWords = new List<string>();
    4.        
    5.         using (StreamReader r = new StreamReader(filename))
    6.         {
    7.             string oneLine = "";
    8.             while ((oneLine = r.ReadLine()) != null)
    9.             {
    10.                 // don't grab words below a minimum length
    11.                 if (oneLine.Length > minLength)
    12.                 {
    13.                     allWords.Add(oneLine);
    14.                 }
    15.             }
    16.         }
    17.     }
    18.  
    That's modified from an example I found on StackOverflow. In my Awake() function I load it like this:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. if (allWords.Contains(chosenWord))
    2.     //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
     
  5. Phocus

    Phocus

    Joined:
    Jul 6, 2015
    Posts:
    18

    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!