def all_ents(v):
return [(ent.text, ent.label_) for ent in ner_model(v).ents]
df1['Entities'] = df1['text'].apply(lambda v: all_ents(v))
df1.head()
当执行这个显示ner_model没有定义时,我能知道如何在spacy 中定义ner模型吗
有东西告诉我您没有正确加载spaCy模型。不知道df1
是什么样子的,我决定用我自己的一个,如下所示:
import spacy
import pandas as pd
# Building my own `df1`, it should look similar to yours
texts = [
"Net income was $9.4 million compared to the prior year of $2.7 million.",
"Revenue exceeded twelve billion dollars, with a loss of $1b.",
"I don't have any entity in me"
]
df1 = pd.DataFrame(texts, columns =['text'])
# Loading spaCy model
model_to_use = "en_core_web_lg" # Or use the path to your own model
ner_model = spacy.load(model_to_use)
# Your code works now
def all_ents(v):
return [(ent.text, ent.label_) for ent in ner_model(v).ents]
df1['Entities'] = df1['text'].apply(lambda v: all_ents(v))
注意:
根据我自己的经验,如果df1
相当大(即,它包含数千个句子(,您可能需要将df1["text"]
转换为列表或生成器,然后应用这些提示。如果您的情况不是这样,或者您对速度最优的代码不感兴趣,那么不要注意这一点,继续您当前的实现。