Views
No views yet
| tag | meaning |
|---|---|
| PER | person name |
| LOC | location name |
| ORG | organization name |
| MISC | other name |
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("flair/ner-french")
6
7# make example sentence
8sentence = Sentence("George Washington est allé à Washington")
9
10# predict NER tags
11tagger.predict(sentence)
12
13# print sentence
14print(sentence)
15
16# print predicted NER spans
17print('The following NER tags are found:')
18# iterate over entities and print
19for entity in sentence.get_spans('ner'):
20 print(entity)
21Span [1,2]: "George Washington" [− Labels: PER (0.7394)]
Span [6]: "Washington" [− Labels: LOC (0.9161)]1from flair.data import Corpus
2from flair.datasets import NER_MULTI_WIKINER
3from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
4
5# 1. get the corpus
6corpus: Corpus = NER_MULTI_WIKINER(languages="fr")
7
8# 2. what tag do we want to predict?
9tag_type = 'ner'
10
11# 3. make the tag dictionary from the corpus
12tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
13
14# 4. initialize each embedding we use
15embedding_types = [
16
17 # GloVe embeddings
18 WordEmbeddings('fr'),
19
20 # contextual string embeddings, forward
21 FlairEmbeddings('fr-forward'),
22
23 # contextual string embeddings, backward
24 FlairEmbeddings('fr-backward'),
25]
26
27# embedding stack consists of Flair and GloVe embeddings
28embeddings = StackedEmbeddings(embeddings=embedding_types)
29
30# 5. initialize sequence tagger
31from flair.models import SequenceTagger
32
33tagger = SequenceTagger(hidden_size=256,
34 embeddings=embeddings,
35 tag_dictionary=tag_dictionary,
36 tag_type=tag_type)
37
38# 6. initialize trainer
39from flair.trainers import ModelTrainer
40
41trainer = ModelTrainer(tagger, corpus)
42
43# 7. run training
44trainer.train('resources/taggers/ner-french',
45 train_with_dev=True,
46 max_epochs=150)@inproceedings{akbik2018coling,
title={Contextual String Embeddings for Sequence Labeling},
author={Akbik, Alan and Blythe, Duncan and Vollgraf, Roland},
booktitle = {{COLING} 2018, 27th International Conference on Computational Linguistics},
pages = {1638--1649},
year = {2018}
}