Lookin up a word in a dictionary for word game

Hi,

I’m making a game where player can type a word up to 13 letters long. I got a text file with all the words in English, but I’m not sure what is the best way to convert this into Lua table so I can look up for player’s word to validate if it is in the dictionary.

I’m afraid that if I put all the words into a single Lua table it will consume too much RAM or worse the device will crash.

Should I split the list of words in to many lua modules. One module for 2 letters words or one module for words that start with “A”. Or if I’m totally lost and there is a ancient secret dev technique to do it.

Could anyone show me the way?

Cheers,

Have you tried? I do not think it will be a problem to be honest. I saw a number that the English language has about 600,000 words. Multiply that by average word length (apparently 4.7 for English) and you get about 3,000,000 characters. That’s not much. There will be some overhead to put them in a Lua table with each word used as a key in the table (for fast lookups). But even if it takes 10x as much space it is still only 30Mb of RAM.

@britzl what do you mean by using each word as a key in the table for fast look ups?

Is it something like this:
dict = {“ape”, “bee”}

Cheers,

I mean like this:

local words = {
    ["ape"] = true,
    ["bee"] = true
}

local function is_valid_word(word)
    return words[word]
end

print(is_valid_word("ape")) -- true
print(is_valid_word("wxyz")) -- false
1 Like

Awesome, thanks @britzl You rock!

I have a massive json that I read into a table of around 275k words and have no issue on all platforms :slight_smile:

@gianmichele what would be the difference between reading a json file into a table from having a Lua table from the beginning?

I’m a linguist making games, I have never used a json file before not sure what the advantages are.

It’s mostly about the maintenance and preparation.
E.g. what is the workflow that suits your use case best?

1 Like

Initially I had it as a lua table, but that for some reason was creating issues in a html5 build (desktop and mobile were fine). i solved by switching it to a json

1 Like

Ahhh ok ok. I’m working on a game for mobile, I guess I’ll be fine but I have it in mind if I move to HTML5 thanks :smiley:

1 Like