微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

相反,它打破如下:

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

И.

Манташева.

https://github.com/mhq/train_punkt开始使用russian.pickle时也是如此,
这是一般的NLTK限制还是语言特定的?

解决方法:

正如一些评论暗示的那样,你想要使用的是Punkt句子分段器/标记器.

NLTK或语言特定?

都不是.正如您所意识到的,您不能简单地在每个时期分开. NLTK附带了几个使用不同语言培训的Punkt分段器.但是,如果您遇到问题,最好的办法是使用更大的培训语料库来为Punkt tokenizer学习.

文档链接

> https://nltk.googlecode.com/svn/trunk/doc/howto/tokenize.html
> https://nltk.googlecode.com/svn/trunk/doc/api/nltk.tokenize.punkt.PunktSentenceTokenizer-class.html

样本实施

以下是指向正确方向的代码的一部分.您应该能够通过提供俄语文本文件为自己做同样的事情.其中一个来源可能是Wikipedia database dumpRussian version,但我认为这是潜在的次要问题.

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐