razdel

Splits russian text into tokens, sentences, section. Rule-based


Keywords
nlp, natural, language, processing, russian, token, sentence, tokenize, python, sentence-boundary-detection, sentence-segmentation, tokenization
License
MIT
Install
pip install razdel==0.5.0

Documentation

CI codecov

razdel — rule-based system for Russian sentence and word tokenization..

Usage

>>> from razdel import tokenize

>>> tokens = list(tokenize('Кружка-термос на 0.5л (50/64 см³, 516;...)'))
>>> tokens
[Substring(0, 13, 'Кружка-термос'),
 Substring(14, 16, 'на'),
 Substring(17, 20, '0.5'),
 Substring(20, 21, 'л'),
 Substring(22, 23, '(')
 ...]
 
>>> [_.text for _ in tokens]
['Кружка-термос', 'на', '0.5', 'л', '(', '50/64', 'см³', ',', '516', ';', '...', ')']
>>> from razdel import sentenize

>>> text = '''
... - "Так в чем же дело?" - "Не ра-ду-ют".
... И т. д. и т. п. В общем, вся газета
... '''

>>> list(sentenize(text))
[Substring(1, 23, '- "Так в чем же дело?"'),
 Substring(24, 40, '- "Не ра-ду-ют".'),
 Substring(41, 56, 'И т. д. и т. п.'),
 Substring(57, 76, 'В общем, вся газета')]

Installation

razdel supports Python 3.5+ and PyPy 3.

$ pip install razdel

Quality, performance

Unfortunately, there is no single correct way to split text into sentences and tokens. For example, one may split «Как же так?! Захар...» — воскликнут Пронин. into three sentences ["«Как же так?!", "Захар...»", "— воскликнут Пронин."] while razdel splits it into two ["«Как же так?!", "Захар...» — воскликнут Пронин."]. What would be the correct way to tokenizer т.е.? One may split in into т.|е., razdel splits into т|.|е|..

razdel tries to mimic segmentation of these 4 datasets : SynTagRus, OpenCorpora, GICRYA and RNC. These datasets mainly consist of news and fiction. razdel rules are optimized for these kinds of texts. Library may perform worse on other domains like social media, scientific articles, legal documents.

We measure absolute number of errors. There are a lot of trivial cases in the tokenization task. For example, text чуть-чуть?! is not non-trivial, one may split it into чуть|-|чуть|?|! while the correct tokenization is чуть-чуть|?!, such examples are rare. Vast majority of cases are trivial, for example text в 5 часов ... is correctly tokenized even via Python native str.split into в| |5| |часов| |.... Due to the large number of trivial case overall quality of all segmenators is high, it is hard to compare differentiate between for examlpe 99.33%, 99.95% and 99.88%, so we report the absolute number of errors.

errors — number of errors. For example, consider etalon segmentation is что-то|?, prediction is что|-|то?, then the number of errors is 3: 1 for missing split то? + 2 for extra splits что|-|то.

time — total seconds taken.

spacy_tokenize, aatimofeev and others a defined in naeval/segment/models.py. Tables are computed in segment/main.ipynb.

Tokens

corpora syntag gicrya rnc
errors time errors time errors time errors time
re.findall(\w+|\d+|\p+) 4217 0.5 2914 0.5 2402 0.3 8630 0.3
spacy 3283 5.6 2639 5.5 1742 3.8 4010 3.5
nltk.word_tokenize 5712 3.7 67523 3.9 12149 2.7 13564 2.8
mystem 4280 4.9 3624 4.6 2515 3.6 1812 3.5
mosestokenizer 1188 2.0 1641 2.1 1696 1.7 2486 1.7
segtok.word_tokenize 1491 2.4 1552 2.4 1657 1.8 1238 1.8
aatimofeev/spacy_russian_tokenizer 1485 56.2 1225 53.3 630 39.2 2972 47.6
koziev/rutokenizer 2744 1.1 1632 1.1 2576 0.9 9915 0.9
razdel.tokenize 1158 2.9 1861 3.0 315 2.0 2264 2.1

Sentencies

corpora syntag gicrya rnc
errors time errors time errors time errors time
re.split([.?!…]) 19974 0.7 5986 0.4 9380 0.5 22483 0.8
segtok.split_single 19450 16.5 4140 10.3 158672 1.5 172887 3.1
mosestokenizer 60212 10.6 39361 5.4 12238 5.7 168743 385.1
nltk.sent_tokenize 16346 8.8 4194 4.3 6774 4.2 32391 5.4
deeppavlov/rusenttokenize 10138 9.9 1180 6.0 8402 5.6 20717 93.4
razdel.sentenize 9408 5.4 798 3.4 11020 3.6 10791 5.4

Support

Development

Test:

pip install -e .
pip install -r requirements/ci.txt
make test
make int  # 2000 integration tests

Package:

make version
git push
git push --tags

make clean wheel upload

mystem errors on syntag:

# see naeval/data
cat syntag_tokens.txt | razdel-ctl sample 1000 | razdel-ctl gen | razdel-ctl diff --show moses_tokenize | less

Non-trivial token tests:

pv data/*_tokens.txt | razdel-ctl gen --recall | razdel-ctl diff space_tokenize > tests.txt
pv data/*_tokens.txt | razdel-ctl gen --precision | razdel-ctl diff re_tokenize >> tests.txt

Update integration tests:

cd razdel/tests/data/
pv sents.txt | razdel-ctl up sentenize > t; mv t sents.txt

razdel and moses diff:

cat data/*_tokens.txt | razdel-ctl sample 1000 | razdel-ctl gen | razdel-ctl up tokenize | razdel-ctl diff moses_tokenize | less

razdel performance:

cat data/*_tokens.txt | razdel-ctl sample 10000 | pv -l | razdel-ctl gen | razdel-ctl diff tokenize | wc -l