简体   繁体   English

使用 Spacy NER 识别人并使人成为一个词?

[英]Use Spacy NER to identify person and make person one word?

I want to use Spacy NER to identify the PERSON and make it one word.我想使用 Spacy NER 来识别 PERSON 并使其成为一个单词。

My dataset looks like this:我的数据集如下所示:

text     
use your superpowers
vote for Barack Obama
vote for Marine Le Pen
play with Michael Jordan
support the supporters

I want my final output to look like this:我希望我的最终输出如下所示:

text     
use your superpowers
vote for Barack_Obama
vote for Marine_Le_Pen
play with Michael_Jordan
support the supporters

This is the code I have so far:这是我到目前为止的代码:

 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)

When I use the code above, I'm getting an error message.当我使用上面的代码时,我收到一条错误消息。

AttributeError: name 'token' is not defined

If you use Spacy , you code should be:如果你使用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)

Output:输出:

>>> 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 supporters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM