forked from wilkie/twisty-words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.lua
85 lines (71 loc) · 1.74 KB
/
dictionary.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- Dictionary
-- Loads a list of words from a file
Dictionary = {}
function Dictionary:load(filename)
local f = assert(io.open(filename, "r"))
local ret = {}
for line in f:lines() do
local words = {}
for i = 3, 6, 1 do
words[i] = {}
end
for word in line:gmatch("[^:]+") do
if #word >= 3 and #word <= 6 then
table.insert(words[#word], word)
end
end
-- create list of unique letters
local letters = {}
local hasletter = {}
for char in words[6][1]:gmatch(".") do
if not hasletter[char] then
hasletter[char] = {}
table.insert(letters, char)
end
end
-- randomize letter ordering
local n = #letters
while n >= 2 do
-- n is last pertinent index
local k = math.random(n) -- 1 <= r <= n
-- swap
letters[n], letters[k] = letters[k], letters[n]
n = n - 1
end
-- fill byletter
byletter = {}
for _,char in ipairs(letters) do
byletter[char] = {}
for i = 3, 6, 1 do
for _,word in pairs(words[i]) do
if word:sub(1,1) == char then
table.insert(byletter[char], word)
end
end
end
table.sort(byletter[char])
end
bylength = {}
for i = 3, 6, 1 do
bylength[i] = {}
end
for char,words in pairs(byletter) do
for _,word in pairs(words) do
table.insert(bylength[#word], word)
end
end
-- print out list!
for _,words in pairs(bylength) do
for _,word in pairs(words) do
print(word)
end
end
entry = {}
entry["by_letter"] = byletter
entry["by_length"] = bylength
entry["letters"] = letters
table.insert(ret, entry)
end
-- screw dictionary objects
return ret
end