-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
240 lines (213 loc) · 7.31 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
# -*- coding: utf-8 -*-
import random
import threading
from functools import partial
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from solver import (getFrequencyDict, getWordScore, isValidW, letterValues,
loadWords, merge)
__version__ = '0.5'
DEBUG = False
PUNKTACJA = 'pl_punktacja.txt'
DICTIONARY = 'pl_slownik.txt'
LETTERS_DICT = {'a': 9, 'ą': 1, 'b': 2, 'c': 3, 'ć': 1, 'd': 3,
'e': 7, 'ę': 1, 'f': 1, 'g': 2, 'h': 2, 'i': 8,
'j': 2, 'k': 3, 'l': 3, 'ł': 2, 'm': 3, 'n': 5,
'ń': 1, 'o': 6, 'ó': 1, 'p': 3, 'r': 4, 's': 4,
'ś': 1, 't': 3, 'u': 2, 'w': 4, 'y': 4, 'z': 5,
'ź': 1, 'ż': 1}
LETTERS = ''.join([k * v for k, v in LETTERS_DICT.items()])
Builder.load_string('''
# -*- coding: utf-8 -*-
<RootWidget>
canvas:
Color:
rgb: (.87,.87,.87)
Rectangle:
pos: self.pos
size: self.size
opis: opis
prog: prog
literki: literki
size: 100, 50
BoxLayout:
padding: 0
orientation: 'vertical'
Label:
id:tytul
canvas.before:
Color:
rgb: (142/255, 173/255, 83/255)
Rectangle:
pos: self.pos
size: self.size
color: .87,.87,.87,1
text: 'ScrabbleSolver'
text_size: self.size
font_size: self.height -9
halign: 'center'
valign: 'middle'
size_hint: 1, .06
ProgressBar:
id: prog
max: 1
value: 0
size_hint: 1, 0.005
Label:
color: 1-.85,1-.85,1-.85,1
text: 'Wpisz literki w pole poniżej:'
text_size: self.width-20, self.height-20
halign: 'center'
valign: 'middle'
size_hint: 1, .05
TextInput:
id: literki
multiline: False
background_color: 1,1,1,0
color: 1-.85,1-.85,1-.85,1
text: ''
font_name: 'RobotoMono-Regular'
font_size: self.height/2
size_hint: 1, .08
padding: (self.size[0]/2 - 23*len(self.text),self.height/4,0,0)
Label:
color: 1-.85,1-.85,1-.85,1
id: opis
text: ''
text_size: self.width-20, self.height-20
halign: 'center'
valign: 'middle'
size_hint: 1, .5
BoxLayout:
size_hint: 1, .15
Button:
id: random
background_color: 142/255, 173/255, 83/255,1
text: '+7 random'
text_size: self.width - 10, self.height - 10
font_size: self.height/5
valign: 'middle'
halign: 'center'
on_press: root.onPressRandom()
Button:
id: szukajButton
text: 'Szukaj'
text_size: self.width - 10, self.height - 10
font_size: self.height/5
valign: 'middle'
halign: 'center'
on_press: root.onPress()
on_release: root.search()
''')
def printDbg(*arg, **kwargs):
if DEBUG:
print(*arg, **kwargs)
class chooseWords(threading.Thread):
def __init__(self, root, wordlist, n, update_bar):
threading.Thread.__init__(self)
self.root = root
self.hand = root.hand
self.ltrVals = root.ltrVals
self.opis = root.opis
self.bestWords = root.bestWords
self.wordlist = wordlist
self.n = n
self.update_bar = update_bar
def run(self):
self.update_bar('reset')
for nr, word in enumerate(self.wordlist):
if nr % 1000 == 0:
self.update_bar(len(self.wordlist))
if isValidW(word, self.hand):
score = getWordScore(word, self.n, self.ltrVals)
self.bestWords.append((word, score))
wordsStr = ''
if self.bestWords:
for w in reversed(self.bestWords[-10:]):
wordsStr += '{0}, {1}pt\n'.format(*w)
self.opis.text = (
'Najodpowiedniejsze słowa to:\n\n' + wordsStr)
else:
self.opis.text = (
'Nie znaleziono odpowiednikow.\nMam za słaby słownik.')
self.update_bar('full')
class RootWidget(BoxLayout):
def __init__(self, **kwargs):
BoxLayout.__init__(self)
# super(RootWidget, self).__init__(**kwargs)
self.BAZY = False
self.wordlist = []
self.ltrVals = {}
self.literki.text = ''.join(random.sample(LETTERS, 7))
self.bestWords = []
self.prog.value = 0
self.hand = 0
def onPress(self):
if self.BAZY:
self.opis.text = 'Przeliczanie konfiguracji liter...'
else:
self.opis.text = 'Ładowanie słownika...'
def onPressRandom(self):
self.literki.text += ''.join(random.sample(LETTERS, 7))
def printText(self, text):
# TODO wrap straight commands to print text in layout
pass
def search(self):
self.bestWords = []
self.prog.value = 0
sekwencja = self.literki.text.replace(' ', '')
if len(sekwencja) < 3:
self.opis.text = 'Wprowadź 3 lub więcej liter i wyszukaj ' + \
'jeszcze raz.'
else:
if not self.BAZY:
self.importFiles()
self.hand = getFrequencyDict(sekwencja)
lettersNr = len(sekwencja)
self.opis.text = 'Obliczanie... \n\nCzekaj'
wordlists = self.wordlist[:lettersNr - 2]
crpWordlist = list(merge(*wordlists))
# TODO implement thread search
work = chooseWords(self, crpWordlist, lettersNr + 1,
lambda x: Clock.schedule_once(
partial(self.update_bar, x)))
work.start()
self.prog.value = 1.0
def importFiles(self):
self.ltrVals = letterValues(PUNKTACJA)
self.wordlist = loadWords(DICTIONARY)
self.BAZY = True
printDbg('imported {:d} words.'.format(
len(list(merge(*self.wordlist)))))
def update_bar(self, wordlistLen, dt=None):
'''
Updates progress bar and text window during search.
wordlistLen: (int)
returns: (None) updated layout
'''
wordsStr = ''
if wordlistLen == 'full':
self.prog.value = 1.0
elif wordlistLen == 'reset':
self.prog.value = 0
else:
self.prog.value = self.prog.value + (1000 / wordlistLen)
# dumps weaker words
best = self.bestWords[-10:]
best = sorted(best, key=lambda x: x[1], reverse=True)
for w in best:
wordsStr += '{0:>{2}}, {1:>4}pt\n'.format(
w[0], w[1], len(best[0][0]))
self.opis.text = 'Najodpowiedniejsze słowa to:\n\n' + wordsStr
class ScrabbleSolver(App):
def build(self):
return RootWidget()
def on_pause(self):
return True
def on_resume(self):
pass
# fun fun fun
if __name__ == '__main__':
ScrabbleSolver().run()