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