-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
122 lines (102 loc) · 3 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
# Filter words by length
def filter_length(wordlist: list, length: int):
# Length must be a positive integer
if length < 1:
raise ValueError()
# Simple matching
ret = []
for word in wordlist:
if len(word) == length:
ret.append(word)
return ret
# Filter words through whitelisted characters
def filter_whitelist(wordlist: list, whitelist: list):
import re
# Assemble regex for filtering
pattern = ''
for chr in whitelist:
pattern = pattern + '(?=.*' + chr + ')'
# Simple matching
ret = []
for word in wordlist:
if re.match(pattern, word):
ret.append(word)
return ret
# Filter words through blacklisted characters
def filter_blacklist(wordlist: list, blacklist: list):
import re
# Assemble regex for filtering
pattern = '^[^'
for chr in blacklist:
pattern = pattern + chr
pattern = pattern + ']+$'
if pattern == '^[^]+$':
return wordlist
# Simple matching
ret = []
for word in wordlist:
if re.match(pattern, word):
ret.append(word)
return ret
# Prune duplicates in list
def prune(lst: list):
ret = []
for item in lst:
if item not in ret:
ret.append(item)
return ret
# Get character frequency map
def get_freq_map(wordlist: list):
freq_map = {}
for word in wordlist:
for char in word:
if char not in freq_map:
freq_map[char] = 1
continue
freq_map[char] = freq_map[char] + 1
ret = sorted(freq_map.items(), key=lambda item: item[1], reverse=True)
for index, item in enumerate(ret):
ret[index] = item[0]
return ret
# Guessing function
def guess(fmap: list, whitelist: list, blacklist: list):
import re
# Get most frequent unlisted char
for chr in fmap:
if chr not in whitelist and chr not in blacklist:
if re.match('^[yY]', input('Is there a "' + chr + '"? ')):
whitelist.append(chr)
break
blacklist.append(chr)
break
# Return most frequent character in filtered list
return get_freq_map(filter_blacklist(filter_whitelist(words, whitelist), blacklist))[0]
# Query the user for target length
try:
word_length = int(input('Word length: '))
except ValueError:
print('Invalid word length! Exiting...')
exit(1)
# Read words.txt
words = filter_length(open('words.txt', 'r').read().split('\n'), word_length)
# Main guessing routine
whitelist = []
blacklist = []
while True:
import re
current_fmap = get_freq_map(words)
guess(current_fmap, whitelist, blacklist)
# Braking condition: if len(filter_whitelist(words, whitelist)) == len(words)
buffer = filter_blacklist(filter_whitelist(words, whitelist), blacklist)
if len(buffer) == len(words):
break
words = buffer
for index, word in enumerate(words):
if index < 5:
if re.match('^[yY]', input('Is it "' + word + '"? ')):
print('yay ig')
break
continue
if input('okay what is it then: ') in words:
print('yea makes sense')
break