It is necessary to process the Russian-language text (s) of large volume. As far as I understood this issue, when working with the nltk library , all text must be translated into byte strings.

Tell me an approximate algorithm, provided that all the text is first stored in text files, after which it should be broken down into sentences and put into the database (keychain)?

  • one
    So, what is the question? Translate text into a byte array? - gil9red
  • At least nltk works successfully with non-byte strings too - andreymal
  • In general, they have a book describing work with the library and there are a lot of examples in it. As far as I can see, the text should be Unicode when working with it, but nothing about the binding of byte strings. - Alex Krass
  • The text file is about 7-8 MB each in UTF-8 encoding. Thanks for the book, I forgot about it! Plus it is necessary to break down the sentences - the solid magic of regular expressions. - JTProg
  • Explain why you need to break the text on the proposals. What do you want to get in the end? - MaxU

2 answers 2

Something like this:

In [59]: from nltk.tokenize import sent_tokenize In [60]: txt = open(r'C:\download\voyna_i_mir_tom_1.txt', encoding='utf-8').read() In [61]: sents = sent_tokenize(txt) In [62]: len(sents) Out[62]: 8033 

first ten sentences:

 In [63]: sents[:10] Out[63]: ['Том I\n\nЛев Николаевич Толстой\n\nВОЙНА И МИР\n\nТом 1\n\n\n\nЧАСТЬ ПЕРВАЯ\n\n\n\nI\n\n—\xa0Еh bien, mon prince.', 'Genes et Lucques ne sont plus que des apanages, des поместья, de la famille Buonaparte.', "Non, je vous previens, que si vous ne me dites pas, que nous avons la guerre, si vous vous permettez encore de pallier toute s les infamies, toutes les atrocites de cet Antichrist (ma parole, j'y crois) — je ne vous connais plus, vous n'etes plus mon ami, vous n'etes plus мой верный раб, comme vous dites.", '[Ну, что, князь, Генуа и Лукка стали не больше, как поместьями фамилии Бонапарте.', 'Нет, я вас предупреждаю, если вы мне не скажете, что у нас война, если вы еще позволите себе защищать все гадости, все ужасы этого Антихриста (право, я верю, что он Антихрист)\xa0— я вас больше не знаю, вы уж не друг мой, вы уж не мой верный раб, как вы говорите. ]', 'Ну, здравствуйте, здравствуйте.', 'Je vois que je vous fais peur, [Я вижу, что я вас пугаю, ] садитесь и рассказывайте.', 'Так говорила в июле 1805 года известная Анна Павловна Шерер, фрейлина и приближенная императрицы Марии Феодоровны, встречая важного и чиновного князя Василия, первого приехавшего на ее вечер.', 'Анна Павловна кашляла несколько дней, у нее был грипп, как она говорила (грипп был тогда новое слово, употреблявшееся только редкими).', "В записочках, разосланных утром с красным лакеем, было написано без различия во всех:\n\n«Si vous n'avez rien de mieux a fai re, M. le comte (или mon prince), et si la perspective de passer la soiree chez une pauvre malade ne vous effraye pas trop, je serai charmee de vous voir chez moi entre 7 et 10 heures."] 
  • And who lost the diacritical marks in the French part of the text? They were no longer in *.txt , ntlk failed to cope with them, the terminal failed with them, or the stackoverflow engine? - TZakrevskiy
  • @TZakrevskiy, I downloaded the file in CP1251 encoding - accordingly, they were no longer there and only then saved it in UTF-8 - MaxU

You can use re:

 import re result = re.split(r'\n', yourtext)