-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpysc.py
executable file
·171 lines (134 loc) · 5.58 KB
/
pysc.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
#!/usr/bin/env python3
# pysc
# Kashev Dalmia | @kashev | [email protected]
# pysc.py
""" A Scrabble cheater script. Provides all words from given letters. """
# IMPORTS
import argparse
import collections
import enum
import itertools
import json
import string
try:
from tabulate import tabulate
pretty_print = True
except ImportError as e:
pretty_print = False
@enum.unique
class ScrabbleDictionary(enum.Enum):
""" The different scrabble dictionaries possible. """
sowpods = "sowpods"
twl = "twl"
wwf = "wwf"
class Word:
""" A class for words, which helps keep track of word score. """
def __init__(self, word, letters, score_dict):
self.word = word
self.score = 0
letter_list = list(letters)
for letter in word:
if letter in letter_list:
letter_list.remove(letter)
self.score += score_dict[letter]
def has_letters(self, required_letters):
required_list = list(required_letters)
letter_list = list(self.word)
for letter in required_list:
try:
letter_list.remove(letter)
except ValueError:
return False
return True
def load_anagram_dict(scrabble_dictionary):
""" Load the scrabble dictionary of choice. """
DICT_PRE = "dict/"
DICT_EXT = "_anagram.txt"
dict_path = DICT_PRE + scrabble_dictionary.value + DICT_EXT
anagram_dict = collections.defaultdict(list)
with open(dict_path, 'r') as dict_file:
for line in dict_file:
words = line.split()
anagram_dict[tuple(words[0])] = words[1:]
return anagram_dict
def load_scoring_dict(scrabble_dictionary):
""" Load the scoring dictionary. """
SCORE_PATH = "dict/scores.json"
with open(SCORE_PATH, 'r') as score_file:
score_dict = json.loads(score_file.read())
if scrabble_dictionary is ScrabbleDictionary.wwf:
return score_dict["wwf"]
else:
return score_dict["scrabble"]
def score_word(word, score_dict):
""" Given a word, score it using the scoring dicitonary. """
return sum([score_dict[letter] for letter in word])
def find_words(letters, anagram_dict, score_dict, required="", length=None):
""" Find all the words that can be made from the given letters. """
BLANK = '.'
num_blanks = letters.count(BLANK)
non_blank_letters = ''.join(sorted(letters + required)).replace(BLANK, '')
target_word_dict = {}
for blanks in itertools.product(string.ascii_lowercase, repeat=num_blanks):
letters = sorted(non_blank_letters + ''.join(sorted(blanks)))
# pick length range
if length is None:
length_range = range(2, len(letters) + 1)
else:
length_range = [length]
for word_length in length_range:
for combination in itertools.combinations(letters, word_length):
if combination in anagram_dict:
for target_string in anagram_dict[combination]:
target_word = Word(target_string, non_blank_letters,
score_dict)
# Ensure the word has required letters.
if not target_word.has_letters(required):
continue
# Add the word if it doesn't exist
if target_string not in target_word_dict:
target_word_dict[target_string] = target_word
# Otherwise only add the word if the new version is
# higher scoring.
else:
if (target_word_dict[target_string].score <
target_word.score):
target_word_dict[target_string] = target_word
return target_word_dict
def main():
""" The main body of the script. """
parser = argparse.ArgumentParser(
description="A Scrabble cheater script. More information at "
"https://github.com/kashev/pysc",
formatter_class=argparse.RawTextHelpFormatter)
helpstring = "choose a dictionary:\n"
for sd in ScrabbleDictionary:
helpstring += "- {}\n".format(sd.name)
parser.add_argument("-d", "--sdict", help=helpstring,
type=ScrabbleDictionary,
default=ScrabbleDictionary.wwf)
parser.add_argument("-r", "--required", type=str, default="",
help="required letters, must be in the word")
parser.add_argument("-l", "--length", type=int, default=None,
help="length of desired words")
parser.add_argument("letters", type=str,
help="Letters from which words will be made")
args = parser.parse_args()
anagram_dict = load_anagram_dict(args.sdict)
score_dict = load_scoring_dict(args.sdict)
target_word_dict = find_words(args.letters.lower(), anagram_dict,
score_dict, args.required.lower(),
args.length)
words = [[word.word, word.score, len(word.word)]
for key, word in target_word_dict.items()]
words.sort(key=lambda x: x[1], reverse=True)
headers = ["word", "score", "length"]
if pretty_print:
table = tabulate(words, headers=headers)
print(table)
else:
print("{} | {} | {}".format(headers[0], headers[1], headers[2]))
for word in words:
print("{} | {} | {}".format(word[0], word[1], word[2]))
if __name__ == '__main__':
main()