如何使 spaCy 不区分大小写

yac*_*yac 5 case-sensitive spacy ner

查找实体名称时如何使 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 "))    
Run Code Online (Sandbox Code Playgroud)

这使

(Nicholas Sparks,)  
()
Run Code Online (Sandbox Code Playgroud)

hap*_*set -1

这很容易。您只需question.lower()在函数中添加一个预处理步骤:

def analyseQuestion(question):

    # Preprocess question to make further analysis case-insensetive
    question = question.lower()

    doc = nlp(question)
    entity=doc.ents 

    return entity
Run Code Online (Sandbox Code Playgroud)

该解决方案的灵感来自Rasa NLU 库的这段代码。但是,对于非英语(非 ASCII)文本,它可能不起作用。对于这种情况你可以尝试:

question = question.decode('utf8').lower().encode('utf8')
Run Code Online (Sandbox Code Playgroud)

然而,spacy 中的 NER 模块在某种程度上取决于标记的情况,并且您可能会遇到一些差异,因为它是统计训练模型。请参阅此链接

  • 我不确定这是否回答了问题。我认为OP正在寻找一种方法来检测像“(Nicholas Sparks,)”这样的实例,即使句子(和潜在的实体)是小写的。 (9认同)
  • @LoganYang 是的 - 关键是“NER 是在小写数据上训练的”,这就是 OP 正在寻找的。这篇文章中尝试的解决方案与(非工作)第二个示例相同,OP已经尝试过并报告不起作用(其中未检测到小写的“nicholas Sparks”)。 (2认同)