What do you think provides more efficiency on this matter, words put into a table, or a text file?
Forum ‹ BigDB ‹ Dictionary, bigDB or GSfile?
7 posts
• Page 1 of 1
Dictionary, bigDB or GSfile?
Hello everyone. I'm totally new to playerio so I hope I can have your patience
. I'm developing a word game similar to scrabble but a bit fast paced. What I want that whenever a user types a word, the system should check if the word exists in the word-list.
What do you think provides more efficiency on this matter, words put into a table, or a text file?
What do you think provides more efficiency on this matter, words put into a table, or a text file?
- lightbeam
- Posts: 21
- Joined: November 4th, 2011, 5:57 pm
Re: Dictionary, bigDB or GSfile?
Make a text file with one line per word, embed that file in your DLL, then use the EmbeddedResource class in your server code to read it and parse it. If you make a Dictionary<string, bool> and add each word to it, you can then check if a word is valid by calling ContainsKey on it. Easy, simple, fast.
http://playerio.com/documentation/refer ... edresource
http://playerio.com/documentation/refer ... edresource
-

Henrik - .IO
- Posts: 570
- Joined: January 4th, 2010, 1:53 pm
Re: Dictionary, bigDB or GSfile?
Henrik wrote:If you make a Dictionary<string, bool> and add each word to it, you can then check if a word is valid by calling ContainsKey on it. Easy, simple, fast.
Sorry, can you be more clear on this part? Dictionary<string, bool> how will I use this function, and what's the boolean for?
And I checked the embeddedresource section and found those Get methods
EmbeddedResource.GetImage
EmbeddedResource.GetByte[]
Can those be used for txt files?
- lightbeam
- Posts: 21
- Joined: November 4th, 2011, 5:57 pm
Re: Dictionary, bigDB or GSfile?
Ok, so the objective is to be able to quickly and efficiently answer the question "does string X exist in this list of strings", and to get that list of strings into your game.
EmbeddedResource has the GetBytes method, and to convert a byte[] into a string, you can use Encoding.UTF8.GetString or something like that. Now you'll have your entire text file as a string. If your textfile then had all the words on separate lines, you need to split it out, which you do with the Split method on string, so you do something like this:
Now you have an array with all your words in it. The simplest way to check if a word exists in your dictionary, you could do something like this:
However, that will do a linear search of your list every time which is a bit inefficient, which is why I suggested you use a dictionary instead. Like this:
That makes lookups faster since we're using a hashmap, but at the cost of a bit more memory and a bit more processing during setup. It's a dictionary from string->bool because we have to have a second type of values, and I just used bool because they're small and we don't really care about the value.
How large is your dictionary? Number of words? Kilobytes?
EmbeddedResource has the GetBytes method, and to convert a byte[] into a string, you can use Encoding.UTF8.GetString or something like that. Now you'll have your entire text file as a string. If your textfile then had all the words on separate lines, you need to split it out, which you do with the Split method on string, so you do something like this:
- Code: Select all
byte[] bytes = PlayerIO.EmbeddedResource.GetBytes("dictionary.txt");
string words = Encoding.UTF8.GetString(bytes);
string[] wordsarray = words.Split("\n");
Now you have an array with all your words in it. The simplest way to check if a word exists in your dictionary, you could do something like this:
- Code: Select all
public bool Valid(string word) {
return Array.Exist(wordsarray, (w) => w == word);
}
However, that will do a linear search of your list every time which is a bit inefficient, which is why I suggested you use a dictionary instead. Like this:
- Code: Select all
//Setup
var dictionary = new Dictionary<string, bool>();
foreach(string word in wordsarray) {
dictionary[word] = true;
}
//Valid method
public bool Valid(string word) {
return dictionary.ContainsKey(word);
}
That makes lookups faster since we're using a hashmap, but at the cost of a bit more memory and a bit more processing during setup. It's a dictionary from string->bool because we have to have a second type of values, and I just used bool because they're small and we don't really care about the value.
How large is your dictionary? Number of words? Kilobytes?
-

Henrik - .IO
- Posts: 570
- Joined: January 4th, 2010, 1:53 pm
Re: Dictionary, bigDB or GSfile?
Thank you for the thorough reply. I get it now. I'm using OSPD4 word list which is around 107 thousand words, the text file is only 650 kb though.
- lightbeam
- Posts: 21
- Joined: November 4th, 2011, 5:57 pm
Re: Dictionary, bigDB or GSfile?
If you want more memory efficiency, maybe a HashSet would be better than a Dictionary, since you wouldn't need the unused Boolean.
- jasonMcIntosh
- Posts: 83
- Joined: February 25th, 2011, 4:51 am
7 posts
• Page 1 of 1