🚀 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問題跟蹤器 中反饋。