Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spkmark · Jan 18, 2017 at 09:05 AM · dictionarystring formatting

C# Check spelling of a word.

hey all. I've been on google all day trying to find out how to check the spelling of a word in game. obviously getting nowhere, that's why i'm here. I'n my game you make up a word by selecting different letters. when you think you have a word you click the 'check word' button and the word gets checked to find out if its an actual word in the dictionary. I've downloaded a text document that has 35000 words. this is as far as i can get on the information that's available. if anyone could help i'd really appreciate it. thank you.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by EDevJogos · Jan 18, 2017 at 09:21 PM

You'll definitely need a tree for this or something like it, i would sugest separating them into alphabetic order for first and second letter, this will leave you with a search of +or- 79 words.

You can use a Dictionary<char, Dictionary<char, List<string>>>, while reading your document look at the first letter of it and set into the first dictionary as key, than in this dictionary look at the second letter and set it as the key and the accutual word add as value to the List, the code would be something like this:

 private Dictionary<char, Dictionary<char, List<string>>> _wordsTree = new Dictionary<char, Dictionary<char, List<string>>>();

 public void Start()
     {
         //This will create a tree like thing.
 
         foreach(char __letter in _alphabet)
         {
             _wordsTree.Add(__letter, new Dictionary<char, List<string>>());
         }
 
         foreach(char __nodeKey in _wordsTree.Keys)
         {
             foreach (char __letter in _alphabet)
             {
                 _wordsTree[__nodeKey].Add(__letter, new List<string>());
             }
         }
 
         //Then you'll have to do read your document word by word and add to the tree:
 
         //This foreach is just a fake ideia of code, i don't remmeber how to read words from
         // a document just look on google shouldn't be hard to find.
         foreach(string __word in wordsDocument.GetWord())
         {
             //This is accutualy how you would add something to the tree.
             _wordsTree[__word[0]][__word[1]].Add(__word); 
         }
 
         //Then to see if the word exist just do:
 
         string __playerWord = "potato"; //This is the word the player entered.
 
         _wordsTree[__playerWord[0]][__playerWord[1]].Contains(__playerWord);
     }


Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spkmark · Jan 19, 2017 at 10:46 AM 0
Share

thank you for taking the time to explain an answer. it looks very close to what I've been looking for. yesterday i actually come up with a little solution that kind of works. but i will test your code to see if it works better. in your code I've done the following to get rid of errors.

private IEnumerable _alphabet; public textasset wordsDocument;

is that accetable to work... also i have one last error in there. "GetWord" says

Error CS1061 'TextAsset' does not contain a definition for 'GetWord' and no extension method 'GetWord' accepting a first argument of type 'TextAsset' could be found (are you missing a using directive or an assembly reference?)

sorry if theres a simple answer. im still learning.

here is the code im using that i made myself.

public textasset wordDoc; public text usedWords; public text word;

public void checkspellings() {

     if (usedWords.text.Contains(word.text))
     {
         clearWord();
         hasIt();
     }

     if (!usedWords.text.Contains(word.text)) {
         if (wordDoc.text.Contains(word.text))
         {
             scoreB += scoreA;
             score.text = "SCORE: " + scoreB;
             print(word.text);
             allOfIt.text = allOfIt.text + "\n" + word.text;
             print(allOfIt.text);
             Debug.Log("Yes $$anonymous$$ate");
             clearWord();
             spellingA();
         }
     }
     if (!wordDoc.text.Contains(word.text))
     {
         Debug.Log("Nope");
         clearWord();
         spellingC();
     }
   
 }

its works great... only problem is that it is also checking against part of a word. i.e if my word is "gth" it will accept it as a spelling because it'll find it at the end of a word like "length". also when it searches usedWords if I've already added something like "mits" it then wont let me add "its" because it is a part of "mits". Thank you again for your help and time.

avatar image EDevJogos spkmark · Jan 19, 2017 at 02:24 PM 0
Share

The problem in your code is that Contains when used with strings check if somewhere on the string contains the string value you passed, if you want to check if the string is the exact same as other string you can use the operator ==, just keep in $$anonymous$$d that wordDoc.text return a string with the entire doc, so comparing it with == against a word would never work, you need to get just the single word from the document.

And in my code the GetWord method does not exist, as i said on the comment that part is just some fake code giving the idea of what should be done, in that case get a single word from the document.

Pass me the link to this word.doc, and i give you a working code.

avatar image spkmark · Jan 19, 2017 at 03:19 PM 0
Share

This is the word doc I'm using. https://raw.githubusercontent.com/docdis/english-words/master/words2.txt i understand what your saying and that makes perfect sense. thank you again for your help.

Also I've been playing around with this. It counts the text file into lines and i can pick a line by number witch prints out the word on that line into my console.

     string[] dataLines = wordsDocument.text.Split();
     string[] dataPairs = new string[dataLines.Length];
     int lineNum = 0;
     foreach (string line in dataLines)
     {
         dataPairs[lineNum++] += line.Split();
     }

     print(dataLines[0]);  //this returns A (the first word from the list).

not sure if it was getting me any closer but it felt right. thanks again.

avatar image EDevJogos spkmark · Jan 19, 2017 at 03:57 PM 0
Share

Here it is:

 private string _alphabet = "abcdefghijklmnopqrstwuvxyz";
     private Dictionary<char, Dictionary<char, List<string>>> _wordsTree = new Dictionary<char, Dictionary<char, List<string>>>();
 
     public TextAsset wordsDocument;
 
     // Use this for initialization
     void Start ()
     {
         //This will create a tree like thing.
 
         foreach (char __letter in _alphabet)
         {
             _wordsTree.Add(__letter, new Dictionary<char, List<string>>());
         }
 
         foreach (char __node$$anonymous$$ey in _wordsTree.$$anonymous$$eys)
         {
             foreach (char __letter in _alphabet)
             {
                 _wordsTree[__node$$anonymous$$ey].Add(__letter, new List<string>());
             }
         }
 
         //Then you'll have to do read your document word by word and add to the tree:
 
         //This foreach is just a fake ideia of code, i don't remmeber how to read words from
         // a document just look on google shouldn't be hard to find.
         foreach (string __word in wordsDocument.text.Split('\n'))
         {
             //This will consider all words as lower case, if you want to have upper case
             //just add the upper case letters into the alphabet string and remove the __word.ToLower()
             //just leave the TrimeEnd.
             //Some of the words have a space at the end Trim will remove it.
             string __lowerCaseWord = __word.ToLower().TrimEnd();
 
             if (__lowerCaseWord.Length > 1)
             {
                 //This is accutualy how you would add something to the tree.
                 _wordsTree[__lowerCaseWord[0]][__lowerCaseWord[1]].Add(__lowerCaseWord);
             }
         }
 
         //Then to see if the word exist just do:
 
         string __playerWord = "potato"; //This is the word the player entered.
 
         Debug.Log(_wordsTree[__playerWord[0]][__playerWord[1]].Contains(__playerWord));
     }
avatar image spkmark · Jan 19, 2017 at 04:20 PM 0
Share

This Works Perfectly. Thank you so much for taking the time to ease my suffering lol. and responding so quickly. looking at this code and the level I'm currently at it would have taken me some time to get it right. however i will be going over this code to make sure i 100% understand it. thanks again.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

typedefing in unity 1 Answer

Need help with targetting 1 Answer

adding dictionary in the game 0 Answers

How to get the keys of a SimpleJSON dictionary ? 3 Answers

Accessing a class from a different script 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges