- Home /
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.
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);
}
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.
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.
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.
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));
}
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
Follow this Question
Related Questions
typedefing in unity 1 Answer
Need help with targetting 1 Answer
adding dictionary in the game 0 Answers