NLTK可以识别首字母后跟点吗?

use*_*840 5 python nlp nltk

我正在尝试使用NLTK来解析俄语文本,但它不适用于А等缩写和缩写.И. Манташева和Я. Вышинский.

相反,它打破如下:

организовывалзабастовкиидемонстрации,поднималрабочихнабакинскихпредприятияхА.

И.

Манташева.

它做了同样的当我用russian.picklehttps://github.com/mhq/train_punkt,
这是一个普遍NLTK限制或特定语言的?

Wes*_*ugh 4

正如一些评论所暗示的那样,您想要使用的是 Punkt 句子分段器/分词器。

NLTK 还是特定语言?

两者都不。正如您所意识到的,您不能简单地按每个时期进行划分。NLTK 附带了几个经过不同语言训练的 Punkt 分段器。但是,如果您遇到问题,最好的选择是使用更大的训练语料库供 Punkt 分词器进行学习。

文档链接

示例实施

下面是为您指明正确方向的部分代码。您应该能够通过提供俄语文本文件来为自己做同样的事情。其中一个来源可能是维基百科数据库转储俄语版本,但我将其作为潜在的次要问题留给您。

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)
Run Code Online (Sandbox Code Playgroud)