summaryrefslogtreecommitdiff
path: root/analyze_new.py
blob: 3999c8c95eb6d4be9bc8d57afc831df8f0ffb220 (plain)
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
#!/usr/bin/python3
import re
import operator
import itertools
import sys
import argparse
from pprint import pprint

def load_text(n):
    with open('new/text' + str(n) + '_orig') as f:
        text = f.read()

    lines = re.split(r'[\?\.\!]+', text) 
    lines = list(map(lambda l: l.strip(), lines))
    lines = list(map(lambda s: s.replace(' ', '').replace('-', ''), lines))

    return "\n".join(lines).strip()

def spaceitout(string,amount):
    amountint= int(amount)
    pile= ""
    for char in string:
        pile= pile + char + " "*amount
    return pile.strip()

def letter_pos(letter):
    if letter in predefined_table:
        return predefined_table[letter]
    else:
        letter_table = table[letter]
        if len(letter_table) > 0:
            return letter_table[0][0]
        else:
            return None

def die(t):
    pprint(t)
    sys.exit()

def dump_lines():
    i = 0
    for l in lines:
        print('%d ' % i, end='')
        print(l)
        i += 1
    sys.exit()

alphabet = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
a = spaceitout(alphabet, 1)
a = a.replace(' ', ',')
#print(a)
#sys.exit()

text = load_text(10)
#text += load_text(1) + "\n"
#text += load_text(2) + "\n"
#text += load_text(3) + "\n"
#text += load_text(4)
text = text.upper()

predefined_table = {
    'М': 8,
    'А': 5,
    'Р': 3,
    'Ш': 1,
    'Щ': 1,
    'И': 1,
    'У': 2,
    'Ю': 10,
    'Й': 5,
    'Л': 2,
    'Ц': 2,
    'О': 4,
    'Д': 8,
    'Т': 9,
    'П': 5,
    'Э': 3,
    'Х': 5
}

table = {}
lines = text.split("\n")

#dump_lines()

for a in alphabet:
    table[a] = {}
    for line in lines:
        indexes = [m.start() for m in re.finditer(a, line)]
        for index in indexes:
            index += 1
            if index in table[a]:
                table[a][index] += 1
            else:
                table[a][index] = 1

for a, t in table.items():
    ts = sorted(t.items(), key=operator.itemgetter(1), reverse=True)
    table[a] = ts

#die(table)

variants = []
for line in lines:
    valid = []
    for a in table:
        if True:
            index = letter_pos(a)
            if index == None:
                continue
            
            try:
                if line[index-1] == a and a not in valid:
                    valid.append(a)
            except IndexError:
                continue
        
        else:
            letter_table = table[a]
            if not len(letter_table):
                continue

            for i in range(2):
                if i > len(letter_table)-1:
                    continue

                if a == 'Щ':
                    index = 1
                else:
                    index = letter_table[i][0]
                try:
                    if line[index-1] == a and a not in valid:
                        valid.append(a)
                except IndexError:
                    continue

    if len(valid) == 0:
        valid = ['?']
    variants.append(valid)
    #print('('+''.join(valid)+')')

variants = list(filter(lambda a: len(a), variants))
#variants = list(map(lambda l: sorted(l), variants))
#die(variants)

variants = variants[:7]
res = list(itertools.product(*variants))
for r in res:
    if r[0] == 'Ь': continue
#    if r[1] != 'А': continue
#    if r[5] != 'Л': continue
#    if r[4] != 'О': continue
    
    #if r[3] != 'И': continue
    #if r[2] != 'Д': continue
    #if r[0] != 'Х': continue
    
    print(''.join(r))