-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
  • 52,115
  • 13
  • 74
  • 94
Drethax
  • 45
  • 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,352
  • 2
  • 5
  • 17
0
for entity in doc.ents:
    if any(entity.label_ == x for x in ["ORG", "GPE", "LOC", "FAC"]):
        print(entity.text, entity.label_)