如何在查找实体名称时使 spaCy 不区分大小写?
是否有任何我应该添加的代码片段或其他内容,因为问题可能会提到非大写的实体?
def analyseQuestion(question):
doc = nlp(question)
entity=doc.ents
return entity
print(analyseQuestion("what is the best seller of Nicholas Sparks "))
print(analyseQuestion("what is the best seller of nicholas sparks "))
这给出了
(Nicholas Sparks,)
()
这很容易。您只需在函数中添加
question.lower()
的预处理步骤即可:
def analyseQuestion(question):
# Preprocess question to make further analysis case-insensetive
question = question.lower()
doc = nlp(question)
entity=doc.ents
return entity
解决方案的灵感来自Rasa NLU 库中的此代码。但是,对于非英语(非 ASCII)文本,它可能不起作用。对于这种情况你可以尝试:
question = question.decode('utf8').lower().encode('utf8')
然而,spacy 中的 NER 模块在某种程度上取决于标记的情况,并且您可能会面临一些差异,因为它是统计训练模型。请参阅此链接。