-1

I would like to improve this following if statement

#Extract ORG, GPE, LOC and FAC labels from phrases
for entity in doc.ents:
    if entity.label_ == "ORG" or entity.label_ == "GPE" or entity.label_ == "LOC" or entity.label_ == "FAC":
        print(entity.text, entity.label_)

Is it possible reduce the number of "entity.label_" variables to one?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Drethax
  • 75
  • 1
  • 8

2 Answers2

1

You can try to check if entity.label_ var in a tuple of all the words.

for entity in doc.ents:
    if entity.label_ in ("ORG", "GPE", "LOC", "FAC"):
        print(entity.text, entity.label_)
Leo Arad
  • 4,452
  • 2
  • 6
  • 17
0
for entity in doc.ents:
    if any(entity.label_ == x for x in ["ORG", "GPE", "LOC", "FAC"]):
        print(entity.text, entity.label_)