summaryrefslogtreecommitdiff
path: root/analyze_new.py
blob: db066c2af24b3bb327f2b8f131f14d60cbe1b0c1 (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
#!/usr/bin/python3
import re
import operator
import itertools
import sys
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 s: s.replace(' ', '').replace('-', ''), lines))

    return "\n".join(lines).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

alphabet = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'

text = ''
text += load_text(1) + "\n"
text += load_text(3) + "\n"
#text += load_text(2)
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")

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

#pprint(table['Э'])
#sys.exit()

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
        
        if False:
            letter_table = table[a]
            if not len(letter_table):
                continue

            for i in range(3):
                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

    variants.append(valid)
    #print('('+''.join(valid)+')')

variants = list(filter(lambda a: len(a), variants))
variants = variants[11:31]

pprint(variants)
sys.exit()

#variants = variants[11:]
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))