-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_classification.py
60 lines (49 loc) · 1.74 KB
/
word_classification.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
import re
import collections
class WordClassification:
def __init__(self):
self.new_search()
def new_search(self):
self.all_words = dict()
self.best_word = ''
self.best_points = 0
def get_word(self):
return self.best_word
def run(self, word):
self.word = word
self.points = 0
self._get_length_points()
self._get_one_point_letters()
self._get_two_point_letters()
self._get_three_point_letters()
self._get_letter_combo_points()
self.all_words[self.points] = self.word
if self.points > self.best_points:
print('new best word: {}'.format(word))
self.best_points = self.points
self.best_word = self.word
return True
return False
def _get_length_points(self):
self.points += self.word.__len__() * 2
def _get_one_point_letters(self):
letters = 'AOGUERNLTSDI'
for letter in letters:
self.points += self.word.count(letter) * 2
def _get_two_point_letters(self):
letters = 'CFHMYPVWB'
for letter in letters:
self.points += self.word.count(letter) * 4
def _get_three_point_letters(self):
letters = 'QJXKZ'
for letter in letters:
self.points += self.word.count(letter) * 6
def _get_letter_combo_points(self):
matches = re.finditer(r"(\w)\1+", self.word)
for matchNum, match in enumerate(matches):
self.points += 5
def get_next_word(self):
if not isinstance(self.all_words, collections.OrderedDict):
self.all_words = collections.OrderedDict(sorted(self.all_words.items()))
length, word = self.all_words.popitem()
return word