🚀 德語命名實體識別(Flair默認模型)
這是隨 Flair 一起發佈的標準4類德語命名實體識別(NER)模型。該模型基於 Flair嵌入 和LSTM - CRF,在CoNLL - 03德語修訂版數據集上的F1分數達到了 87.94。
✨ 主要特性
- 預測4種標籤:
| 標籤 | 含義 |
| ---- | ---- |
| PER | 人名 |
| LOC | 地名 |
| ORG | 組織名 |
| MISC | 其他名稱 |
📦 安裝指南
使用此模型需要安裝 Flair,可以通過以下命令進行安裝:
pip install flair
💻 使用示例
基礎用法
以下是在Flair中使用該模型的示例代碼:
from flair.data import Sentence
from flair.models import SequenceTagger
tagger = SequenceTagger.load("flair/ner-german")
sentence = Sentence("George Washington ging nach Washington")
tagger.predict(sentence)
print(sentence)
print('The following NER tags are found:')
for entity in sentence.get_spans('ner'):
print(entity)
運行上述代碼會得到以下輸出:
Span [1,2]: "George Washington" [− Labels: PER (0.9977)]
Span [5]: "Washington" [− Labels: LOC (0.9895)]
這表明在句子 "George Washington ging nach Washington" 中,識別出了實體 "George Washington"(標記為 人名)和 "Washington"(標記為 地名)。
🔧 技術細節
訓練腳本
以下是用於訓練該模型的Flair腳本:
from flair.data import Corpus
from flair.datasets import CONLL_03_GERMAN
from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
corpus: Corpus = CONLL_03_GERMAN()
tag_type = 'ner'
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
embedding_types = [
WordEmbeddings('de'),
FlairEmbeddings('de-forward'),
FlairEmbeddings('de-backward'),
]
embeddings = StackedEmbeddings(embeddings=embedding_types)
from flair.models import SequenceTagger
tagger = SequenceTagger(hidden_size=256,
embeddings=embeddings,
tag_dictionary=tag_dictionary,
tag_type=tag_type)
from flair.trainers import ModelTrainer
trainer = ModelTrainer(tagger, corpus)
trainer.train('resources/taggers/ner-german',
train_with_dev=True,
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}
}
📚 問題反饋
如果你在使用過程中遇到問題,可以在 Flair問題跟蹤器 中反饋。