Views
No views yet
| tag | meaning |
|---|---|
| ADJP | adjectival |
| ADVP | adverbial |
| CONJP | conjunction |
| INTJ | interjection |
| LST | list marker |
| NP | noun phrase |
| PP | prepositional |
| PRT | particle |
| SBAR | subordinate clause |
| VP | verb phrase |
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("flair/chunk-english")
6
7# make example sentence
8sentence = Sentence("The happy man has been eating at the diner")
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('np'):
20 print(entity)
21Span [1,2,3]: "The happy man" [− Labels: NP (0.9958)]
Span [4,5,6]: "has been eating" [− Labels: VP (0.8759)]
Span [7]: "at" [− Labels: PP (1.0)]
Span [8,9]: "the diner" [− Labels: NP (0.9991)]
1from flair.data import Corpus
2from flair.datasets import CONLL_2000
3from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
4
5# 1. get the corpus
6corpus: Corpus = CONLL_2000()
7
8# 2. what tag do we want to predict?
9tag_type = 'np'
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 # contextual string embeddings, forward
18 FlairEmbeddings('news-forward'),
19
20 # contextual string embeddings, backward
21 FlairEmbeddings('news-backward'),
22]
23
24# embedding stack consists of Flair and GloVe embeddings
25embeddings = StackedEmbeddings(embeddings=embedding_types)
26
27# 5. initialize sequence tagger
28from flair.models import SequenceTagger
29
30tagger = SequenceTagger(hidden_size=256,
31 embeddings=embeddings,
32 tag_dictionary=tag_dictionary,
33 tag_type=tag_type)
34
35# 6. initialize trainer
36from flair.trainers import ModelTrainer
37
38trainer = ModelTrainer(tagger, corpus)
39
40# 7. run training
41trainer.train('resources/taggers/chunk-english',
42 train_with_dev=True,
43 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}
}