This tutorials shows you how to do named entity recognition, showcases various NER models, and provides a full list of all NER models in Flair.
Tagging entities with our standard modelOur standard model uses Flair embeddings and was trained over the English CoNLL-03 task and can recognize 4 different entity types. It offers a good tradeoff between accuracy and speed.
As example, let's use the sentence "George Washington went to Washington.":
from flair.nn import Classifier
from flair.data import Sentence
tagger = Classifier.load('ner')
sentence = Sentence('George Washington went to Washington.')
tagger.predict(sentence)
print(sentence)
This should print:
Sentence: "George Washington went to Washington ." → ["George Washington"/PER, "Washington"/LOC]
The printout tells us that two entities are labeled in this sentence: "George Washington" as PER (person) and "Washington" as LOC (location).
Tagging entities with our best modelOur best 4-class model is trained using a very large transformer. Use it if accuracy is the most important to you, and speed/memory not so much.
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)
print(sentence)
As you can see, it's the same code, just with 'ner-large' as model instead of 'ner'. This model also works with most languages.
:::hint If you want the fastest model we ship, you can also try 'ner-fast'. :::
Tagging entities in non-English textWe also have NER models for text in other languages.
Tagging a German sentenceTo tag a German sentence, just load the appropriate model:
tagger = Classifier.load('de-ner-large')
sentence = Sentence('George Washington ging nach Washington.')
tagger.predict(sentence)
print(sentence)
This should print:
Sentence: "George Washington ging nach Washington ." → ["George Washington"/PER, "Washington"/LOC]
Tagging an Arabic sentence
Flair also works for languages that write from right to left. To tag an Arabic sentence, just load the appropriate model:
tagger = Classifier.load('ar-ner')
sentence = Sentence("احب برلين")
tagger.predict(sentence)
print(sentence)
This should print:
Sentence[2]: "احب برلين" → ["برلين"/LOC]
Tagging Entities with 18 Classes
We also ship models that distinguish between more than just 4 classes. For instance, use our ontonotes models to classify 18 different types of entities.
from flair.data import Sentence
from flair.nn import Classifier
sentence = Sentence('On September 1st George won 1 dollar while watching Game of Thrones.')
tagger = Classifier.load('ner-ontonotes-large')
tagger.predict(sentence)
print(sentence)
This should print:
Sentence[13]: "On September 1st George won 1 dollar while watching Game of Thrones." → ["September 1st"/DATE, "George"/PERSON, "1 dollar"/MONEY, "Game of Thrones"/WORK_OF_ART]
Finding for instance that "Game of Thrones" is a work of art and that "September 1st" is a date.
Biomedical DataFor biomedical data, we offer the hunflair models that detect 5 different types of biomedical entities.
from flair.data import Sentence
from flair.nn import Classifier
sentence = Sentence('Behavioral abnormalities in the Fmr1 KO2 Mouse Model of Fragile X Syndrome.')
tagger = Classifier.load('bioner')
tagger.predict(sentence)
print(sentence)
This should print:
Sentence[13]: "Behavioral abnormalities in the Fmr1 KO2 Mouse Model of Fragile X Syndrome." → ["Behavioral abnormalities"/Disease, "Fmr1"/Gene, "Mouse"/Species, "Fragile X Syndrome"/Disease]
Thus finding entities of classes "Species", "Disease" and "Gene" in this text.
List of NER ModelsWe end this section with a list of all models we currently ship with Flair.
You choose which pre-trained model you load by passing the appropriate string to the load()
method of the Classifier
class.
A full list of our current and community-contributed models can be browsed on the model hub.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4