问题描述
我目前正在尝试将拼写检查步骤添加到 Spacy 的一个内置管道中,特别是 'en_core_web_sm'
我发现了一个非常简洁的组件,名为 Contextual Spell Check,我已将它插入到管道中。问题是词形还原步骤不是词形还原拼写检查过的单词,而是原始文本,即使在我将管道重新排序为 ['tok2vec','parser','contextual spellchecker','tagger','attribute_ruler','lemmatizer','ner']
之后也是如此。
例如:
doc_a = nlp("Income wes $9.4 milion compared to the prior year of $2.7 milion.")
doc_b = nlp("Income was $9.4 milion compared to the prior year of $2.7 milion.")
将返回正确拼写检查的结果:
print(doc_a._.outcome_spellCheck)
# Income was $9.4 million compared to the prior year of $2.7 million.
print(doc_b._.outcome_spellCheck)
# Income was $9.4 million compared to the prior year of $2.7 million.
但是,检查基本结果:
# doc_a with misspelled 'was'. Note lemma is still the original typo 'wes'
print(doc_a.to_json()['tokens'])
# {'id': 1,'start': 7,'end': 10,'tag': 'MD','pos': 'AUX','morph': 'VerbType=Mod','lemma': 'wes','dep': 'ROOT','head': 1}
# doc_b with correctly spelled 'was'. Correctly lemmatized to 'be'
print(doc_b.to_json()['tokens'])
# {'id': 1,'tag': 'VBD','morph': 'Mood=Ind|Number=Sing|Person=3|Tense=Past|VerbForm=Fin','lemma': 'be','head': 1}
如何确保词形还原发生在经过拼写检查的术语上?
解决方法
spaCy 旨在确保永远不会修改原始文本。不幸的是,这不是您想要的,但实际上没有办法解决它。
在这种情况下,我建议您使用仅拼写检查的管道,并使用拼写检查器输出生成一个新字符串,您可以将其提供给词形还原管道。
这样的东西...
spellchecker = ... minimal nlp with the spellchecker...
nlp = ... normal non-spellcheck pipeline ...
doc = nlp(spellchecker(text)._.outcome_spellCheck)
您还可以执行其他操作,例如加载词形还原器并创建一个组件以将拼写检查器结果直接提供给它,或者使用属性标尺将拼写检查器输出映射到您的标记,但单独的管道可能更容易设置因为它在部件之间具有最小的依赖性。