-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
42 lines (34 loc) · 987 Bytes
/
setup.py
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
import glob
import errno
import pickle
#def getScore(word):
# """Return a tuple with a score and breakdown a given word would get."""
# breakdown = []
# for letter in word:
# breakdown.append(letterScore(letter))
# total = sum(breakdown)
# return total
def setup(trie):
"""Add each word from csv files to trie data structure."""
path = './words/*.txt'
files = glob.glob(path)
for name in files:
try:
f = open(name, "r").read().splitlines()
lines = list(f)
print(len(lines), 'total words')
for word in lines:
# make sure word is in lowercase
word = word.lower()
trie.addWord(word, getScore(word))
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
def save_trie(trie, name):
"""Save a trie to a file."""
with open('./words/' + name + '.pkl', 'wb') as f:
pickle.dump(trie, f, pickle.HIGHEST_PROTOCOL)
def load_trie(name):
"""Load a trie from a file."""
with open('./words/' + name + '.pkl', 'rb') as f:
return pickle.load(f)