flair icon indicating copy to clipboard operation
flair copied to clipboard

[Bug]: How to change entities in the predict function

Open JINO-ROHIT opened this issue 6 months ago • 2 comments

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: , O, S-ORG, S-MISC, B-PER, E-PER, S-LOC, B-ORG, E-ORG, I-PER, S-PER, B-MISC, I-MISC, E-MISC, I-ORG, B-LOC, E-LOC, I-LOC, <START>, <STOP>

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

JINO-ROHIT avatar Jun 16 '25 17:06 JINO-ROHIT

@helpmefindaname

JINO-ROHIT avatar Jun 17 '25 06:06 JINO-ROHIT

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.

hsleonis avatar Aug 06 '25 15:08 hsleonis