【发布时间】:2020-03-18 10:43:54
【问题描述】:
我正在使用nltk tokenize 库来拆分英文句子。
许多句子包含诸如e.g. 或eg. 之类的缩写,因此我用这些自定义缩写更新了标记器。
不过,我发现了一个奇怪的标记化行为:
import nltk
nltk.download("punkt")
sentence_tokenizer = nltk.data.load("tokenizers/punkt/english.pickle")
extra_abbreviations = ['e.g', 'eg']
sentence_tokenizer._params.abbrev_types.update(extra_abbreviations)
line = 'Required experience with client frameworks (e.g. React, Vue.js) and testing (e.g. Karma, Tape)'
for s in sentence_tokenizer.tokenize(line):
print(s)
# OUTPUT
# Required experience with client frameworks (e.g. React, Vue.js) and testing (e.g.
# Karma, Tape)
如您所见,分词器不会在第一个缩写(正确)上拆分,但在第二个缩写(不正确)上拆分。
奇怪的是,如果我将Karma 这个词改成其他任何东西,它就可以正常工作。
import nltk
nltk.download("punkt")
sentence_tokenizer = nltk.data.load("tokenizers/punkt/english.pickle")
extra_abbreviations = ['e.g', 'eg']
sentence_tokenizer._params.abbrev_types.update(extra_abbreviations)
line = 'Required experience with client frameworks (e.g. React, Vue.js) and testing (e.g. SomethingElse, Tape)'
for s in sentence_tokenizer.tokenize(line):
print(s)
# OUTPUT
# Required experience with client frameworks (e.g. React, Vue.js) and testing (e.g. SomethingElse, Tape)
任何线索为什么会发生这种情况?
【问题讨论】: