[Bug]: How to change entities in the predict function
Describe the bug
I need to detect only PERSON AND EMAIL instead of all the 2o entities.how to change this?
Loading model from flair/ner-english-large
2025-06-16 22:55:27,369 SequenceTagger predicts: Dictionary with 20 tags:
To Reproduce
from flair.data import Sentence
from flair.nn import Classifier
# make a sentence
sentence = Sentence('George Washington went to Washington.')
# load the NER tagger
tagger = Classifier.load('ner-large')
# run NER over sentence
tagger.predict(sentence)
# print the sentence with all annotations
print(sentence)
Expected behavior
NA
Logs and Stack traces
Screenshots
No response
Additional Context
No response
Environment
NA
@helpmefindaname
This isn't a bug. Pre-trained NER models like ner-english-large are designed to detect all entity types they were trained on (in this case, 20 types). You can't modify what entities the model predicts at runtime.
Solution: Filter the results after prediction to keep only PERSON entities:
from flair.data import Sentence
from flair.nn import Classifier
sentence = Sentence('George Washington went to Washington.')
tagger = Classifier.load('ner-large')
tagger.predict(sentence)
# Filter for only PERSON entities
person_entities = [entity for entity in sentence.get_spans('ner')
if 'PER' in entity.tag]
print(person_entities)
For EMAIL detection, you'll need a different approach since standard NER models don't typically detect email addresses - consider using regex or training a custom model.