-
Notifications
You must be signed in to change notification settings - Fork 1
/
nausea.py
201 lines (156 loc) · 7.38 KB
/
nausea.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'''
Ver. 1.3
Implemented unittests.
Text processing broken into smaller functions.
Routine function now works with only one file permanently.
'''
import re
from nltk.stem.snowball import RussianStemmer
from nltk.corpus import stopwords
from alphabet_detector import AlphabetDetector
from collections import Counter
from multiprocessing import Pool
import sqlite3
import unittest
def getText(path):
'''
Function that returns string with text from asked file.
'''
with open(path, encoding='utf-8') as file:
text = file.read()
return text
def preprod(text):
'''
Drop all non-letter characters from text, split string into words,
return list of lowercase words.
'''
text = re.sub('[^A-Za-zА-Яа-я]', ' ', text).split()
text = [word.lower() for word in text if len(word) > 2]
return text
def letterSwap(word):
'''
Turns latin-like letters in word into cyrillic ones and reverse if fails.
'''
ad = AlphabetDetector()
# latin keys cyr values
latin_like_cyr = {'a': 'а', 'c': 'с', 'e': 'е', 'o': 'о', 'p': 'р',
'y': 'у', 'A': 'А', 'B': 'В', 'C': 'С', 'E': 'Е',
'H': 'Н', 'K': 'К', 'M': 'М', 'O': 'О', 'P': 'Р',
'T': 'Т', 'X': 'Х'}
cyr_like_latin = {v: k for k, v in latin_like_cyr.items()}
for char in latin_like_cyr.keys():
word = word.replace(char, latin_like_cyr[char])
if ad.only_alphabet_chars(word, 'CYRILLIC'):
return word
else:
for char in cyr_like_latin:
word = word.replace(char, cyr_like_latin[char])
return word
def cleanText(text):
'''
Function checks and repairs words with hidden latin characters in and vv.
Function assuming that there are only latin and cyrillic characters
in text.
'''
ad = AlphabetDetector()
st = RussianStemmer()
is_broken = False
clean_text = []
for word in text:
if ad.only_alphabet_chars(word, 'CYRILLIC'):
clean_text.append(word)
elif ad.only_alphabet_chars(word, 'LATIN'):
clean_text.append(word)
else:
is_broken = True
clean_text.append(letterSwap(word))
clean_text = [st.stem(word) for word in clean_text]
return clean_text, is_broken
def nauseaRate(text):
commons_count = 0
commons = Counter(text)
if not text:
return 0
for x in range(5):
commons_count += commons.most_common(5)[x][1]
nausea = commons_count / len(text)
return nausea
def sqlSubmit(data):
'''
Submit results to table
'''
conn = sqlite3.connect('nausea.db')
with conn:
c = conn.cursor()
c.execute('DROP TABLE IF EXISTS text_info')
# print('Table dropped')
c.execute(
'CREATE TABLE text_info (filename TEXT, nausea REAL, cheat INT)')
# print('Table created')
c.executemany('INSERT INTO text_info VALUES (?,?,?)', data)
conn.commit()
class ut_case(unittest.TestCase):
def test_letterSwap(self):
'''
Some letters in first elements of cases are swapped with latin equals.
letterSwap function should find them and swap with cyrillic ones
'''
cases = [('привeт', 'привет'), ('кoшка', 'кошка'), ('Нellо', 'Hello'),
('саt', 'cat')]
for case in cases:
self.assertEqual(letterSwap(case[0]), case[1])
def test_text_preproduction(self):
'''
This test checks how preprod function splits text into words.
'''
lorem_ipsum_ru = 'Далеко-далеко за словесными горами в стране \
гласных и согласных живут рыбные тексты. Вдали \
от всех живут они в буквенных домах на берегу \
Семантика большого языкового океана. Маленький \
ручеек Даль журчит по всей стране и обеспечивает ее \
всеми необходимыми правилами.'
pre_stem = ['далеко', 'далеко', 'словесными', 'горами', 'стране',
'гласных', 'согласных', 'живут', 'рыбные', 'тексты',
'вдали', 'всех', 'живут', 'они', 'буквенных', 'домах',
'берегу', 'семантика', 'большого', 'языкового', 'океана',
'маленький', 'ручеек', 'даль', 'журчит', 'всей', 'стране',
'обеспечивает', 'всеми', 'необходимыми', 'правилами']
self.assertEqual(preprod(lorem_ipsum_ru), pre_stem)
def test_text_repair(self):
'''
This test compares stemming of fine text and 'fraud' text.
'''
upright_pre_stem = ['далеко', 'далеко', 'словесными', 'горами',
'стране', 'гласных', 'согласных', 'живут',
'рыбные', 'тексты', 'вдали', 'всех', 'живут',
'они', 'буквенных', 'домах', 'берегу', 'семантика',
'большого', 'языкового', 'океана', 'маленький',
'ручеек', 'даль', 'журчит', 'всей', 'стране',
'обеспечивает', 'всеми', 'необходимыми',
'правилами']
# same text but some letters changed into latin ones at random
broken_pre_stem = ['дaлеко', 'далеко', 'словeсными', 'горaми',
'стране', 'гласных', 'соглaсных', 'живут', 'рыбные',
'тексты', 'вдали', 'всех', 'живyт', 'они',
'бyквенных', 'домах', 'берегу', 'семантика',
'большого', 'языкового', 'oкeана', 'маленький',
'ручеек', 'даль', 'журчит', 'всей', 'стране',
'обеспечивает', 'всеми', 'необходимыми',
'правилами']
self.assertEqual(cleanText(upright_pre_stem)[0],
cleanText(broken_pre_stem)[0])
def routine(path):
filename = re.findall(r'\w+\.txt$', path)[0]
text = getText(path)
raw_text = preprod(text)
clean_text, is_broken = cleanText(raw_text)
nausea = nauseaRate(clean_text)
return filename, nausea, is_broken
if __name__ == '__main__':
indexes = [str(index)[-4:] for index in range(10001, 10106)]
paths = ['text_files/' + index + '.txt' for index in indexes]
p = Pool()
results = p.map(routine, paths)
p.close()
p.join()
sqlSubmit(results)