你用的是什么版本?有了lower
它对我来说可以正常工作:
>>> doc = nlp(u'Algorithms; Deterministic algorithms; Adaptive algorithms; Something...'.lower())
>>> for word in doc:
... print(word.text, word.lemma_, word.tag_)
...
(u'algorithms', u'algorithm', u'NNS')
(u';', u';', u':')
(u'deterministic', u'deterministic', u'JJ')
(u'algorithms', u'algorithm', u'NNS')
(u';', u';', u':')
(u'adaptive', u'adaptive', u'JJ')
(u'algorithms', u'algorithm', u'NN')
(u';', u';', u':')
(u'something', u'something', u'NN')
(u'...', u'...', u'.')
如果没有lower
,标注器会分配Algorithms
标签 NNP,即专有名词。这可以防止词形还原,因为模型在统计上已经猜测该词是专有名词。
如果你愿意,你可以在分词器中设置一个特殊情况规则来告诉 spaCy 这Algorithms
绝不是一个专有名词。
from spacy.attrs import POS, LEMMA, ORTH, TAG
nlp = spacy.load('en')
nlp.tokenizer.add_special_case(u'Algorithms', [{ORTH: u'Algorithms', LEMMA: u'algorithm', TAG: u'NNS', POS: u'NOUN'}])
doc = nlp(u'Algorithms; Deterministic algorithms; Adaptive algorithms; Something...')
for word in doc:
print(word.text, word.lemma_, word.tag_)
(u'Algorithms', u'algorithm', u'NNS')
(u';', u';', u':')
(u'Deterministic', u'deterministic', u'JJ')
(u'algorithms', u'algorithm', u'NN')
(u';', u';', u':')
(u'Adaptive', u'adaptive', u'JJ')
(u'algorithms', u'algorithm', u'NNS')
(u';', u';', u':')
(u'Something', u'something', u'NN')
(u'...', u'...', u'.')
该tokenizer.add_special_case
函数允许您指定如何对字符串进行标记,并在每个子标记上设置属性。