KoichiYasuoka commited on
Commit
fb5a49f
1 Parent(s): b4bb108

initial release

Browse files
Files changed (9) hide show
  1. README.md +76 -0
  2. config.json +517 -0
  3. maker.py +55 -0
  4. pytorch_model.bin +3 -0
  5. special_tokens_map.json +7 -0
  6. tokenizer.json +0 -0
  7. tokenizer_config.json +14 -0
  8. ud.py +61 -0
  9. vocab.txt +0 -0
README.md ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "th"
4
+ tags:
5
+ - "thai"
6
+ - "token-classification"
7
+ - "pos"
8
+ - "dependency-parsing"
9
+ datasets:
10
+ - "universal_dependencies"
11
+ license: "apache-2.0"
12
+ pipeline_tag: "token-classification"
13
+ widget:
14
+ - text: "หลายหัวดีกว่าหัวเดียว"
15
+ ---
16
+
17
+ # roberta-base-thai-syllable-ud-goeswith
18
+
19
+ ## Model Description
20
+
21
+ This is a RoBERTa model pre-trained on Thai Wikipedia texts for POS-tagging and dependency-parsing (using `goeswith` for subwords), derived from [roberta-base-thai-syllable](https://huggingface.co/KoichiYasuoka/roberta-base-thai-syllable).
22
+
23
+ ## How to Use
24
+
25
+ ```py
26
+ class UDgoeswith(object):
27
+ def __init__(self,bert):
28
+ from transformers import AutoTokenizer,AutoModelForTokenClassification
29
+ self.tokenizer=AutoTokenizer.from_pretrained(bert)
30
+ self.model=AutoModelForTokenClassification.from_pretrained(bert)
31
+ def __call__(self,text):
32
+ import numpy,torch,ufal.chu_liu_edmonds
33
+ w=self.tokenizer(text,return_offsets_mapping=True)
34
+ v=w["input_ids"]
35
+ x=[v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[j] for i,j in enumerate(v[1:-1],1)]
36
+ with torch.no_grad():
37
+ e=self.model(input_ids=torch.tensor(x)).logits.numpy()[:,1:-2,:]
38
+ r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
39
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
40
+ g=self.model.config.label2id["X|_|goeswith"]
41
+ r=numpy.tri(e.shape[0])
42
+ for i in range(e.shape[0]):
43
+ for j in range(i+2,e.shape[1]):
44
+ r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
45
+ e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
46
+ m=numpy.full((e.shape[0]+1,e.shape[1]+1),numpy.nan)
47
+ m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
48
+ p=numpy.zeros(m.shape)
49
+ p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
50
+ for i in range(1,m.shape[0]):
51
+ m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
52
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
53
+ if [0 for i in h if i==0]!=[0]:
54
+ m[:,0]+=numpy.where(m[:,0]==numpy.nanmax(m[[i for i,j in enumerate(h) if j==0],0]),0,numpy.nan)
55
+ m[[i for i,j in enumerate(h) if j==0]]+=[0 if i==0 or j==0 else numpy.nan for i,j in enumerate(h)]
56
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
57
+ u="# text = "+text+"\n"
58
+ v=[(s,e) for s,e in w["offset_mapping"] if s<e]
59
+ for i,(s,e) in enumerate(v,1):
60
+ q=self.model.config.id2label[p[i,h[i]]].split("|")
61
+ u+="\t".join([str(i),text[s:e],"_",q[0],"_","|".join(q[1:-1]),str(h[i]),q[-1],"_","_" if i<len(v) and e<v[i][0] else "SpaceAfter=No"])+"\n"
62
+ return u+"\n"
63
+
64
+ nlp=UDgoeswith("KoichiYasuoka/roberta-base-thai-syllable-ud-goeswith")
65
+ print(nlp("หลายหัวดีกว่าหัวเดียว"))
66
+ ```
67
+
68
+ with [ufal.chu-liu-edmonds](https://pypi.org/project/ufal.chu-liu-edmonds/).
69
+ Or without ufal.chu-liu-edmonds:
70
+
71
+ ```
72
+ from transformers import pipeline
73
+ nlp=pipeline("universal-dependencies","KoichiYasuoka/roberta-base-thai-syllable-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
74
+ print(nlp("หลายหัวดีกว่าหัวเดียว"))
75
+ ```
76
+
config.json ADDED
@@ -0,0 +1,517 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "RobertaForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "classifier_dropout": null,
8
+ "custom_pipelines": {
9
+ "universal-dependencies": {
10
+ "impl": "ud.UniversalDependenciesPipeline"
11
+ }
12
+ },
13
+ "eos_token_id": 2,
14
+ "gradient_checkpointing": false,
15
+ "hidden_act": "gelu",
16
+ "hidden_dropout_prob": 0.1,
17
+ "hidden_size": 768,
18
+ "id2label": {
19
+ "0": "-|_|dep",
20
+ "1": "ADP|_|acl",
21
+ "2": "ADP|_|advcl",
22
+ "3": "ADP|_|advmod",
23
+ "4": "ADP|_|appos",
24
+ "5": "ADP|_|case",
25
+ "6": "ADP|_|cc",
26
+ "7": "ADP|_|cc:preconj",
27
+ "8": "ADP|_|csubj",
28
+ "9": "ADP|_|fixed",
29
+ "10": "ADP|_|mark",
30
+ "11": "ADP|_|obl",
31
+ "12": "ADP|_|root",
32
+ "13": "ADV|PronType=Int|advmod",
33
+ "14": "ADV|_|advcl",
34
+ "15": "ADV|_|advmod",
35
+ "16": "ADV|_|aux",
36
+ "17": "ADV|_|cc",
37
+ "18": "ADV|_|ccomp",
38
+ "19": "ADV|_|conj",
39
+ "20": "ADV|_|fixed",
40
+ "21": "ADV|_|mark",
41
+ "22": "ADV|_|obj",
42
+ "23": "ADV|_|root",
43
+ "24": "ADV|_|xcomp",
44
+ "25": "AUX|_|advmod",
45
+ "26": "AUX|_|aux",
46
+ "27": "AUX|_|aux:pass",
47
+ "28": "AUX|_|ccomp",
48
+ "29": "AUX|_|conj",
49
+ "30": "AUX|_|cop",
50
+ "31": "AUX|_|mark",
51
+ "32": "CCONJ|_|advmod",
52
+ "33": "CCONJ|_|case",
53
+ "34": "CCONJ|_|cc",
54
+ "35": "CCONJ|_|compound",
55
+ "36": "CCONJ|_|conj",
56
+ "37": "CCONJ|_|fixed",
57
+ "38": "CCONJ|_|mark",
58
+ "39": "CCONJ|_|nsubj",
59
+ "40": "CCONJ|_|obl",
60
+ "41": "CCONJ|_|root",
61
+ "42": "DET|PronType=Int|det",
62
+ "43": "DET|_|advmod",
63
+ "44": "DET|_|case",
64
+ "45": "DET|_|cc:preconj",
65
+ "46": "DET|_|conj",
66
+ "47": "DET|_|det",
67
+ "48": "DET|_|det:predet",
68
+ "49": "DET|_|fixed",
69
+ "50": "DET|_|mark",
70
+ "51": "DET|_|nsubj",
71
+ "52": "DET|_|nsubj:pass",
72
+ "53": "DET|_|obj",
73
+ "54": "DET|_|obl",
74
+ "55": "DET|_|obl:tmod",
75
+ "56": "DET|_|root",
76
+ "57": "INTJ|_|acl",
77
+ "58": "INTJ|_|nsubj",
78
+ "59": "INTJ|_|root",
79
+ "60": "NOUN|_|acl",
80
+ "61": "NOUN|_|acl:relcl",
81
+ "62": "NOUN|_|advcl",
82
+ "63": "NOUN|_|advmod",
83
+ "64": "NOUN|_|appos",
84
+ "65": "NOUN|_|aux",
85
+ "66": "NOUN|_|case",
86
+ "67": "NOUN|_|cc",
87
+ "68": "NOUN|_|ccomp",
88
+ "69": "NOUN|_|clf",
89
+ "70": "NOUN|_|compound",
90
+ "71": "NOUN|_|conj",
91
+ "72": "NOUN|_|dislocated",
92
+ "73": "NOUN|_|fixed",
93
+ "74": "NOUN|_|flat:name",
94
+ "75": "NOUN|_|iobj",
95
+ "76": "NOUN|_|mark",
96
+ "77": "NOUN|_|nmod",
97
+ "78": "NOUN|_|nmod:poss",
98
+ "79": "NOUN|_|nsubj",
99
+ "80": "NOUN|_|nsubj:pass",
100
+ "81": "NOUN|_|obj",
101
+ "82": "NOUN|_|obl",
102
+ "83": "NOUN|_|obl:poss",
103
+ "84": "NOUN|_|obl:tmod",
104
+ "85": "NOUN|_|parataxis",
105
+ "86": "NOUN|_|root",
106
+ "87": "NOUN|_|vocative",
107
+ "88": "NOUN|_|xcomp",
108
+ "89": "NUM|_|acl",
109
+ "90": "NUM|_|acl:relcl",
110
+ "91": "NUM|_|advmod",
111
+ "92": "NUM|_|appos",
112
+ "93": "NUM|_|ccomp",
113
+ "94": "NUM|_|clf",
114
+ "95": "NUM|_|conj",
115
+ "96": "NUM|_|flat:name",
116
+ "97": "NUM|_|nmod",
117
+ "98": "NUM|_|nsubj",
118
+ "99": "NUM|_|nummod",
119
+ "100": "NUM|_|obj",
120
+ "101": "NUM|_|obl",
121
+ "102": "NUM|_|obl:poss",
122
+ "103": "NUM|_|obl:tmod",
123
+ "104": "NUM|_|root",
124
+ "105": "NUM|_|xcomp",
125
+ "106": "PART|Aspect=Perf|aux",
126
+ "107": "PART|Aspect=Perf|xcomp",
127
+ "108": "PART|Aspect=Prog|aux",
128
+ "109": "PART|Polarity=Neg|advmod",
129
+ "110": "PART|PronType=Int|acl",
130
+ "111": "PART|PronType=Int|advmod",
131
+ "112": "PART|PronType=Int|discourse",
132
+ "113": "PART|PronType=Int|obj",
133
+ "114": "PART|PronType=Int|root",
134
+ "115": "PART|_|acl",
135
+ "116": "PART|_|advmod",
136
+ "117": "PART|_|aux",
137
+ "118": "PART|_|cc",
138
+ "119": "PART|_|cc:preconj",
139
+ "120": "PART|_|ccomp",
140
+ "121": "PART|_|clf",
141
+ "122": "PART|_|compound",
142
+ "123": "PART|_|compound:prt",
143
+ "124": "PART|_|conj",
144
+ "125": "PART|_|discourse",
145
+ "126": "PART|_|fixed",
146
+ "127": "PART|_|mark",
147
+ "128": "PART|_|nmod",
148
+ "129": "PART|_|nmod:poss",
149
+ "130": "PART|_|nsubj",
150
+ "131": "PART|_|obj",
151
+ "132": "PART|_|obl",
152
+ "133": "PART|_|root",
153
+ "134": "PRON|Person=1|compound",
154
+ "135": "PRON|Person=1|nmod:poss",
155
+ "136": "PRON|Person=1|nsubj",
156
+ "137": "PRON|Person=1|nsubj:pass",
157
+ "138": "PRON|Person=1|obj",
158
+ "139": "PRON|Person=1|obl",
159
+ "140": "PRON|Person=1|obl:poss",
160
+ "141": "PRON|Person=2|compound",
161
+ "142": "PRON|Person=2|nmod:poss",
162
+ "143": "PRON|Person=2|nsubj",
163
+ "144": "PRON|Person=2|obj",
164
+ "145": "PRON|Person=2|obl",
165
+ "146": "PRON|Person=3|advmod",
166
+ "147": "PRON|Person=3|appos",
167
+ "148": "PRON|Person=3|compound",
168
+ "149": "PRON|Person=3|conj",
169
+ "150": "PRON|Person=3|nmod",
170
+ "151": "PRON|Person=3|nmod:poss",
171
+ "152": "PRON|Person=3|nsubj",
172
+ "153": "PRON|Person=3|nsubj:pass",
173
+ "154": "PRON|Person=3|obj",
174
+ "155": "PRON|Person=3|obl",
175
+ "156": "PRON|Person=3|obl:poss",
176
+ "157": "PRON|Person=3|reparandum",
177
+ "158": "PRON|Person=3|xcomp",
178
+ "159": "PRON|PronType=Int|nsubj",
179
+ "160": "PRON|PronType=Int|obj",
180
+ "161": "PRON|PronType=Int|obl",
181
+ "162": "PRON|PronType=Int|root",
182
+ "163": "PRON|_|acl",
183
+ "164": "PRON|_|acl:relcl",
184
+ "165": "PRON|_|advcl",
185
+ "166": "PRON|_|advmod",
186
+ "167": "PRON|_|ccomp",
187
+ "168": "PRON|_|compound",
188
+ "169": "PRON|_|conj",
189
+ "170": "PRON|_|fixed",
190
+ "171": "PRON|_|nmod",
191
+ "172": "PRON|_|nmod:poss",
192
+ "173": "PRON|_|nsubj",
193
+ "174": "PRON|_|obj",
194
+ "175": "PRON|_|obl",
195
+ "176": "PRON|_|obl:poss",
196
+ "177": "PRON|_|root",
197
+ "178": "PRON|_|xcomp",
198
+ "179": "PROPN|_|acl",
199
+ "180": "PROPN|_|acl:relcl",
200
+ "181": "PROPN|_|advmod",
201
+ "182": "PROPN|_|appos",
202
+ "183": "PROPN|_|aux",
203
+ "184": "PROPN|_|cc",
204
+ "185": "PROPN|_|ccomp",
205
+ "186": "PROPN|_|clf",
206
+ "187": "PROPN|_|compound",
207
+ "188": "PROPN|_|conj",
208
+ "189": "PROPN|_|flat:name",
209
+ "190": "PROPN|_|goeswith",
210
+ "191": "PROPN|_|nmod",
211
+ "192": "PROPN|_|nmod:poss",
212
+ "193": "PROPN|_|nsubj",
213
+ "194": "PROPN|_|nsubj:pass",
214
+ "195": "PROPN|_|obj",
215
+ "196": "PROPN|_|obl",
216
+ "197": "PROPN|_|obl:poss",
217
+ "198": "PROPN|_|obl:tmod",
218
+ "199": "PROPN|_|root",
219
+ "200": "PROPN|_|xcomp",
220
+ "201": "PUNCT|_|advmod",
221
+ "202": "PUNCT|_|clf",
222
+ "203": "PUNCT|_|punct",
223
+ "204": "PUNCT|_|root",
224
+ "205": "SCONJ|_|mark",
225
+ "206": "SYM|_|advmod",
226
+ "207": "SYM|_|clf",
227
+ "208": "SYM|_|nsubj",
228
+ "209": "SYM|_|obj",
229
+ "210": "SYM|_|obl",
230
+ "211": "VERB|Mood=Imp|aux",
231
+ "212": "VERB|Mood=Imp|xcomp",
232
+ "213": "VERB|Voice=Pass|aux",
233
+ "214": "VERB|Voice=Pass|aux:pass",
234
+ "215": "VERB|_|acl",
235
+ "216": "VERB|_|acl:relcl",
236
+ "217": "VERB|_|advcl",
237
+ "218": "VERB|_|advmod",
238
+ "219": "VERB|_|appos",
239
+ "220": "VERB|_|aux",
240
+ "221": "VERB|_|case",
241
+ "222": "VERB|_|cc",
242
+ "223": "VERB|_|ccomp",
243
+ "224": "VERB|_|compound",
244
+ "225": "VERB|_|conj",
245
+ "226": "VERB|_|csubj",
246
+ "227": "VERB|_|fixed",
247
+ "228": "VERB|_|mark",
248
+ "229": "VERB|_|nmod",
249
+ "230": "VERB|_|nmod:poss",
250
+ "231": "VERB|_|nsubj",
251
+ "232": "VERB|_|obj",
252
+ "233": "VERB|_|obl",
253
+ "234": "VERB|_|obl:poss",
254
+ "235": "VERB|_|parataxis",
255
+ "236": "VERB|_|root",
256
+ "237": "VERB|_|xcomp",
257
+ "238": "X|_|goeswith"
258
+ },
259
+ "initializer_range": 0.02,
260
+ "intermediate_size": 3072,
261
+ "label2id": {
262
+ "-|_|dep": 0,
263
+ "ADP|_|acl": 1,
264
+ "ADP|_|advcl": 2,
265
+ "ADP|_|advmod": 3,
266
+ "ADP|_|appos": 4,
267
+ "ADP|_|case": 5,
268
+ "ADP|_|cc": 6,
269
+ "ADP|_|cc:preconj": 7,
270
+ "ADP|_|csubj": 8,
271
+ "ADP|_|fixed": 9,
272
+ "ADP|_|mark": 10,
273
+ "ADP|_|obl": 11,
274
+ "ADP|_|root": 12,
275
+ "ADV|PronType=Int|advmod": 13,
276
+ "ADV|_|advcl": 14,
277
+ "ADV|_|advmod": 15,
278
+ "ADV|_|aux": 16,
279
+ "ADV|_|cc": 17,
280
+ "ADV|_|ccomp": 18,
281
+ "ADV|_|conj": 19,
282
+ "ADV|_|fixed": 20,
283
+ "ADV|_|mark": 21,
284
+ "ADV|_|obj": 22,
285
+ "ADV|_|root": 23,
286
+ "ADV|_|xcomp": 24,
287
+ "AUX|_|advmod": 25,
288
+ "AUX|_|aux": 26,
289
+ "AUX|_|aux:pass": 27,
290
+ "AUX|_|ccomp": 28,
291
+ "AUX|_|conj": 29,
292
+ "AUX|_|cop": 30,
293
+ "AUX|_|mark": 31,
294
+ "CCONJ|_|advmod": 32,
295
+ "CCONJ|_|case": 33,
296
+ "CCONJ|_|cc": 34,
297
+ "CCONJ|_|compound": 35,
298
+ "CCONJ|_|conj": 36,
299
+ "CCONJ|_|fixed": 37,
300
+ "CCONJ|_|mark": 38,
301
+ "CCONJ|_|nsubj": 39,
302
+ "CCONJ|_|obl": 40,
303
+ "CCONJ|_|root": 41,
304
+ "DET|PronType=Int|det": 42,
305
+ "DET|_|advmod": 43,
306
+ "DET|_|case": 44,
307
+ "DET|_|cc:preconj": 45,
308
+ "DET|_|conj": 46,
309
+ "DET|_|det": 47,
310
+ "DET|_|det:predet": 48,
311
+ "DET|_|fixed": 49,
312
+ "DET|_|mark": 50,
313
+ "DET|_|nsubj": 51,
314
+ "DET|_|nsubj:pass": 52,
315
+ "DET|_|obj": 53,
316
+ "DET|_|obl": 54,
317
+ "DET|_|obl:tmod": 55,
318
+ "DET|_|root": 56,
319
+ "INTJ|_|acl": 57,
320
+ "INTJ|_|nsubj": 58,
321
+ "INTJ|_|root": 59,
322
+ "NOUN|_|acl": 60,
323
+ "NOUN|_|acl:relcl": 61,
324
+ "NOUN|_|advcl": 62,
325
+ "NOUN|_|advmod": 63,
326
+ "NOUN|_|appos": 64,
327
+ "NOUN|_|aux": 65,
328
+ "NOUN|_|case": 66,
329
+ "NOUN|_|cc": 67,
330
+ "NOUN|_|ccomp": 68,
331
+ "NOUN|_|clf": 69,
332
+ "NOUN|_|compound": 70,
333
+ "NOUN|_|conj": 71,
334
+ "NOUN|_|dislocated": 72,
335
+ "NOUN|_|fixed": 73,
336
+ "NOUN|_|flat:name": 74,
337
+ "NOUN|_|iobj": 75,
338
+ "NOUN|_|mark": 76,
339
+ "NOUN|_|nmod": 77,
340
+ "NOUN|_|nmod:poss": 78,
341
+ "NOUN|_|nsubj": 79,
342
+ "NOUN|_|nsubj:pass": 80,
343
+ "NOUN|_|obj": 81,
344
+ "NOUN|_|obl": 82,
345
+ "NOUN|_|obl:poss": 83,
346
+ "NOUN|_|obl:tmod": 84,
347
+ "NOUN|_|parataxis": 85,
348
+ "NOUN|_|root": 86,
349
+ "NOUN|_|vocative": 87,
350
+ "NOUN|_|xcomp": 88,
351
+ "NUM|_|acl": 89,
352
+ "NUM|_|acl:relcl": 90,
353
+ "NUM|_|advmod": 91,
354
+ "NUM|_|appos": 92,
355
+ "NUM|_|ccomp": 93,
356
+ "NUM|_|clf": 94,
357
+ "NUM|_|conj": 95,
358
+ "NUM|_|flat:name": 96,
359
+ "NUM|_|nmod": 97,
360
+ "NUM|_|nsubj": 98,
361
+ "NUM|_|nummod": 99,
362
+ "NUM|_|obj": 100,
363
+ "NUM|_|obl": 101,
364
+ "NUM|_|obl:poss": 102,
365
+ "NUM|_|obl:tmod": 103,
366
+ "NUM|_|root": 104,
367
+ "NUM|_|xcomp": 105,
368
+ "PART|Aspect=Perf|aux": 106,
369
+ "PART|Aspect=Perf|xcomp": 107,
370
+ "PART|Aspect=Prog|aux": 108,
371
+ "PART|Polarity=Neg|advmod": 109,
372
+ "PART|PronType=Int|acl": 110,
373
+ "PART|PronType=Int|advmod": 111,
374
+ "PART|PronType=Int|discourse": 112,
375
+ "PART|PronType=Int|obj": 113,
376
+ "PART|PronType=Int|root": 114,
377
+ "PART|_|acl": 115,
378
+ "PART|_|advmod": 116,
379
+ "PART|_|aux": 117,
380
+ "PART|_|cc": 118,
381
+ "PART|_|cc:preconj": 119,
382
+ "PART|_|ccomp": 120,
383
+ "PART|_|clf": 121,
384
+ "PART|_|compound": 122,
385
+ "PART|_|compound:prt": 123,
386
+ "PART|_|conj": 124,
387
+ "PART|_|discourse": 125,
388
+ "PART|_|fixed": 126,
389
+ "PART|_|mark": 127,
390
+ "PART|_|nmod": 128,
391
+ "PART|_|nmod:poss": 129,
392
+ "PART|_|nsubj": 130,
393
+ "PART|_|obj": 131,
394
+ "PART|_|obl": 132,
395
+ "PART|_|root": 133,
396
+ "PRON|Person=1|compound": 134,
397
+ "PRON|Person=1|nmod:poss": 135,
398
+ "PRON|Person=1|nsubj": 136,
399
+ "PRON|Person=1|nsubj:pass": 137,
400
+ "PRON|Person=1|obj": 138,
401
+ "PRON|Person=1|obl": 139,
402
+ "PRON|Person=1|obl:poss": 140,
403
+ "PRON|Person=2|compound": 141,
404
+ "PRON|Person=2|nmod:poss": 142,
405
+ "PRON|Person=2|nsubj": 143,
406
+ "PRON|Person=2|obj": 144,
407
+ "PRON|Person=2|obl": 145,
408
+ "PRON|Person=3|advmod": 146,
409
+ "PRON|Person=3|appos": 147,
410
+ "PRON|Person=3|compound": 148,
411
+ "PRON|Person=3|conj": 149,
412
+ "PRON|Person=3|nmod": 150,
413
+ "PRON|Person=3|nmod:poss": 151,
414
+ "PRON|Person=3|nsubj": 152,
415
+ "PRON|Person=3|nsubj:pass": 153,
416
+ "PRON|Person=3|obj": 154,
417
+ "PRON|Person=3|obl": 155,
418
+ "PRON|Person=3|obl:poss": 156,
419
+ "PRON|Person=3|reparandum": 157,
420
+ "PRON|Person=3|xcomp": 158,
421
+ "PRON|PronType=Int|nsubj": 159,
422
+ "PRON|PronType=Int|obj": 160,
423
+ "PRON|PronType=Int|obl": 161,
424
+ "PRON|PronType=Int|root": 162,
425
+ "PRON|_|acl": 163,
426
+ "PRON|_|acl:relcl": 164,
427
+ "PRON|_|advcl": 165,
428
+ "PRON|_|advmod": 166,
429
+ "PRON|_|ccomp": 167,
430
+ "PRON|_|compound": 168,
431
+ "PRON|_|conj": 169,
432
+ "PRON|_|fixed": 170,
433
+ "PRON|_|nmod": 171,
434
+ "PRON|_|nmod:poss": 172,
435
+ "PRON|_|nsubj": 173,
436
+ "PRON|_|obj": 174,
437
+ "PRON|_|obl": 175,
438
+ "PRON|_|obl:poss": 176,
439
+ "PRON|_|root": 177,
440
+ "PRON|_|xcomp": 178,
441
+ "PROPN|_|acl": 179,
442
+ "PROPN|_|acl:relcl": 180,
443
+ "PROPN|_|advmod": 181,
444
+ "PROPN|_|appos": 182,
445
+ "PROPN|_|aux": 183,
446
+ "PROPN|_|cc": 184,
447
+ "PROPN|_|ccomp": 185,
448
+ "PROPN|_|clf": 186,
449
+ "PROPN|_|compound": 187,
450
+ "PROPN|_|conj": 188,
451
+ "PROPN|_|flat:name": 189,
452
+ "PROPN|_|goeswith": 190,
453
+ "PROPN|_|nmod": 191,
454
+ "PROPN|_|nmod:poss": 192,
455
+ "PROPN|_|nsubj": 193,
456
+ "PROPN|_|nsubj:pass": 194,
457
+ "PROPN|_|obj": 195,
458
+ "PROPN|_|obl": 196,
459
+ "PROPN|_|obl:poss": 197,
460
+ "PROPN|_|obl:tmod": 198,
461
+ "PROPN|_|root": 199,
462
+ "PROPN|_|xcomp": 200,
463
+ "PUNCT|_|advmod": 201,
464
+ "PUNCT|_|clf": 202,
465
+ "PUNCT|_|punct": 203,
466
+ "PUNCT|_|root": 204,
467
+ "SCONJ|_|mark": 205,
468
+ "SYM|_|advmod": 206,
469
+ "SYM|_|clf": 207,
470
+ "SYM|_|nsubj": 208,
471
+ "SYM|_|obj": 209,
472
+ "SYM|_|obl": 210,
473
+ "VERB|Mood=Imp|aux": 211,
474
+ "VERB|Mood=Imp|xcomp": 212,
475
+ "VERB|Voice=Pass|aux": 213,
476
+ "VERB|Voice=Pass|aux:pass": 214,
477
+ "VERB|_|acl": 215,
478
+ "VERB|_|acl:relcl": 216,
479
+ "VERB|_|advcl": 217,
480
+ "VERB|_|advmod": 218,
481
+ "VERB|_|appos": 219,
482
+ "VERB|_|aux": 220,
483
+ "VERB|_|case": 221,
484
+ "VERB|_|cc": 222,
485
+ "VERB|_|ccomp": 223,
486
+ "VERB|_|compound": 224,
487
+ "VERB|_|conj": 225,
488
+ "VERB|_|csubj": 226,
489
+ "VERB|_|fixed": 227,
490
+ "VERB|_|mark": 228,
491
+ "VERB|_|nmod": 229,
492
+ "VERB|_|nmod:poss": 230,
493
+ "VERB|_|nsubj": 231,
494
+ "VERB|_|obj": 232,
495
+ "VERB|_|obl": 233,
496
+ "VERB|_|obl:poss": 234,
497
+ "VERB|_|parataxis": 235,
498
+ "VERB|_|root": 236,
499
+ "VERB|_|xcomp": 237,
500
+ "X|_|goeswith": 238
501
+ },
502
+ "layer_norm_eps": 1e-05,
503
+ "mask_token_id": 4,
504
+ "max_position_embeddings": 514,
505
+ "model_type": "roberta",
506
+ "num_attention_heads": 12,
507
+ "num_hidden_layers": 12,
508
+ "pad_token_id": 1,
509
+ "position_embedding_type": "absolute",
510
+ "tokenizer_class": "BertTokenizerFast",
511
+ "torch_dtype": "float32",
512
+ "transformers_version": "4.22.1",
513
+ "type_vocab_size": 1,
514
+ "unk_token_id": 3,
515
+ "use_cache": true,
516
+ "vocab_size": 16582
517
+ }
maker.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/python3
2
+ src="KoichiYasuoka/roberta-base-thai-syllable"
3
+ tgt="KoichiYasuoka/roberta-base-thai-syllable-ud-goeswith"
4
+ url="https://github.com/KoichiYasuoka/spaCy-Thai"
5
+ import os
6
+ d=os.path.join(os.path.basename(url),"UD_Thai-Corpora")
7
+ os.system("test -d {} || git clone --depth=1 {}".format(d,url))
8
+ s='{if(NF>0)u=u$0"\\n";else{f=FILENAME;if(u~/\\t0\\troot\\t/)print u>(f~/-dev/?"dev":f~/-test/?"test":"train")".conllu";u=""}}'
9
+ os.system("nawk -F'\\t' '{}' {}/*-ud-*.conllu".format(s,d))
10
+ class UDgoeswithDataset(object):
11
+ def __init__(self,conllu,tokenizer):
12
+ self.ids,self.tags,label=[],[],set()
13
+ with open(conllu,"r",encoding="utf-8") as r:
14
+ cls,sep,msk=tokenizer.cls_token_id,tokenizer.sep_token_id,tokenizer.mask_token_id
15
+ dep,c="-|_|dep",[]
16
+ for s in r:
17
+ t=s.split("\t")
18
+ if len(t)==10 and t[0].isdecimal():
19
+ c.append(t)
20
+ elif c!=[]:
21
+ v=tokenizer([t[1] for t in c],add_special_tokens=False)["input_ids"]
22
+ for i in range(len(v)-1,-1,-1):
23
+ for j in range(1,len(v[i])):
24
+ c.insert(i+1,[c[i][0],"_","_","X","_","_",c[i][0],"goeswith","_","_"])
25
+ y=["0"]+[t[0] for t in c]
26
+ h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(c,1)]
27
+ p,v=[t[3]+"|"+t[5]+"|"+t[7] for t in c],sum(v,[])
28
+ self.ids.append([cls]+v+[sep])
29
+ self.tags.append([dep]+p+[dep])
30
+ label=set(sum([self.tags[-1],list(label)],[]))
31
+ for i,k in enumerate(v):
32
+ self.ids.append([cls]+v[0:i]+[msk]+v[i+1:]+[sep,k])
33
+ self.tags.append([dep]+[t if h[j]==i+1 else dep for j,t in enumerate(p)]+[dep,dep])
34
+ c=[]
35
+ self.label2id={l:i for i,l in enumerate(sorted(label))}
36
+ def __call__(*args):
37
+ label=set(sum([list(t.label2id) for t in args],[]))
38
+ lid={l:i for i,l in enumerate(sorted(label))}
39
+ for t in args:
40
+ t.label2id=lid
41
+ return lid
42
+ __len__=lambda self:len(self.ids)
43
+ __getitem__=lambda self,i:{"input_ids":self.ids[i],"labels":[self.label2id[t] for t in self.tags[i]]}
44
+ from transformers import AutoTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
45
+ tkz=AutoTokenizer.from_pretrained(src)
46
+ trainDS=UDgoeswithDataset("train.conllu",tkz)
47
+ devDS=UDgoeswithDataset("dev.conllu",tkz)
48
+ testDS=UDgoeswithDataset("test.conllu",tkz)
49
+ lid=trainDS(devDS,testDS)
50
+ cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()})
51
+ arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=32,output_dir="/tmp",overwrite_output_dir=True,save_total_limit=2,evaluation_strategy="epoch",learning_rate=5e-05,warmup_ratio=0.1)
52
+ trn=Trainer(args=arg,data_collator=DataCollatorForTokenClassification(tkz),model=AutoModelForTokenClassification.from_pretrained(src,config=cfg),train_dataset=trainDS,eval_dataset=devDS)
53
+ trn.train()
54
+ trn.save_model(tgt)
55
+ tkz.save_pretrained(tgt)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5614b6b4e9336d394455eb9e54aabddea48d6c140c8b665e0f8961d0d5e65dfc
3
+ size 393552177
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "<s>",
3
+ "mask_token": "<mask>",
4
+ "pad_token": "<pad>",
5
+ "sep_token": "</s>",
6
+ "unk_token": "<unk>"
7
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "<s>",
3
+ "do_basic_tokenize": true,
4
+ "do_lower_case": false,
5
+ "mask_token": "<mask>",
6
+ "model_max_length": 416,
7
+ "never_split": null,
8
+ "pad_token": "<pad>",
9
+ "sep_token": "</s>",
10
+ "strip_accents": false,
11
+ "tokenize_chinese_chars": true,
12
+ "tokenizer_class": "BertTokenizerFast",
13
+ "unk_token": "<unk>"
14
+ }
ud.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TokenClassificationPipeline
2
+
3
+ class UniversalDependenciesPipeline(TokenClassificationPipeline):
4
+ def _forward(self,model_input):
5
+ import torch
6
+ v=model_input["input_ids"][0].tolist()
7
+ with torch.no_grad():
8
+ e=self.model(input_ids=torch.tensor([v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[j] for i,j in enumerate(v[1:-1],1)]))
9
+ return {"logits":e.logits[:,1:-2,:],**model_input}
10
+ def postprocess(self,model_output,**kwargs):
11
+ import numpy
12
+ e=model_output["logits"].numpy()
13
+ r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
14
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
15
+ g=self.model.config.label2id["X|_|goeswith"]
16
+ r=numpy.tri(e.shape[0])
17
+ for i in range(e.shape[0]):
18
+ for j in range(i+2,e.shape[1]):
19
+ r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
20
+ e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
21
+ m,p=numpy.nanmax(e,axis=2),numpy.nanargmax(e,axis=2)
22
+ h=self.chu_liu_edmonds(m)
23
+ z=[i for i,j in enumerate(h) if i==j]
24
+ if len(z)>1:
25
+ k,h=z[numpy.nanargmax(m[z,z])],numpy.nanmin(m)-numpy.nanmax(m)
26
+ m[:,z]+=[[0 if j in z and (i!=j or i==k) else h for i in z] for j in range(m.shape[0])]
27
+ h=self.chu_liu_edmonds(m)
28
+ v=[(s,e) for s,e in model_output["offset_mapping"][0].tolist() if s<e]
29
+ q=[self.model.config.id2label[p[j,i]].split("|") for i,j in enumerate(h)]
30
+ g="aggregation_strategy" in kwargs and kwargs["aggregation_strategy"]!="none"
31
+ if g:
32
+ for i,j in reversed(list(enumerate(q[1:],1))):
33
+ if j[-1]=="goeswith" and set([t[-1] for t in q[h[i]+1:i+1]])=={"goeswith"}:
34
+ h=[b if i>b else b-1 for a,b in enumerate(h) if i!=a]
35
+ v[i-1]=(v[i-1][0],v.pop(i)[1])
36
+ q.pop(i)
37
+ t=model_output["sentence"].replace("\n"," ")
38
+ u="# text = "+t+"\n"
39
+ for i,(s,e) in enumerate(v):
40
+ u+="\t".join([str(i+1),t[s:e],t[s:e] if g else "_",q[i][0],"_","|".join(q[i][1:-1]),str(0 if h[i]==i else h[i]+1),q[i][-1],"_","_" if i+1<len(v) and e<v[i+1][0] else "SpaceAfter=No"])+"\n"
41
+ return u+"\n"
42
+ def chu_liu_edmonds(self,matrix):
43
+ import numpy
44
+ h=numpy.nanargmax(matrix,axis=0)
45
+ x=[-1 if i==j else j for i,j in enumerate(h)]
46
+ for b in [lambda x,i,j:-1 if i not in x else x[i],lambda x,i,j:-1 if j<0 else x[j]]:
47
+ y=[]
48
+ while x!=y:
49
+ y=list(x)
50
+ for i,j in enumerate(x):
51
+ x[i]=b(x,i,j)
52
+ if max(x)<0:
53
+ return h
54
+ y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
55
+ z=matrix-numpy.nanmax(matrix,axis=0)
56
+ m=numpy.block([[z[x,:][:,x],numpy.nanmax(z[x,:][:,y],axis=1).reshape(len(x),1)],[numpy.nanmax(z[y,:][:,x],axis=0),numpy.nanmax(z[y,y])]])
57
+ k=[j if i==len(x) else x[j] if j<len(x) else y[numpy.nanargmax(z[y,x[i]])] for i,j in enumerate(self.chu_liu_edmonds(m))]
58
+ h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
59
+ i=y[numpy.nanargmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
60
+ h[i]=x[k[-1]] if k[-1]<len(x) else i
61
+ return h
vocab.txt ADDED
The diff for this file is too large to render. See raw diff