🚀 Flair英文快速命名实体识别模型
本项目是基于 Flair 框架的英文快速四分类命名实体识别(NER)模型。该模型在英文文本的命名实体识别任务中表现出色,能够快速准确地识别出人名、地名、组织机构名和其他类型的实体。
✨ 主要特性
- 高准确率:在修正后的CoNLL - 03数据集上,F1分数达到了 92.92。
- 四标签预测:能够预测四种标签,分别为:
| 标签 | 含义 |
| ---- | ---- |
| 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-english-fast")
sentence = Sentence("George Washington went to 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.9515)]
Span [5]: "Washington" [− Labels: LOC (0.992)]
这表明在句子 "George Washington went to Washington" 中,识别出了实体 "George Washington"(标签为 人名)和 "Washington"(标签为 地名)。
📚 详细文档
模型训练脚本
以下是用于训练该模型的Flair脚本:
from flair.data import Corpus
from flair.datasets import CONLL_03
from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings
corpus: Corpus = CONLL_03()
tag_type = 'ner'
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
embedding_types = [
WordEmbeddings('glove'),
FlairEmbeddings('news-forward-fast'),
FlairEmbeddings('news-backward-fast'),
]
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-english',
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问题跟踪器 中反馈。