-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodels.py
272 lines (187 loc) · 5.71 KB
/
models.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import re
import logging
from time import time
from contextlib import contextmanager
from naeval.record import Record
from .substring import (
Substring,
find_substrings
)
def labeled(label):
def decorator(item):
item.label = label
return item
return decorator
class Timing(Record):
__attributes__ = ['model']
def __init__(self, model):
self.model = model
self.time = 0
def __call__(self, *args):
start = time()
result = list(self.model(*args))
self.time += (time() - start)
return result
def reset(self):
self.time = 0
#######
#
# SENTENIZE
#
#######
DOT = re.compile(r'([.?!…])\s+')
def re_split(text):
previous = 0
for match in DOT.finditer(text):
delimiter = match.group(1)
start = match.start()
yield text[previous:start] + delimiter
previous = match.end()
if previous < len(text):
yield text[previous:]
@labeled('re.split([.?!…])')
def re_sentenize(text):
chunks = re_split(text)
return find_substrings(chunks, text)
@contextmanager
def no_logger(logger):
logger.disabled = True
try:
yield
finally:
logger.disabled = False
LOGGER = logging.getLogger()
@labeled('deeppavlov/rusenttokenize')
def deeppavlov_sentenize(text):
from rusenttokenize import ru_sent_tokenize
with no_logger(LOGGER):
chunks = ru_sent_tokenize(text)
return find_substrings(chunks, text)
@labeled('nltk.sent_tokenize')
def nltk_sentenize(text):
from nltk import sent_tokenize
chunks = sent_tokenize(text, 'russian')
return find_substrings(chunks, text)
@labeled('segtok.split_single')
def segtok_sentenize(text):
# pip install segtok
from segtok.segmenter import split_single
chunks = split_single(text)
return find_substrings(chunks, text)
class MosesSentenizer:
label = 'mosestokenizer'
def __init__(self):
from mosestokenizer import MosesSentenceSplitter
self.splitter = MosesSentenceSplitter('ru')
def __call__(self, text):
if not text:
# prevent AssertionError: blank lines are not allowed
return []
chunks = self.splitter([text])
return find_substrings(chunks, text)
def adapt_razdel(records):
for record in records:
yield Substring(record.start, record.stop, record.text)
@labeled('razdel.sentenize')
def razdel_sentenize(text):
from razdel import sentenize
sents = sentenize(text)
return adapt_razdel(sents)
##########
#
# TOKENIZE
#
########
TOKEN = re.compile(r'([^\W\d]+|\d+|[^\w\s])')
@labeled(r're.findall(\w+|\d+|\p+)')
def re_tokenize(text):
chunks = TOKEN.findall(text)
return find_substrings(chunks, text)
@labeled('nltk.word_tokenize')
def nltk_tokenize(text):
from nltk.tokenize import word_tokenize
chunks = word_tokenize(text, 'russian')
return find_substrings(chunks, text)
class SpacyTokenizer:
label = 'spacy'
def __init__(self):
from spacy.lang.ru import Russian
self.nlp = Russian()
def __call__(self, text):
doc = self.nlp(text)
chunks = (token.text for token in doc)
return find_substrings(chunks, text)
class TimofeevTokenizer:
label = 'aatimofeev/spacy_russian_tokenizer'
def __init__(self):
from spacy.lang.ru import Russian
from spacy_russian_tokenizer import (
RussianTokenizer,
MERGE_PATTERNS,
SYNTAGRUS_RARE_CASES
)
self.nlp = Russian()
self.nlp.add_pipe(
RussianTokenizer(self.nlp, MERGE_PATTERNS + SYNTAGRUS_RARE_CASES),
name='russian_tokenizer'
)
def __call__(self, text):
doc = self.nlp(text)
chunks = (token.text for token in doc)
return find_substrings(chunks, text)
def parse_mystem(data):
# {'analysis': [], 'text': 'Mystem'},
# {'text': ' — '},
# {'analysis': [{'lex': 'консольный'}], 'text': 'консольная'},
# {'text': ' '},
# {'analysis': [{'lex': 'программа'}], 'text': 'программа'},
# {'text': '\n'}]
for item in data:
text = item['text'].strip()
if text:
yield text
class MystemTokenizer:
label = 'mystem'
def __init__(self):
from pymystem3 import Mystem
self.analyzer = Mystem(
grammar_info=False,
entire_input=True,
disambiguation=False,
weight=False
)
def __call__(self, text):
data = self.analyzer.analyze(text)
chunks = parse_mystem(data)
return find_substrings(chunks, text)
class MosesTokenizer:
label = 'mosestokenizer'
def __init__(self):
from mosestokenizer import MosesTokenizer
self.tokenizer = MosesTokenizer('ru')
# disable
self.tokenizer.argv.append('-no-escape') # " -> "
self.tokenizer.argv.remove('-a') # - -> @-@
self.tokenizer.restart()
def __call__(self, text):
chunks = self.tokenizer(text)
return find_substrings(chunks, text)
@labeled('segtok.word_tokenize')
def segtok_tokenize(text):
from segtok.tokenizer import word_tokenizer
chunks = word_tokenizer(text)
return find_substrings(chunks, text)
@labeled('razdel.tokenize')
def razdel_tokenize(text):
from razdel import tokenize
sents = tokenize(text)
return adapt_razdel(sents)
class KozievTokenizer:
label = 'koziev/rutokenizer'
def __init__(self):
import rutokenizer
self.tokenizer = rutokenizer.Tokenizer()
self.tokenizer.load()
def __call__(self, text):
chunks = self.tokenizer.tokenize(text)
return find_substrings(chunks, text)