Views
No views yet
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("tadejmagajna/flair-sl-pos")
6
7# make example sentence
8sentence = Sentence("Danes je lep dan.")
9
10# predict PoS tags
11tagger.predict(sentence)
12
13# print sentence
14print(sentence)
15
16# print predicted PoS spans
17print('The following PoS tags are found:')
18# iterate over parts of speech and print
19for tag in sentence.get_spans('pos'):
20 print(tag)Sentence: "Danes je lep dan ." [− Tokens: 5 − Token-Labels: "Danes <Rgp> je <Va-r3s-n> lep <Agpmsnn> dan <Ncmsn> . <Z>"]
The following PoS tags are found:
Span [1]: "Danes" [− Labels: Rgp (1.0)]
Span [2]: "je" [− Labels: Va-r3s-n (1.0)]
Span [3]: "lep" [− Labels: Agpmsnn (0.9999)]
Span [4]: "dan" [− Labels: Ncmsn (1.0)]
Span [5]: "." [− Labels: Z (1.0)]1from flair.data import Corpus
2from flair.datasets import UD_SLOVENIAN
3from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
4
5# 1. get the corpus
6corpus: Corpus = UD_SLOVENIAN()
7
8# 2. what tag do we want to predict?
9tag_type = 'pos'
10
11# 3. make the tag dictionary from the corpus
12tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
13
14# 4. initialize embeddings
15embedding_types = [
16 WordEmbeddings('sl'),
17 FlairEmbeddings('sl-forward'),
18 FlairEmbeddings('sl-backward'),
19]
20embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types)
21
22# 5. initialize sequence tagger
23from flair.models import SequenceTagger
24
25tagger: SequenceTagger = SequenceTagger(hidden_size=256,
26 embeddings=embeddings,
27 tag_dictionary=tag_dictionary,
28 tag_type=tag_type)
29
30# 6. initialize trainer
31from flair.trainers import ModelTrainer
32
33trainer: ModelTrainer = ModelTrainer(tagger, corpus)
34
35# 7. start training
36trainer.train('resources/taggers/pos-slovene',
37 train_with_dev=True,
38 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}
}