File size: 4,906 Bytes
6cfe17a
 
 
 
 
 
 
 
15bb1b2
d0744e9
b29b2c4
 
 
 
ce71f45
 
b29b2c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3c710b
 
 
 
 
 
 
007695a
b3c710b
 
 
 
 
 
 
007695a
b3c710b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b29b2c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---
license: cc-by-4.0
datasets:
- pythainlp/thainer-corpus-v2
language:
- th
metrics:
- f1
widget:
- text: "ฉันชื่อ นางสาวมะลิวา บุญสระดี อาศัยอยู่ที่อำเภอนางรอง จังหวัดบุรีรัมย์ อายุ 23 ปี เพิ่งเรียนจบจาก มหาวิทยาลัยขอนแก่น และนี่คือข้อมูลปลอม ชื่อคนไม่มีอยู่จริง"
---

This is a Named Entity Recognition model that trained with [Thai NER v2.0 Corpus](https://huggingface.co/datasets/pythainlp/thainer-corpus-v2)

Training script and split data: [https://zenodo.org/record/7761354](https://zenodo.org/record/7761354)

The model was trained by [WangchanBERTa base model](https://huggingface.co/airesearch/wangchanberta-base-att-spm-uncased).

Validation from the Validation set
- Precision: 0.830336794125095
- Recall: 0.873701039168665
- F1: 0.8514671513892494
- Accuracy: 0.9736483416628805

Test from the Test set
- Precision: 0.8199168093956447
- Recall: 0.8781446540880503
- F1: 0.8480323927622422
- Accuracy: 0.9724346779516247

Download: [HuggingFace Hub](https://huggingface.co/datasets/pythainlp/thainer-corpus-v2)

Read more: [Thai NER v2.0](https://pythainlp.github.io/Thai-NER/version/2)

## Inference

Huggingface doesn't support inference token classification for Thai and It will give wrong tag. You must using this code.

```python
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)
```
output:
```python
[('ฉัน', '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')]
```

## Cite

> Wannaphong Phatthiyaphaibun. (2022). Thai NER 2.0 (2.0) [Data set]. Zenodo. https://doi.org/10.5281/zenodo.7761354

or 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}
}
```