Skip to content

Commit d13f8ea

Browse files
authored
Merge pull request #3130 from flairNLP/models_and_tutorials
Documentation updates for Flair release 0.12
2 parents cb6b0e5 + 6121ca6 commit d13f8ea

30 files changed

+2505
-1695
lines changed

README.md

+58-28
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ A very simple framework for **state-of-the-art NLP**. Developed by [Humboldt Uni
1212
Flair is:
1313

1414
* **A powerful NLP library.** Flair allows you to apply our state-of-the-art natural language processing (NLP)
15-
models to your text, such as named entity recognition (NER), part-of-speech tagging (PoS),
15+
models to your text, such as named entity recognition (NER), sentiment analysis, part-of-speech tagging (PoS),
1616
special support for [biomedical data](/resources/docs/HUNFLAIR.md),
1717
sense disambiguation and classification, with support for a rapidly growing number of languages.
1818

1919
* **A text embedding library.** Flair has simple interfaces that allow you to use and combine different word and
20-
document embeddings, including our proposed **[Flair embeddings](https://www.aclweb.org/anthology/C18-1139/)**, BERT embeddings and ELMo embeddings.
20+
document embeddings, including our proposed [Flair embeddings](https://www.aclweb.org/anthology/C18-1139/) and various transformers.
2121

2222
* **A PyTorch NLP framework.** Our framework builds directly on [PyTorch](https://pytorch.org/), making it easy to
2323
train your own models and experiment with new approaches using Flair embeddings and classes.
2424

25-
Now at [version 0.11](https://github.com/flairNLP/flair/releases)!
25+
Now at [version 0.12](https://github.com/flairNLP/flair/releases)!
2626

2727

2828
## State-of-the-Art Models
@@ -37,7 +37,7 @@ Flair ships with state-of-the-art models for a range of NLP tasks. For instance,
3737
| Dutch | Conll-03 (4-class) | **95.25** | *93.7 [(Yu et al., 2020)](https://www.aclweb.org/anthology/2020.acl-main.577.pdf)* | [Flair Dutch 4-class NER demo](https://huggingface.co/flair/ner-dutch-large) |
3838
| Spanish | Conll-03 (4-class) | **90.54** | *90.3 [(Yu et al., 2020)](https://www.aclweb.org/anthology/2020.acl-main.577.pdf)* | [Flair Spanish 4-class NER demo](https://huggingface.co/flair/ner-spanish-large) |
3939

40-
**New:** Most Flair sequence tagging models (named entity recognition, part-of-speech tagging etc.) are now hosted
40+
Many Flair sequence tagging models (named entity recognition, part-of-speech tagging etc.) are also hosted
4141
on the [__🤗 HuggingFace model hub__](https://huggingface.co/models?library=flair&sort=downloads)! You can browse models, check detailed information on how they were trained, and even try each model out online!
4242

4343

@@ -53,61 +53,91 @@ pip install flair
5353

5454
Flair requires Python 3.7+.
5555

56-
### Example Usage
56+
### Example 1: Tag Entities in Text
5757

58-
Let's run named entity recognition (NER) over an example sentence. All you need to do is make a `Sentence`, load
58+
Let's run **named entity recognition** (NER) over an example sentence. All you need to do is make a `Sentence`, load
5959
a pre-trained model and use it to predict tags for the sentence:
6060

6161
```python
6262
from flair.data import Sentence
63-
from flair.models import SequenceTagger
63+
from flair.nn import Classifier
6464

6565
# make a sentence
6666
sentence = Sentence('I love Berlin .')
6767

6868
# load the NER tagger
69-
tagger = SequenceTagger.load('ner')
69+
tagger = Classifier.load('ner')
7070

7171
# run NER over sentence
7272
tagger.predict(sentence)
73+
74+
# print the sentence with all annotations
75+
print(sentence)
76+
```
77+
78+
This should print:
79+
80+
```console
81+
Sentence: "I love Berlin ." → ["Berlin"/LOC]
7382
```
7483

75-
Done! The `Sentence` now has entity annotations. Print the sentence to see what the tagger found.
84+
This means that "Berlin" was tagged as a **location entity** in this sentence.
85+
86+
* *to learn more about NER tagging in Flair, check out our [NER tutorial](/resources/docs/TUTORIAL_TAGGING_NER.md)!*
87+
88+
89+
### Example 2: Detect Sentiment
90+
91+
Let's run **sentiment analysis** over an example sentence to determine whether it is POSITIVE or NEGATIVE.
92+
Same code as above, just a different model:
7693

7794
```python
78-
# print the sentence with all annotations
79-
print(sentence)
95+
from flair.data import Sentence
96+
from flair.nn import Classifier
97+
98+
# make a sentence
99+
sentence = Sentence('I love Berlin .')
100+
101+
# load the NER tagger
102+
tagger = Classifier.load('sentiment')
80103

81-
print('The following NER tags are found:')
104+
# run NER over sentence
105+
tagger.predict(sentence)
82106

83-
# iterate over entities and print each
84-
for entity in sentence.get_spans('ner'):
85-
print(entity)
107+
# print the sentence with all annotations
108+
print(sentence)
86109
```
87110

88111
This should print:
89112

90113
```console
91-
Sentence: "I love Berlin ." → ["Berlin"/LOC]
114+
Sentence[4]: "I love Berlin ." → POSITIVE (0.9983)
115+
```
92116

93-
The following NER tags are found:
117+
This means that the sentence "I love Berlin" was tagged as having **POSITIVE** sentiment.
94118

95-
Span[2:3]: "Berlin" → LOC (0.999)
96-
```
119+
* *to learn more about sentiment analysis in Flair, check out our [sentiment analysis tutorial](/resources/docs/TUTORIAL_TAGGING_SENTIMENT.md)!*
97120

98121
## Tutorials
99122

100123
We provide a set of **quick tutorials** to get you started with the library:
101124

102-
* [Tutorial 1: Basics](/resources/docs/TUTORIAL_1_BASICS.md)
103-
* [Tutorial 2: Tagging your Text](/resources/docs/TUTORIAL_2_TAGGING.md)
104-
* [Tutorial 3: Embedding Words](/resources/docs/TUTORIAL_3_WORD_EMBEDDING.md)
105-
* [Tutorial 4: List of All Word Embeddings](/resources/docs/TUTORIAL_4_ELMO_BERT_FLAIR_EMBEDDING.md)
106-
* [Tutorial 5: Embedding Documents](/resources/docs/TUTORIAL_5_DOCUMENT_EMBEDDINGS.md)
107-
* [Tutorial 6: Loading a Dataset](/resources/docs/TUTORIAL_6_CORPUS.md)
108-
* [Tutorial 7: Training a Model](/resources/docs/TUTORIAL_7_TRAINING_A_MODEL.md)
109-
* [Tutorial 8: Training your own Flair Embeddings](/resources/docs/TUTORIAL_9_TRAINING_LM_EMBEDDINGS.md)
110-
* [Tutorial 9: Training a Zero Shot Text Classifier (TARS)](/resources/docs/TUTORIAL_10_TRAINING_ZERO_SHOT_MODEL.md)
125+
1. [**Tutorial 1: Basics**](/resources/docs/TUTORIAL_FLAIR_BASICS.md)
126+
2. [**Tutorial 2: Tagging your Text**](/resources/docs/TUTORIAL_TAGGING_OVERVIEW.md)
127+
* ... how to **tag entities** in your text → [*2.1*](/resources/docs/TUTORIAL_TAGGING_NER.md)
128+
* ... how to use **sentiment analysis**[*2.2*](/resources/docs/TUTORIAL_TAGGING_SENTIMENT.md)
129+
* ... how to use **entity linking**[*2.3*](/resources/docs/TUTORIAL_TAGGING_LINKING.md)
130+
* ... how to use **part-of-speech tagging**[*2.4*](/resources/docs/TUTORIAL_TAGGING_POS.md)
131+
* ... how to use **relation extraction**[*2.5*](/resources/docs/TUTORIAL_TAGGING_RELATIONS.md)
132+
* ... and more → [*full tutorial*](/resources/docs/TUTORIAL_TAGGING_OVERVIEW.md)
133+
3. [**Tutorial 3: Using Embeddings**](/resources/docs/TUTORIAL_EMBEDDINGS_OVERVIEW.md)
134+
4. [**Tutorial 4: Training a Model**](/resources/docs/TUTORIAL_TRAINING_OVERVIEW.md)
135+
* ... how **model training generally works in Flair**[4.1](/resources/docs/TUTORIAL_TRAINING_MODELS.md)
136+
* ... how to **load a prepared dataset**[4.2](/resources/docs/TUTORIAL_CORPUS_PREPARED.md)
137+
* ... how to **load your own dataset**[4.3](/resources/docs/TUTORIAL_CORPUS_CUSTOM.md)
138+
* ... how to **train a sequence labeling model** (NER/PoS) → [4.4](/resources/docs/TUTORIAL_TRAINING_SEQUENCE_LABELER.md)
139+
* ... how to **train a text classifier** (sentiment analysis, etc.) → [4.5](/resources/docs/TUTORIAL_TRAINING_TEXT_CLASSIFIER.md)
140+
* ... and more → [*full tutorial*](/resources/docs/TUTORIAL_TRAINING_OVERVIEW.md)
111141

112142
The tutorials explain how the base NLP classes work, how you can load pre-trained models to tag your
113143
text, how you can embed your text with different word or document embeddings, and how you can train your own

flair/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
device = torch.device("cpu")
1818

1919
# global variable: version
20-
__version__ = "0.11.3"
20+
__version__ = "0.12"
2121

2222
# global variable: arrow symbol
2323
_arrow = " → "

flair/models/multitask_model.py

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ def _fetch_model(model_name) -> str:
258258
model_map["hunflair"] = "/".join([hu_path, "bioner", "hunflair.pt"])
259259
model_map["hunflair-paper"] = "/".join([hu_path, "bioner", "hunflair-paper.pt"])
260260

261+
# entity linker
262+
model_map["linker"] = "/".join([hu_path, "zelda", "v1", "zelda-v1.pt"])
263+
model_map["zelda"] = "/".join([hu_path, "zelda", "v1", "zelda-v1.pt"])
264+
261265
cache_dir = Path("models")
262266
if model_name in model_map:
263267
model_name = cached_path(model_map[model_name], cache_dir=cache_dir)

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
python-dateutil>=2.6.1
22
torch>=1.5.0,!=1.8
3-
gensim>=3.4.0
3+
gensim>=3.8.0
44
tqdm>=4.26.0
55
segtok>=1.5.7
66
matplotlib>=2.2.3

0 commit comments

Comments
 (0)