Views
No views yet
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("flair/frame-english-fast")
6
7# make example sentence
8sentence = Sentence("George returned to Berlin to return his hat.")
9
10# predict NER tags
11tagger.predict(sentence)
12
13# print sentence
14print(sentence)
15
16# print predicted NER spans
17print('The following frame tags are found:')
18# iterate over entities and print
19for entity in sentence.get_spans('frame'):
20 print(entity)
21Span [2]: "returned" [− Labels: return.01 (0.9867)]
Span [6]: "return" [− Labels: return.02 (0.4741)]1from flair.data import Corpus
2from flair.datasets import ColumnCorpus
3from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
4
5# 1. load the corpus (Ontonotes does not ship with Flair, you need to download and reformat into a column format yourself)
6corpus = ColumnCorpus(
7 "resources/tasks/srl", column_format={1: "text", 11: "frame"}
8)
9
10# 2. what tag do we want to predict?
11tag_type = 'frame'
12
13# 3. make the tag dictionary from the corpus
14tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
15
16# 4. initialize each embedding we use
17embedding_types = [
18
19 BytePairEmbeddings("en"),
20
21 FlairEmbeddings("news-forward-fast"),
22
23 FlairEmbeddings("news-backward-fast"),
24]
25
26# embedding stack consists of Flair and GloVe embeddings
27embeddings = StackedEmbeddings(embeddings=embedding_types)
28
29# 5. initialize sequence tagger
30from flair.models import SequenceTagger
31
32tagger = SequenceTagger(hidden_size=256,
33 embeddings=embeddings,
34 tag_dictionary=tag_dictionary,
35 tag_type=tag_type)
36
37# 6. initialize trainer
38from flair.trainers import ModelTrainer
39
40trainer = ModelTrainer(tagger, corpus)
41
42# 7. run training
43trainer.train('resources/taggers/frame-english-fast',
44 train_with_dev=True,
45 max_epochs=150)@inproceedings{akbik2019flair,
title={FLAIR: An easy-to-use framework for state-of-the-art NLP},
author={Akbik, Alan and Bergmann, Tanja and Blythe, Duncan and Rasul, Kashif and Schweter, Stefan and Vollgraf, Roland},
booktitle={{NAACL} 2019, 2019 Conference of the North American Chapter of the Association for Computational Linguistics (Demonstrations)},
pages={54--59},
year={2019}
}