1from flair.data import Sentence
2from flair.models import SequenceTagger
34# load tagger5tagger = SequenceTagger.load("flair/upos-multi-fast")67# make example sentence8sentence = Sentence("Ich liebe Berlin, as they say. ")910# predict NER tags11tagger.predict(sentence)1213# print sentence14print(sentence)1516# print predicted NER spans17print('The following NER tags are found:')18# iterate over entities and print19for entity in sentence.get_spans('pos'):20print(entity)
So, the words "Ich" and "they" are labeled as pronouns (PRON), while "liebe" and "say" are labeled as verbs (VERB) in the multilingual sentence "Ich liebe Berlin, as they say".
Training: Script to train this model
The following Flair script was used to train this model:
python
1from flair.data import MultiCorpus
2from flair.datasets import UD_ENGLISH, UD_GERMAN, UD_FRENCH, UD_ITALIAN, UD_POLISH, UD_DUTCH, UD_CZECH, \
3 UD_DANISH, UD_SPANISH, UD_SWEDISH, UD_NORWEGIAN, UD_FINNISH
4from flair.embeddings import StackedEmbeddings, FlairEmbeddings
56# 1. make a multi corpus consisting of 12 UD treebanks (in_memory=False here because this corpus becomes large)7corpus = MultiCorpus([8 UD_ENGLISH(in_memory=False),9 UD_GERMAN(in_memory=False),10 UD_DUTCH(in_memory=False),11 UD_FRENCH(in_memory=False),12 UD_ITALIAN(in_memory=False),13 UD_SPANISH(in_memory=False),14 UD_POLISH(in_memory=False),15 UD_CZECH(in_memory=False),16 UD_DANISH(in_memory=False),17 UD_SWEDISH(in_memory=False),18 UD_NORWEGIAN(in_memory=False),19 UD_FINNISH(in_memory=False),20])2122# 2. what tag do we want to predict?23tag_type ='upos'2425# 3. make the tag dictionary from the corpus26tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)2728# 4. initialize each embedding we use29embedding_types =[3031# contextual string embeddings, forward32 FlairEmbeddings('multi-forward-fast'),3334# contextual string embeddings, backward35 FlairEmbeddings('multi-backward-fast'),36]3738# embedding stack consists of Flair and GloVe embeddings39embeddings = StackedEmbeddings(embeddings=embedding_types)4041# 5. initialize sequence tagger42from flair.models import SequenceTagger
4344tagger = SequenceTagger(hidden_size=256,45 embeddings=embeddings,46 tag_dictionary=tag_dictionary,47 tag_type=tag_type,48 use_crf=False)4950# 6. initialize trainer51from flair.trainers import ModelTrainer
5253trainer = ModelTrainer(tagger, corpus)5455# 7. run training56trainer.train('resources/taggers/upos-multi-fast',57 train_with_dev=True,58 max_epochs=150)
Cite
Please cite the following paper when using this model.
@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}
}