128 lines
3.8 KiB
Markdown
128 lines
3.8 KiB
Markdown
# NER (Named Entity Recognition)
|
|
|
|
## Requirements
|
|
````shell
|
|
pip install flair
|
|
````
|
|
## Download Models
|
|
download models and place in data folder
|
|
https://drive.google.com/file/d/1mBW3zA8sd1zDo7KOiUCXmG64h8eJc_ip/view
|
|
|
|
## Getting started
|
|
|
|
````shell
|
|
python flair_ner_inference_.py
|
|
````
|
|
|
|
for train:
|
|
|
|
````shell
|
|
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](https://github.com/flairNLP/flair/blob/master/resources/docs/HUNFLAIR.md),
|
|
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](https://www.aclweb.org/anthology/C18-1139/) and various transformers.
|
|
|
|
* **A PyTorch NLP framework.** Our framework builds directly on [PyTorch](https://pytorch.org/), 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:
|
|
|
|
```python
|
|
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:
|
|
|
|
```console
|
|
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](https://flairnlp.github.io/docs/tutorial-basics/tagging-entities)!*
|
|
|
|
|
|
### 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:
|
|
|
|
```python
|
|
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:
|
|
|
|
```console
|
|
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](https://flairnlp.github.io/docs/tutorial-basics/tagging-sentiment)!*
|
|
|
|
## Tutorials
|
|
|
|
On our new :fire: [**Flair documentation page**](https://flairnlp.github.io/docs/intro) you will find many tutorials to get you started!
|
|
|
|
In particular:
|
|
- [Tutorial 1: Basic tagging](https://flairnlp.github.io/docs/category/tutorial-1-basic-tagging) → how to tag your text
|
|
- [Tutorial 2: Training models](https://flairnlp.github.io/docs/category/tutorial-2-training-models) → how to train your own state-of-the-art NLP models
|
|
- [Tutorial 3: Embeddings](https://flairnlp.github.io/docs/category/tutorial-3-embeddings) → how to produce embeddings for words and documents
|
|
|
|
There is also a dedicated landing page for our [biomedical NER and datasets](/resources/docs/HUNFLAIR.md) with
|
|
installation instructions and tutorials.
|
|
|