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-multi")
6
7# make example sentence in any of the four languages
8sentence = Sentence("George Washington ging nach 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.9977)]
Span [5]: "Washington" [− Labels: LOC (0.9895)]1from flair.data import Corpus
2from flair.datasets import CONLL_03, CONLL_03_GERMAN, CONLL_03_DUTCH, CONLL_03_SPANISH
3from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
4
5# 1. get the multi-language corpus
6corpus: Corpus = MultiCorpus([
7 CONLL_03(), # English corpus
8 CONLL_03_GERMAN(), # German corpus
9 CONLL_03_DUTCH(), # Dutch corpus
10 CONLL_03_SPANISH(), # Spanish corpus
11 ])
12
13# 2. what tag do we want to predict?
14tag_type = 'ner'
15
16# 3. make the tag dictionary from the corpus
17tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
18
19# 4. initialize each embedding we use
20embedding_types = [
21
22 # GloVe embeddings
23 WordEmbeddings('glove'),
24
25 # FastText embeddings
26 WordEmbeddings('de'),
27
28 # contextual string embeddings, forward
29 FlairEmbeddings('multi-forward'),
30
31 # contextual string embeddings, backward
32 FlairEmbeddings('multi-backward'),
33]
34
35# embedding stack consists of Flair and GloVe embeddings
36embeddings = StackedEmbeddings(embeddings=embedding_types)
37
38# 5. initialize sequence tagger
39from flair.models import SequenceTagger
40
41tagger = SequenceTagger(hidden_size=256,
42 embeddings=embeddings,
43 tag_dictionary=tag_dictionary,
44 tag_type=tag_type)
45
46# 6. initialize trainer
47from flair.trainers import ModelTrainer
48
49trainer = ModelTrainer(tagger, corpus)
50
51# 7. run training
52trainer.train('resources/taggers/ner-multi',
53 train_with_dev=True,
54 max_epochs=150)@misc{akbik2019multilingual,
title={Multilingual sequence labeling with one model},
author={Akbik, Alan and Bergmann, Tanja and Vollgraf, Roland}
booktitle = {{NLDL} 2019, Northern Lights Deep Learning Workshop},
year = {2019}
}@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}
}