-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
273 lines (213 loc) · 9.14 KB
/
main.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import itertools
import math
import pickle
import random
import re
import sys
class Countdown:
vowels = 'aeiou'
consonants = 'bcdfghjklmnpqrstvwxyz'
words = []
len_words = {}
vowel_pile_size = 67
consonant_pile_size = 74
vowel_pile = ''
consonant_pile = ''
board = ''
def __init__(self, board=None, word_file=None):
try:
if word_file:
raise IOError()
self.words = pickle.load(file('pickles/words.pickle'))
self.len_words = pickle.load(file('pickles/len_words.pickle'))
loaded_from = 'pickle.'
except IOError:
# Get a list of words.
word_file = word_file or '/usr/share/dict/words'
with open(word_file, 'rb') as f:
for line in f:
# Ignore any which have more than 9 letters.
line = line.strip().lower()
if len(line) > 9:
continue
# Ignore any which have more than 5 vowels.
if self.count_vowels(line) > 5:
continue
# Ignore any which have more than 6 consonants.
if self.count_consonants(line) > 5:
continue
self.words.append(line)
try:
self.len_words[len(line)].append(line)
except KeyError:
self.len_words[len(line)] = [line]
pickle.dump(self.words, file('pickles/words.pickle', 'w'))
pickle.dump(self.len_words, file('pickles/len_words.pickle', 'w'))
loaded_from = 'words list.'
print 'Loaded {:,} words from {}'.format(
len(self.words),
loaded_from
)
if board == 'load':
exit()
self.build_vowel_pile()
self.build_consonant_pile()
print 'Generated piles.'
if not board:
self.generate_tiles()
else:
self.board = board
self.solve()
def count_vowels(self, word):
return len([letter for letter in word if letter in self.vowels])
def count_consonants(self, word):
return len([letter for letter in word if letter in self.consonants])
def build_vowel_pile(self):
try:
self.vowel_pile = pickle.load(file('pickles/vowel_pile.pickle'))
except IOError:
# The amount of times a single letter appears within the pile varies
# from letter to letter according to it's frequency within natural
# English. As a naive implementation, we'll just base the weightings
# off the number of occurrences within our dataset.
#
# For each vowel, work out how many times it appears within the word set.
vowel_occurrences = {letter: 0.00 for letter in self.vowels}
for word in self.words:
for letter in self.vowels:
vowel_occurrences[letter] += word.count(letter)
total = sum(vowel_occurrences.values())
# For each of the letters, work out it's percentage value of frequency
# within the word set and then work out how many letters of each type
# are needed as a percentage of the pile size, then build the actual pile.
self.vowel_pile = ''.join(
letter * int(
math.ceil(
(self.vowel_pile_size * vowel_occurrences[letter]) / total
)
)
for letter in self.vowels
)
pickle.dump(self.vowel_pile, file('pickles/vowel_pile.pickle', 'w'))
def build_consonant_pile(self):
try:
self.consonant_pile = pickle.load(file('pickles/consonant_pile.pickle'))
except IOError:
# The amount of times a single letter appears within the pile varies
# from letter to letter according to it's frequency within natural
# English. As a naive implementation, we'll just base the weightings
# off the number of occurrences within our dataset.
#
# For each consonant, work out how many times it appears within the word set.
consonant_occurrences = {letter: 0.00 for letter in self.consonants}
for word in self.words:
for letter in self.consonants:
consonant_occurrences[letter] += word.count(letter)
total = sum(consonant_occurrences.values())
# For each of the letters, work out it's percentage value of frequency
# within the word set and then work out how many letters of each type
# are needed as a percentage of the pile size, then build the actual pile.
self.consonant_pile = ''.join(
letter * int(
math.ceil(
(self.consonant_pile_size * consonant_occurrences[letter]) / total
)
)
for letter in self.consonants
)
pickle.dump(self.consonant_pile, file('pickles/consonant_pile.pickle', 'w'))
def generate_tiles(self, consonants=None, vowels=None):
# Minimum requirements for character selections are 3 vowels and
# 4 consonants and a total of 9 letters. This therefore means that
# the combinations possible are:
#
# 3 vowels, 6 consonants.
# 4 vowels, 5 consonants.
# 5 vowels, 4 consonants.
if not consonants:
if vowels:
consonants = 9 - vowels
else:
vowels = random.randint(3, 5)
consonants = 9 - vowels
if not vowels:
if consonants:
vowels = 9 - consonants
else:
consonants = random.randint(4, 6)
vowels = 9 - consonants
print 'Picking {} consonants and {} vowels.'.format(
consonants,
vowels,
)
board = ''
# Copy the piles as we'll need to modify them.
consonant_pile, vowel_pile = self.consonant_pile, self.vowel_pile
# Get consonants from the pile, removing them from the pile as they
# are chosen (once a letter is removed from a pile, it can't be picked
# again).
for x in range(1, consonants + 1):
index = random.randint(0, len(consonant_pile)-1)
board = board + consonant_pile[index]
consonant_pile = consonant_pile[:index] + consonant_pile[index+1:]
# Get vowels from the pile.
for x in range(1, vowels + 1):
index = random.randint(0, len(vowel_pile)-1)
board = board + vowel_pile[index]
vowel_pile = vowel_pile[:index] + vowel_pile[index+1:]
print 'Your board: {}'.format(board.upper())
self.board = board
def solve(self):
self.board = self.board.lower()
# The first thing we want to do is limit our word set to only contain
# words which have the letters in our board.
board_regex = re.compile(r'[^{}]+'.format(self.board))
word_list = []
len_words = {}
for word in self.words:
# print re.search(board_regex, word)
if re.search(board_regex, word) == None:
word_list.append(word)
try:
len_words[len(word)].append(word)
except KeyError:
len_words[len(word)] = [word]
print 'Filtered to {} words.'.format(len(word_list))
# print word_list
# Manipulate the board into every possible order and check if it's a
# valid word according to the word list.
print 'Attempting to solve {}.'.format(self.board)
total_words = 0
found = set()
# 4+ consonant regex
pattern = re.compile(r'([{consonants}]{{3,}})'.format(
consonants=self.consonants
))
for x in range(9, 4, -1):
print 'Trying {} letter words.'.format(x)
iteration = 0
level_words = 0
if total_words > 5:
break
for word in itertools.permutations(self.board, x):
permutation = ''.join(word)
# You don't really get words with 3+ consonants together, so
# just ignore those.
consonant_test = re.search(pattern, permutation)
if consonant_test is None and permutation not in found:
if x in len_words and permutation in len_words[x]:
print permutation
found.add(permutation)
level_words += 1
total_words += 1
print '{} words found: {}'.format(total_words, found)
if __name__ == '__main__':
if len(sys.argv) == 2:
# We're using a manual board. (main.py potatoes)
Countdown(sys.argv[1])
elif len(sys.argv) == 3:
# We're loading a new word list in. (main.py load file.txt)
Countdown(sys.argv[1], sys.argv[2])
else:
# Lets just play automatically.
Countdown()