ravecoder
ravecoder

Reputation: 201

Spacy Named Entity Recognition Issue

I am trying to tag ORG's from a bunch of text I am parsing through

What I have so far is as follows:

import spacy
import en_core_web_sm
nlp = en_core_web_sm.load()

file = open("C:\\sample.txt")

doc = nlp(file.read())
print([(X.text, X.label_) for X in doc.ents])

Now, my result prints all possible tags, I just want it to print ORGs instead. Any suggestions on how to do that?

Upvotes: 1

Views: 164

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

X.label_ holds the name of the entity, so all you need is add a condition to only return those tuples where X.label_ equals ORG:

print([(X.text, X.label_) for X in doc.ents if X.label_ == "ORG"])
#                                           ^------------------^   

Upvotes: 2

Related Questions