我想用Spacy NER来识别那个人,然后把它写成一个词。
我的数据集如下所示:
text
use your superpowers
vote for Barack Obama
vote for Marine Le Pen
play with Michael Jordan
support the supporters我希望我的最后输出如下所示:
text
use your superpowers
vote for Barack_Obama
vote for Marine_Le_Pen
play with Michael_Jordan
support the supporters这是我到目前为止掌握的代码:
def get_ner (string):
nlp = spacy.load("en_core_web_trf")
doc = nlp(string)
for token.text in doc:
if token.ents=="Person":
s= ent['start']
e= ent['end']
txt = txt[:s] + txt[s:e+1].replace(' ', '_') + txt[e:]
return txt
df['text']= df.text.apply(get_ner)当我使用上面的代码时,我会收到一条错误消息。
AttributeError: name 'token' is not defined发布于 2022-07-16 13:38:14
如果使用Spacy,您的代码应该是:
nlp = spacy.load('en_core_web_trf')
def get_ner(txt):
doc = nlp(txt)
for ent in doc.ents:
if ent.label_ == 'PERSON':
s = ent.start_char
e = ent.end_char
txt = txt[:s] + txt[s:e+1].replace(' ', '_') + txt[e:]
return txt
df['text'] = df['text'].apply(get_ner)输出:
>>> df
text
0 use your superpowers
1 vote for Barack_Obama
2 vote for Marine_Le_Pen
3 play with Michael_Jordan
4 support the supportershttps://stackoverflow.com/questions/72995392
复制相似问题