首页 > 解决方案 > NLTK 的额外缩写列表?

问题描述

我正在尝试使用 Python + NLTK 对大量文章进行一些繁重的句子标记化。不幸的是,它对待“等”。作为句子的结尾而不是缩写。我怀疑它对其他缩写也会做同样的事情,比如“eg”或“ie”

我知道我可以添加如下内容:

sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
extra_abbreviations = ['et al']
sentence_tokenizer._params.abbrev_types.update(extra_abbreviations)

但我不想手动确定数据集中可能遇到的所有可能的额外缩写并手动输入它们。有没有人为这个特定问题想出一个更通用的解决方案,即使它只是一长串可以从文件加载并作为参数传递给更新方法的缩写?

标签: pythonnltk

解决方案


SpaCy 的句子标记器将为您解决这个问题。

import spacy
nlp = spacy.load('en_core_web_lg')
text = 'Exercise (e.g. riding a bike or taking a hike) will help you live longer. This is a second sentence'
doc = nlp(text)
sentences = [sent.string.strip() for sent in doc.sents]
print(sentences)

输出:['锻炼(例如骑自行车或远足)会帮助你活得更久。','这是第二句话']


推荐阅读