Home > database >  How to only extract only organization names from text using spacy
How to only extract only organization names from text using spacy

Time:11-05

I want to extract only organization from a string or column.

I am using this code:

def ent(doc):
    for x in (nlp(doc)).ents:
        if x.label_ != "ORG": continue
        else:
            return (x.text)


d= ("Brock Group (American Industrial Partners) acquires Aegion's Energy Services Businesses")
ent(d)


However, this code only extract only one organization not all; like in this case only gives:

'Brock Group'

CodePudding user response:

Your code only extracts the first organization name because it returns when the condition is met. You can use list comprehension

def ent(doc):
    return [x for x in (nlp(doc)).ents if x.label_ == "ORG"]
  • Related