Reputation: 153
I'm trying to output my spaCy NER results to a csv file rather than a plain text file.
So far I have this code to try and achieve that:
def spacy_ner():
with open("spacy_results.txt", "w") as f:
cf = csv.DictWriter(f, ['Character', 'begin', 'end'\
, 'Label'], extrasaction='ignore')
cf.writeheader()
nlp = spacy.load('en_core_web_md')
doc = nlp(text)
for ent in doc.ents:
if ent.label_ == 'PERSON':
cf.writerow(ent.text, ent.start_char, ent.end_char, ent.label_)
print("Processing done")
I'm not sure how to create the csv file, while trying to display all the attributes mentioned above: ent.text, ent.start_char, ent.end_char, ent.label_
Running the code above calls a TypeError: writerow() takes 2 positional arguments but 5 were given
It works fine if I write the result to a .txt file, but it would be nice if it was a more structred and easy to access csv file.
What am I not seeing here that I need to change? Any help would be great!
Upvotes: 1
Views: 3084
Reputation: 23773
A csv.DictWriter requires a dictionary for writing. If your ent
object doesn't have a to_dict()
method, you will have to make one.
for ent in doc.ents:
if ent.label_ == 'PERSON':
d = {'Character':ent.text, 'begin':ent.start_char,
'end':ent.end_char,'Label':ent.label_}
cf.writerow(d)
Upvotes: 1