正如一些评论所暗示的那样,您想要使用的是 Punkt 句子分段器/标记器。
NLTK 或特定语言?
两者都不。正如您所意识到的,您不能简单地在每个时期进行拆分。NLTK 带有几个受过不同语言培训的 Punkt 分段器。但是,如果您遇到问题,最好的办法是使用更大的训练语料库供 Punkt 分词器学习。
文档链接
示例实现
以下是为您指明正确方向的部分代码。通过提供俄语文本文件,您应该能够为自己做同样的事情。其中一个来源可能是俄语版本的Wikipedia 数据库转储,但我将其作为潜在的次要问题留给您。
import logging
try:
import cPickle as pickle
except ImportError:
import pickle
import nltk
def create_punkt_sent_detector(fnames, punkt_fname, progress_count=None):
"""Makes a pass through the corpus to train a Punkt sentence segmenter.
Args:
fname: List of filenames to be used for training.
punkt_fname: Filename to save the trained Punkt sentence segmenter.
progress_count: Display a progress count every integer number of pages.
"""
logger = logging.getLogger('create_punkt_sent_detector')
punkt = nltk.tokenize.punkt.PunktTrainer()
logger.info("Training punkt sentence detector")
doc_count = 0
try:
for fname in fnames:
with open(fname, mode='rb') as f:
punkt.train(f.read(), finalize=False, verbose=False)
doc_count += 1
if progress_count and doc_count % progress_count == 0:
logger.debug('Pages processed: %i', doc_count)
except KeyboardInterrupt:
print 'KeyboardInterrupt: Stopping the reading of the dump early!'
logger.info('Now finalzing Punkt training.')
punkt.finalize_training(verbose=True)
learned = punkt.get_params()
sbd = nltk.tokenize.punkt.PunktSentenceTokenizer(learned)
with open(punkt_fname, mode='wb') as f:
pickle.dump(sbd, f, protocol=pickle.HIGHEST_PROTOCOL)
return sbd
if __name__ == 'main':
punkt_fname = 'punkt_russian.pickle'
try:
with open(punkt_fname, mode='rb') as f:
sent_detector = pickle.load(f)
except (IOError, pickle.UnpicklingError):
sent_detector = None
if sent_detector is None:
corpora = ['russian-1.txt', 'russian-2.txt']
sent_detector = create_punkt_sent_detector(fnames=corpora,
punkt_fname=punkt_fname)
tokenized_text = sent_detector.tokenize("some russian text.",
realign_boundaries=True)
print '\n'.join(tokenized_text)