forked from kongr45gpen/objective-commit-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble.py
284 lines (238 loc) · 9.51 KB
/
scrabble.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
274
275
276
277
278
279
280
281
282
283
284
import copy
from enum import Enum, auto
import math
from typing import List
import enchant
import numpy as np
import re
from functools import lru_cache
class Direction(Enum):
Down = auto()
Right = auto()
uk_dictionary = enchant.Dict("en_UK")
us_dictionary = enchant.Dict("en_US")
class Scrabble:
DIMENSION = 15
def __init__(self):
self.matrix = np.array([[None for x in range(self.DIMENSION)] for y in range(self.DIMENSION)])
# matrix[x][y]
# x↑ = right
# y↑ = down
def suggest_positions(self, word):
word_size = len(word)
candidate_positions = []
for i, letter in enumerate(word):
positions = list(zip(*np.where(self.matrix == letter)))
for col, row in positions:
writeable = True
# Determine if we have enough space to write the word vertically
if i <= row and row + word_size - i < self.DIMENSION:
for j in range(1, i + 1):
char = self.matrix[col][row - j]
if (char is not None) and char != word[i - j]:
writeable = False
break
for j in range(1, word_size - i):
char = self.matrix[col][row + j]
if (char is not None) and char != word[i + j]:
writeable = False
break
if writeable:
candidate_positions.append((col, row - i, Direction.Down))
writeable = True
# Attempt to write horizontally
if i <= col and col + word_size - i < self.DIMENSION:
for j in range(1, i + 1):
char = self.matrix[col - j][row]
if (char is not None) and char != word[i - j]:
writeable = False
break
for j in range(1, word_size - i):
char = self.matrix[col+j][row]
if (char is not None) and char != word[i + j]:
writeable = False
break
if writeable:
candidate_positions.append((col - i, row, Direction.Right))
return set(candidate_positions)
def put(self, word, location, direction):
word = word.lower()
for idx, c in enumerate(word):
x = location[0]
y = location[1]
if direction == Direction.Down:
y += idx
elif direction == Direction.Right:
x += idx
self.matrix[x][y] = c
self.score.cache_clear()
def put_best(self, word):
word = word.lower()
candidate_positions = self.suggest_positions(word)
best_score = None
best_scrabble = None
for pos in candidate_positions:
s2 = copy.deepcopy(self)
s2.put(word, (pos[0], pos[1]), pos[2])
if s2.is_valid():
score = s2.score()
if best_score is None or score > best_score:
best_score = score
best_scrabble = s2
if best_scrabble is not None:
self.matrix = best_scrabble.matrix
self.score.cache_clear()
else:
return True
def put_first_word(self, word):
middle = math.floor(self.DIMENSION / 2)
hor = copy.deepcopy(self)
ver = copy.deepcopy(self)
if len(word) + middle >= self.DIMENSION:
start = self.DIMENSION - len(word)
if start < 0:
# fail
return False
hor.put(word, (start, middle), Direction.Right)
ver.put(word, (middle, start), Direction.Down)
else:
hor.put(word, (middle, middle), Direction.Right)
ver.put(word, (middle, middle), Direction.Down)
results = [ hor, ver ]
return [r for r in results if r.is_valid()]
def put_best_many(self, words):
all_candidates : List[Scrabble] = [ self ]
# Put first word in the middle of scrabble
while len(self.get_words()) == 0 and len(words) > 0:
init_scrabble = self.put_first_word(words[0])
words.pop(0)
if init_scrabble is not False and len(init_scrabble) > 0:
all_candidates = init_scrabble
break
# Add every word consecutively to the candidate Scrabbles
for word in words:
word = word.lower()
new_candidates = []
# For every candidate Scrabble, find all possible candidate
# sub-Scrabbles
for candidate in all_candidates:
positions = candidate.suggest_positions(word)
for pos in positions:
c = copy.deepcopy(candidate)
c.put(word, (pos[0], pos[1]), pos[2])
if c.is_valid():
new_candidates.append(c)
if len(new_candidates) > 0:
# No need to keep old Scrabbles, just store the latest good Scrabbles
all_candidates = new_candidates
# All possible Scrabbles calculated, now find the best one
best = max(all_candidates, key=lambda c: c.score())
self.matrix = best.matrix
self.score.cache_clear()
@lru_cache(maxsize=None)
def score(self):
return sum([ self._word_score(w) for w in self.get_words() ])
def _word_score(self, word, modifier=None):
word = word.lower()
score = 0
for idx, c in enumerate(word):
if idx >= 7:
# Large word limit
break
character_score = 0
if c in [ 'a', 'e', 'i', 'o', 'u', 'l', 'n', 's', 't', 'r' ]:
character_score = 1
elif c in [ 'd', 'g' ]:
character_score = 2
elif c in [ 'b', 'c', 'm', 'p' ]:
character_score = 3
elif c in [ 'f', 'h', 'v', 'w', 'y' ]:
character_score = 4
elif c in [ 'k' ]:
character_score = 5
elif c in [ 'j', 'x' ]:
character_score = 8
elif c in [ 'q', 'z' ]:
character_score = 10
else:
# Some number or symbol or emoji or something. These kinds of characters can be of very high quality, or not.
# Let's just forget they exist at all
character_score = 0
score += character_score
return score
def is_valid(self):
return all([uk_dictionary.check(word) or us_dictionary.check(word) for word in self.get_words()])
def get_words(self):
words = []
# Vertical words
for i in range(self.DIMENSION):
word = ""
for j in range(self.DIMENSION):
char = self.matrix[i][j]
is_letter = char != ' ' and char is not None
if is_letter:
# We have a normal character that will
# form the word
word += char
if not is_letter or j == self.DIMENSION - 1:
# We have a new word!! maybe
if len(word.strip()) <= 1:
word = ""
continue
# We actually have a new word!
words.append(word)
word = ""
word = ""
for j in range(self.DIMENSION):
char = self.matrix[j][i]
is_letter = char != ' ' and char is not None
if is_letter:
# We have a normal character that will
# form the word
word += char
if not is_letter or i == self.DIMENSION - 1:
# We have a new word!! maybe
if len(word.strip()) <= 1:
word = ""
continue
# We actually have a new word!
words.append(word)
word = ""
return words
def process_commit_message(message):
# First line of commit only
message = message.partition('\n')[0]
# First 80 git characters only
message = message[0:80]
words = re.findall(r"[A-Za-z]+", message)
# First 7 characters from each word... No overdoing it
words = [w[0:7] for w in words]
return words
def __str__(self):
str = ""
for y in range(self.DIMENSION):
for x in range(self.DIMENSION):
if self.matrix[x][y] is None:
str += ' '
else:
str += self.matrix[x][y]
str += "\n"
return str
if __name__ == "__main__":
s = Scrabble()
# s.put("lite", (2,2), Direction.Down)
# s.put("baby", (2,2), Direction.Right)
# s.put("beard", (4,2), Direction.Down)
s.put_best_many(["establish", "lite", "EnumAttr", "fault", "TFL_DimensionTypeAttr"])
print(s)
# for position in s.suggest_positions("daredevilitiness"):
# print("SUGGESTED POSITION:")
# s2 = copy.deepcopy(s)
# s2.put("daredevilitiness", (position[0], position[1]), position[2])
# print(s2)
# print(s2.is_valid())
# print(s)
# print(s.get_words())
# print(s.is_valid())
# print(s.suggest_positions("baeeby"))
# print(s.suggest_positions("boy"))