Views
No views yet
| tag | meaning |
|---|---|
| ADJ | adjective |
| ADP | adposition |
| ADV | adverb |
| AUX | auxiliary |
| CCONJ | coordinating conjunction |
| DET | determiner |
| INTJ | interjection |
| NOUN | noun |
| NUM | numeral |
| PART | particle |
| PRON | pronoun |
| PROPN | proper noun |
| PUNCT | punctuation |
| SCONJ | subordinating conjunction |
| SYM | symbol |
| VERB | verb |
| X | other |
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("flair/upos-english-fast")
6
7# make example sentence
8sentence = Sentence("I love Berlin.")
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('pos'):
20 print(entity)
21Span [1]: "I" [− Labels: PRON (0.9996)]
Span [2]: "love" [− Labels: VERB (1.0)]
Span [3]: "Berlin" [− Labels: PROPN (0.9986)]
Span [4]: "." [− Labels: PUNCT (1.0)]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: Corpus = ColumnCorpus(
7 "resources/tasks/onto-ner",
8 column_format={0: "text", 1: "pos", 2: "upos", 3: "ner"},
9 tag_to_bioes="ner",
10 )
11
12# 2. what tag do we want to predict?
13tag_type = 'upos'
14
15# 3. make the tag dictionary from the corpus
16tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
17
18# 4. initialize each embedding we use
19embedding_types = [
20
21 # contextual string embeddings, forward
22 FlairEmbeddings('news-forward-fast'),
23
24 # contextual string embeddings, backward
25 FlairEmbeddings('news-backward-fast'),
26]
27
28# embedding stack consists of Flair and GloVe embeddings
29embeddings = StackedEmbeddings(embeddings=embedding_types)
30
31# 5. initialize sequence tagger
32from flair.models import SequenceTagger
33
34tagger = SequenceTagger(hidden_size=256,
35 embeddings=embeddings,
36 tag_dictionary=tag_dictionary,
37 tag_type=tag_type)
38
39# 6. initialize trainer
40from flair.trainers import ModelTrainer
41
42trainer = ModelTrainer(tagger, corpus)
43
44# 7. run training
45trainer.train('resources/taggers/upos-english-fast',
46 train_with_dev=True,
47 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}
}