3.8 KiB
NER (Named Entity Recognition)
Requirements
pip install flair
Download Models
download models and place in data folder https://drive.google.com/file/d/1mBW3zA8sd1zDo7KOiUCXmG64h8eJc_ip/view
Getting started
python flair_ner_inference_.py
for train:
python flair_ner_train.py
Documentation
Flair is:
-
A powerful NLP library. Flair allows you to apply our state-of-the-art natural language processing (NLP) models to your text, such as named entity recognition (NER), sentiment analysis, part-of-speech tagging (PoS), special support for biomedical data, sense disambiguation and classification, with support for a rapidly growing number of languages.
-
A text embedding library. Flair has simple interfaces that allow you to use and combine different word and document embeddings, including our proposed Flair embeddings and various transformers.
-
A PyTorch NLP framework. Our framework builds directly on PyTorch, making it easy to train your own models and experiment with new approaches using Flair embeddings and classes.
Quick Start Flair
Requirements and Installation
In your favorite virtual environment, simply do:
pip install flair
Flair requires Python 3.7+.
Example 1: Tag Entities in Text
Let's run named entity recognition (NER) over an example sentence. All you need to do is make a Sentence
, load
a pre-trained model and use it to predict tags for the sentence:
from flair.data import Sentence
from flair.nn import Classifier
# make a sentence
sentence = Sentence('I love Berlin .')
# load the NER tagger
tagger = Classifier.load('ner')
# run NER over sentence
tagger.predict(sentence)
# print the sentence with all annotations
print(sentence)
This should print:
Sentence: "I love Berlin ." → ["Berlin"/LOC]
This means that "Berlin" was tagged as a location entity in this sentence.
- to learn more about NER tagging in Flair, check out our NER tutorial!
Example 2: Detect Sentiment
Let's run sentiment analysis over an example sentence to determine whether it is POSITIVE or NEGATIVE. Same code as above, just a different model:
from flair.data import Sentence
from flair.nn import Classifier
# make a sentence
sentence = Sentence('I love Berlin .')
# load the NER tagger
tagger = Classifier.load('sentiment')
# run NER over sentence
tagger.predict(sentence)
# print the sentence with all annotations
print(sentence)
This should print:
Sentence[4]: "I love Berlin ." → POSITIVE (0.9983)
This means that the sentence "I love Berlin" was tagged as having POSITIVE sentiment.
- to learn more about sentiment analysis in Flair, check out our sentiment analysis tutorial!
Tutorials
On our new 🔥 Flair documentation page you will find many tutorials to get you started!
In particular:
- Tutorial 1: Basic tagging → how to tag your text
- Tutorial 2: Training models → how to train your own state-of-the-art NLP models
- Tutorial 3: Embeddings → how to produce embeddings for words and documents
There is also a dedicated landing page for our biomedical NER and datasets with installation instructions and tutorials.