-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (111 loc) · 3.41 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
def checker(d: dict, word):
for c in d['in']:
if c not in word:
return False
for c in d['not in']:
if c in word:
return False
if d['pos']:
for i, c in d['pos']:
if word[i] != c:
return False
if d['not pos']:
for i, c in d['not pos']:
if word[i] == c:
return False
return True
def check_var_in_pos(var, pos_var):
for p, v in pos_var:
if var == v:
return p
return False
with open('russian_nouns.txt', 'r', encoding='utf8') as f:
words = [word.rstrip() for word in f.readlines()]
filtered_words = list(
filter(lambda x: len(x) == 5 and '-' not in x, words)
)
all_chars = {}
for word in filtered_words:
for c in list(set(word)):
if c not in all_chars.keys():
all_chars[c] = 1
else:
all_chars[c] += 1
all_words = {}
for word in filtered_words:
counter = 0
for c in word:
cc = word.count(c)
counter += (
all_chars[c]
/ cc
* sum(1 / 2 ** (n - 1) for n in range(1, cc + 1))
)
all_words[word] = counter
most_best_words = sorted(
[(k, v) for k, v in all_words.items()],
key=lambda x: int(x[1]),
reverse=True,
)
in_var = ''
not_in_var = ''
pos_var = []
not_pos_var = []
result = ''
first_time = True
while result != 'жжжжж':
best_word = ''
for word, weight in most_best_words:
if checker(
{
'in': in_var,
'not in': not_in_var,
'pos': pos_var,
'not pos': not_pos_var,
},
word,
):
if not best_word:
best_word = word
print(f'{word}: {weight}')
if best_word:
print('Лучшее слово:', best_word)
else:
print('Ошибка, нет таких слов!')
break
data = input(
'Введите результаты и введенное слово'
+ (
' (не обязательно если вы использовали лучшее слово, '
'пример: "жжбсб посол" или просто "жжбсб")'
if first_time
else ''
)
+ ': '
).split()
first_time = False
if len(data) == 2:
selected_word = data[1]
if selected_word == '':
selected_word = best_word
else:
selected_word = best_word
result = data[0]
if len(result) != 5 or len(selected_word) != 5:
raise ValueError
for p, c in enumerate(result):
i = selected_word[p]
if c == 'с':
if pos := check_var_in_pos(i, pos_var):
not_pos_var += list(
filter(lambda x: x[0] != pos, enumerate(i * 5))
)
else:
not_in_var += i
elif c == 'б':
in_var += i
not_pos_var += [(p, i)]
elif c == 'ж':
pos_var += [(p, i)]
else:
raise ValueError