Views
No views yet
| tag | meaning |
|---|---|
| ADD | |
| AFX | Affix |
| CC | Coordinating conjunction |
| CD | Cardinal number |
| DT | Determiner |
| EX | Existential there |
| FW | Foreign word |
| HYPH | Hyphen |
| IN | Preposition or subordinating conjunction |
| JJ | Adjective |
| JJR | Adjective, comparative |
| JJS | Adjective, superlative |
| LS | List item marker |
| MD | Modal |
| NFP | Superfluous punctuation |
| NN | Noun, singular or mass |
| NNP | Proper noun, singular |
| NNPS | Proper noun, plural |
| NNS | Noun, plural |
| PDT | Predeterminer |
| POS | Possessive ending |
| PRP | Personal pronoun |
| PRP$ | Possessive pronoun |
| RB | Adverb |
| RBR | Adverb, comparative |
| RBS | Adverb, superlative |
| RP | Particle |
| SYM | Symbol |
| TO | to |
| UH | Interjection |
| VB | Verb, base form |
| VBD | Verb, past tense |
| VBG | Verb, gerund or present participle |
| VBN | Verb, past participle |
| VBP | Verb, non-3rd person singular present |
| VBZ | Verb, 3rd person singular present |
| WDT | Wh-determiner |
| WP | Wh-pronoun |
| WP$ | Possessive wh-pronoun |
| WRB | Wh-adverb |
| XX | Unknown |
pip install flair)1from flair.data import Sentence
2from flair.models import SequenceTagger
3
4# load tagger
5tagger = SequenceTagger.load("flair/pos-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: PRP (1.0)]
Span [2]: "love" [− Labels: VBP (0.9998)]
Span [3]: "Berlin" [− Labels: NNP (0.9999)]
Span [4]: "." [− Labels: . (0.9998)]
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 = 'pos'
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'),
23
24 # contextual string embeddings, backward
25 FlairEmbeddings('news-backward'),
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/pos-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}
}