-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_solver.py
64 lines (49 loc) · 1.76 KB
/
wordle_solver.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
import random
import nltk
nltk.download('words')
from nltk.corpus import words
opts = set([w.lower() for w in words.words() if len(w) == 5])
not_included = ['scaut']
opts = [x for x in opts if x not in not_included]
class Word:
def __init__(self, opts):
self.contains = []
self.does_not_contain = []
self.sd = {}
self.tried_words = []
self.opts = opts
def add(self, letters):
self.contains.extend(list(letters))
def subtract(self, letters):
self.does_not_contain.extend(list(letters))
def check_word(self, word):
for key in self.sd.keys():
if word[key] != self.sd[key]:
return False
return True
def specify(self, letter, idx):
self.sd[idx] = letter
def tried(self, word):
self.tried_words.append(word)
def check_vowel(self, word, threshold=3):
n = 0
for l in list('aeious'):
if l in word:
n += 1
return n >= threshold
def vowel_word(self):
vowel_words =[w for w in opts if check_vowel(w)]
return random.choice(vowel_words)
def generate_options(self):
optc = [x for x in self.opts if all([letter in x for letter in self.contains])]
opts_nc = [x for x in optc if not any([letter in x for letter in self.does_not_contain])]
opts_specific = [x for x in opts_nc if self.check_word(x)]
opts_specific = [x for x in opts_specific if x not in self.tried_words]
return opts_specific
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("action")
args = parser.parse_args()
action = args.action
if action == 'new':
return Word()