wannaphong
commited on
Commit
•
b3c710b
1
Parent(s):
ce71f45
Update README.md
Browse files
README.md
CHANGED
@@ -32,6 +32,105 @@ Download: [HuggingFace Hub](https://huggingface.co/datasets/pythainlp/thainer-co
|
|
32 |
|
33 |
Read more: [Thai NER v2.0](https://pythainlp.github.io/Thai-NER/version/2)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
## Cite
|
36 |
|
37 |
> Wannaphong Phatthiyaphaibun. (2022). Thai NER 2.0 (2.0) [Data set]. Zenodo. https://doi.org/10.5281/zenodo.7761354
|
|
|
32 |
|
33 |
Read more: [Thai NER v2.0](https://pythainlp.github.io/Thai-NER/version/2)
|
34 |
|
35 |
+
## Inference
|
36 |
+
|
37 |
+
Huggingface doesn't support inference token classification for Thai and It will give wrong tag. You must using this code.
|
38 |
+
|
39 |
+
```python
|
40 |
+
from transformers import AutoTokenizer
|
41 |
+
from transformers import AutoModelForTokenClassification
|
42 |
+
import torch
|
43 |
+
|
44 |
+
name="pythainlp/thainer-corpus-v2-base-model"
|
45 |
+
tokenizer = AutoTokenizer.from_pretrained(name)
|
46 |
+
model = AutoModelForTokenClassification.from_pretrained(name)
|
47 |
+
|
48 |
+
sentence="ฉันชื่อ นางสาวมะลิวา บุญสระดี อาศัยอยู่ที่อำเภอนางรอง จังหวัดบุรีรัมย์ อายุ 23 ปี เพิ่งเรียนจบจาก มหาวิทยาลัยขอนแก่น และนี่คือข้อมูลปลอมชื่อคนไม่มีอยู่จริง อายุ 23 ปี"
|
49 |
+
inputs=tokenizer(cut,is_split_into_words=True,return_tensors="pt")
|
50 |
+
|
51 |
+
ids = inputs["input_ids"]
|
52 |
+
mask = inputs["attention_mask"]
|
53 |
+
# forward pass
|
54 |
+
outputs = model(ids, attention_mask=mask)
|
55 |
+
logits = outputs[0]
|
56 |
+
|
57 |
+
predictions = torch.argmax(logits, dim=2)
|
58 |
+
predicted_token_class = [model.config.id2label[t.item()] for t in predictions[0]]
|
59 |
+
|
60 |
+
def fix_span_error(words,ner):
|
61 |
+
_ner = []
|
62 |
+
_ner=ner
|
63 |
+
_new_tag=[]
|
64 |
+
for i,j in zip(words,_ner):
|
65 |
+
#print(i,j)
|
66 |
+
i=tokenizer.decode(i)
|
67 |
+
if i.isspace() and j.startswith("B-"):
|
68 |
+
j="O"
|
69 |
+
if i=='' or i=='<s>' or i=='</s>':
|
70 |
+
continue
|
71 |
+
if i=="<_>":
|
72 |
+
i=" "
|
73 |
+
_new_tag.append((i,j))
|
74 |
+
return _new_tag
|
75 |
+
|
76 |
+
ner_tag=fix_span_error(inputs['input_ids'][0],predicted_token_class)
|
77 |
+
print(ner_tag)
|
78 |
+
```
|
79 |
+
output:
|
80 |
+
```python
|
81 |
+
[('ฉัน', 'O'),
|
82 |
+
('ชื่อ', 'O'),
|
83 |
+
(' ', 'O'),
|
84 |
+
('นางสาว', 'B-PERSON'),
|
85 |
+
('มะลิ', 'I-PERSON'),
|
86 |
+
('วา', 'I-PERSON'),
|
87 |
+
(' ', 'I-PERSON'),
|
88 |
+
('บุญ', 'I-PERSON'),
|
89 |
+
('สระ', 'I-PERSON'),
|
90 |
+
('ดี', 'I-PERSON'),
|
91 |
+
(' ', 'O'),
|
92 |
+
('อาศัย', 'O'),
|
93 |
+
('อยู่', 'O'),
|
94 |
+
('ที่', 'O'),
|
95 |
+
('อําเภอ', 'B-LOCATION'),
|
96 |
+
('นาง', 'I-LOCATION'),
|
97 |
+
('รอง', 'I-LOCATION'),
|
98 |
+
(' ', 'O'),
|
99 |
+
('จังหวัด', 'B-LOCATION'),
|
100 |
+
('บุรีรัมย์', 'I-LOCATION'),
|
101 |
+
(' ', 'O'),
|
102 |
+
('อายุ', 'O'),
|
103 |
+
(' ', 'O'),
|
104 |
+
('23', 'B-AGO'),
|
105 |
+
(' ', 'I-AGO'),
|
106 |
+
('ปี', 'I-AGO'),
|
107 |
+
(' ', 'O'),
|
108 |
+
('เพิ่ง', 'O'),
|
109 |
+
('เรียนจบ', 'O'),
|
110 |
+
('จาก', 'O'),
|
111 |
+
(' ', 'O'),
|
112 |
+
('มหาวิทยาลั', 'B-ORGANIZATION'),
|
113 |
+
('ยขอนแก่น', 'I-ORGANIZATION'),
|
114 |
+
(' ', 'O'),
|
115 |
+
('และ', 'O'),
|
116 |
+
('นี่', 'O'),
|
117 |
+
('คือ', 'O'),
|
118 |
+
('ข้อมูล', 'O'),
|
119 |
+
('ปลอม', 'O'),
|
120 |
+
('ชื่อ', 'O'),
|
121 |
+
('คน', 'O'),
|
122 |
+
('ไม่', 'O'),
|
123 |
+
('มี', 'O'),
|
124 |
+
('อยู่', 'O'),
|
125 |
+
('จริง', 'O'),
|
126 |
+
(' ', 'O'),
|
127 |
+
('อายุ', 'O'),
|
128 |
+
(' ', 'O'),
|
129 |
+
('23', 'B-AGO'),
|
130 |
+
(' ', 'O'),
|
131 |
+
('ปี', 'I-AGO')]
|
132 |
+
```
|
133 |
+
|
134 |
## Cite
|
135 |
|
136 |
> Wannaphong Phatthiyaphaibun. (2022). Thai NER 2.0 (2.0) [Data set]. Zenodo. https://doi.org/10.5281/zenodo.7761354
|