这是一个命名实体识别模型,使用了 Thai NER v2.0 Corpus 进行训练
训练脚本和数据划分: https://zenodo.org/record/7761354
模型由 WangchanBERTa base model 进行训练。
通过验证集验证结果如下:
通过测试集验证结果如下:
下载链接: HuggingFace Hub
阅读更多: Thai NER v2.0
Huggingface不支持泰语的推断标记分类,会给出错误的标签。您必须使用以下代码。
from transformers import AutoTokenizer
from transformers import AutoModelForTokenClassification
from pythainlp.tokenize import word_tokenize # pip install pythainlp
import torch
name="pythainlp/thainer-corpus-v2-base-model"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForTokenClassification.from_pretrained(name)
sentence="ฉันชื่อ นางสาวมะลิวา บุญสระดี อาศัยอยู่ที่อำเภอนางรอง จังหวัดบุรีรัมย์ อายุ 23 ปี เพิ่งเรียนจบจาก มหาวิทยาลัยขอนแก่น และนี่คือข้อมูลปลอมชื่อคนไม่มีอยู่จริง อายุ 23 ปี"
cut=word_tokenize(sentence.replace(" ", "<_>"))
inputs=tokenizer(cut,is_split_into_words=True,return_tensors="pt")
ids = inputs["input_ids"]
mask = inputs["attention_mask"]
# forward pass
outputs = model(ids, attention_mask=mask)
logits = outputs[0]
predictions = torch.argmax(logits, dim=2)
predicted_token_class = [model.config.id2label[t.item()] for t in predictions[0]]
def fix_span_error(words,ner):
_ner = []
_ner=ner
_new_tag=[]
for i,j in zip(words,_ner):
#print(i,j)
i=tokenizer.decode(i)
if i.isspace() and j.startswith("B-"):
j="O"
if i=='' or i=='<s>' or i=='</s>':
continue
if i=="<_>":
i=" "
_new_tag.append((i,j))
return _new_tag
ner_tag=fix_span_error(inputs['input_ids'][0],predicted_token_class)
print(ner_tag)
输出:
[('ฉัน', 'O'),
('ชื่อ', 'O'),
(' ', 'O'),
('นางสาว', 'B-PERSON'),
('มะลิ', 'I-PERSON'),
('วา', 'I-PERSON'),
(' ', 'I-PERSON'),
('บุญ', 'I-PERSON'),
('สระ', 'I-PERSON'),
('ดี', 'I-PERSON'),
(' ', 'O'),
('อาศัย', 'O'),
('อยู่', 'O'),
('ที่', 'O'),
('อําเภอ', 'B-LOCATION'),
('นาง', 'I-LOCATION'),
('รอง', 'I-LOCATION'),
(' ', 'O'),
('จังหวัด', 'B-LOCATION'),
('บุรีรัมย์', 'I-LOCATION'),
(' ', 'O'),
('อายุ', 'O'),
(' ', 'O'),
('23', 'B-AGO'),
(' ', 'I-AGO'),
('ปี', 'I-AGO'),
(' ', 'O'),
('เพิ่ง', 'O'),
('เรียนจบ', 'O'),
('จาก', 'O'),
(' ', 'O'),
('มหาวิทยาลั', 'B-ORGANIZATION'),
('ยขอนแก่น', 'I-ORGANIZATION'),
(' ', 'O'),
('และ', 'O'),
('นี่', 'O'),
('คือ', 'O'),
('ข้อมูล', 'O'),
('ปลอม', 'O'),
('ชื่อ', 'O'),
('คน', 'O'),
('ไม่', 'O'),
('มี', 'O'),
('อยู่', 'O'),
('จริง', 'O'),
(' ', 'O'),
('อายุ', 'O'),
(' ', 'O'),
('23', 'B-AGO'),
(' ', 'O'),
('ปี', 'I-AGO')]
Wannaphong Phatthiyaphaibun. (2022). Thai NER 2.0 (2.0) [数据集]. Zenodo. https://doi.org/10.5281/zenodo.7761354
或者BibTeX
@dataset{wannaphong_phatthiyaphaibun_2022_7761354,
author = {Wannaphong Phatthiyaphaibun},
title = {Thai NER 2.0},
month = sep,
year = 2022,
publisher = {Zenodo},
version = {2.0},
doi = {10.5281/zenodo.7761354},
url = {https://doi.org/10.5281/zenodo.7761354}
}