Use Spacy NER to identify person and make person one word?

Viewed 65

I want to use Spacy NER to identify the PERSON and make it one word.

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
1 Answers

If you use Spacy, you code should be:

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
Related