🚀 Flair中的四语言命名实体识别(英语、德语、荷兰语和西班牙语)
本项目是适用于4种CoNLL - 03语言的标准4类命名实体识别(NER)模型,它随Flair库一同发布。该模型对像法语这样的相关语言也有一定的效果。
F1分数:92.16(CoNLL - 03英语),87.33(CoNLL - 03修订版德语),88.96(CoNLL - 03荷兰语),86.65(CoNLL - 03西班牙语)
该模型可预测4种标签:
标签 |
含义 |
PER |
人名 |
LOC |
地名 |
ORG |
组织名 |
MISC |
其他名称 |
此模型基于Flair嵌入和LSTM - CRF构建。
🚀 快速开始
本模型需要安装**Flair**库,可使用以下命令进行安装:
pip install flair
💻 使用示例
基础用法
from flair.data import Sentence
from flair.models import SequenceTagger
tagger = SequenceTagger.load("flair/ner-multi")
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, CONLL_03_GERMAN, CONLL_03_DUTCH, CONLL_03_SPANISH
from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
corpus: Corpus = MultiCorpus([
CONLL_03(),
CONLL_03_GERMAN(),
CONLL_03_DUTCH(),
CONLL_03_SPANISH(),
])
tag_type = 'ner'
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
embedding_types = [
WordEmbeddings('glove'),
WordEmbeddings('de'),
FlairEmbeddings('multi-forward'),
FlairEmbeddings('multi-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-multi',
train_with_dev=True,
max_epochs=150)
📄 引用
使用此模型时,请引用以下论文:
@misc{akbik2019multilingual,
title={Multilingual sequence labeling with one model},
author={Akbik, Alan and Bergmann, Tanja and Vollgraf, Roland}
booktitle = {{NLDL} 2019, Northern Lights Deep Learning Workshop},
year = {2019}
}
@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}
}