KoichiYasuoka commited on
Commit
9fade74
1 Parent(s): afa9dc5

initial release

Browse files
Files changed (8) hide show
  1. README.md +76 -0
  2. config.json +775 -0
  3. maker.py +55 -0
  4. pytorch_model.bin +3 -0
  5. special_tokens_map.json +7 -0
  6. tokenizer_config.json +15 -0
  7. ud.py +60 -0
  8. vocab.txt +0 -0
README.md ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "vi"
4
+ tags:
5
+ - "vietnamese"
6
+ - "token-classification"
7
+ - "pos"
8
+ - "dependency-parsing"
9
+ datasets:
10
+ - "universal_dependencies"
11
+ license: "cc-by-sa-4.0"
12
+ pipeline_tag: "token-classification"
13
+ widget:
14
+ - text: "Hai cái đầu thì tốt hơn một."
15
+ ---
16
+
17
+ # bert-base-vietnamese-ud-goeswith
18
+
19
+ ## Model Description
20
+
21
+ This is a BERT model pre-trained on Vietnamese texts for POS-tagging and dependency-parsing (using `goeswith` for subwords), derived from [vibert-base-cased](https://huggingface.co/FPTAI/vibert-base-cased).
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/bert-base-vietnamese-ud-goeswith")
65
+ print(nlp("Hai cái đầu thì tốt hơn một."))
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/bert-base-vietnamese-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
74
+ print(nlp("Hai cái đầu thì tốt hơn một."))
75
+ ```
76
+
config.json ADDED
@@ -0,0 +1,775 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "custom_pipelines": {
8
+ "universal-dependencies": {
9
+ "impl": "ud.UniversalDependenciesPipeline"
10
+ }
11
+ },
12
+ "directionality": "bidi",
13
+ "hidden_act": "gelu",
14
+ "hidden_dropout_prob": 0.1,
15
+ "hidden_size": 768,
16
+ "id2label": {
17
+ "0": "-|_|dep",
18
+ "1": "ADJ|_|acl",
19
+ "2": "ADJ|_|acl:subj",
20
+ "3": "ADJ|_|acl:tmod",
21
+ "4": "ADJ|_|acl:tonp",
22
+ "5": "ADJ|_|advcl",
23
+ "6": "ADJ|_|advcl:objective",
24
+ "7": "ADJ|_|advmod",
25
+ "8": "ADJ|_|advmod:adj",
26
+ "9": "ADJ|_|advmod:neg",
27
+ "10": "ADJ|_|amod",
28
+ "11": "ADJ|_|appos",
29
+ "12": "ADJ|_|appos:nmod",
30
+ "13": "ADJ|_|ccomp",
31
+ "14": "ADJ|_|compound",
32
+ "15": "ADJ|_|compound:adj",
33
+ "16": "ADJ|_|compound:amod",
34
+ "17": "ADJ|_|compound:apr",
35
+ "18": "ADJ|_|compound:atov",
36
+ "19": "ADJ|_|compound:dir",
37
+ "20": "ADJ|_|compound:prt",
38
+ "21": "ADJ|_|compound:svc",
39
+ "22": "ADJ|_|compound:verbnoun",
40
+ "23": "ADJ|_|compound:vmod",
41
+ "24": "ADJ|_|conj",
42
+ "25": "ADJ|_|csubj",
43
+ "26": "ADJ|_|csubj:asubj",
44
+ "27": "ADJ|_|dep",
45
+ "28": "ADJ|_|discourse",
46
+ "29": "ADJ|_|dislocated",
47
+ "30": "ADJ|_|fixed",
48
+ "31": "ADJ|_|flat",
49
+ "32": "ADJ|_|flat:name",
50
+ "33": "ADJ|_|nmod",
51
+ "34": "ADJ|_|nsubj",
52
+ "35": "ADJ|_|obj",
53
+ "36": "ADJ|_|obl",
54
+ "37": "ADJ|_|obl:about",
55
+ "38": "ADJ|_|obl:adj",
56
+ "39": "ADJ|_|obl:comp",
57
+ "40": "ADJ|_|obl:tmod",
58
+ "41": "ADJ|_|obl:with",
59
+ "42": "ADJ|_|parataxis",
60
+ "43": "ADJ|_|root",
61
+ "44": "ADJ|_|xcomp",
62
+ "45": "ADJ|_|xcomp:adj",
63
+ "46": "ADP|_|acl:tmod",
64
+ "47": "ADP|_|advcl",
65
+ "48": "ADP|_|case",
66
+ "49": "ADP|_|cc",
67
+ "50": "ADP|_|ccomp",
68
+ "51": "ADP|_|compound",
69
+ "52": "ADP|_|compound:atov",
70
+ "53": "ADP|_|compound:dir",
71
+ "54": "ADP|_|compound:prt",
72
+ "55": "ADP|_|compound:svc",
73
+ "56": "ADP|_|conj",
74
+ "57": "ADP|_|csubj",
75
+ "58": "ADP|_|dep",
76
+ "59": "ADP|_|discourse",
77
+ "60": "ADP|_|fixed",
78
+ "61": "ADP|_|mark",
79
+ "62": "ADP|_|mark:pcomp",
80
+ "63": "ADP|_|nmod",
81
+ "64": "ADP|_|obl",
82
+ "65": "ADP|_|obl:tmod",
83
+ "66": "ADP|_|parataxis",
84
+ "67": "ADP|_|root",
85
+ "68": "ADP|_|xcomp",
86
+ "69": "ADV|_|acl:subj",
87
+ "70": "ADV|_|advcl",
88
+ "71": "ADV|_|advcl:objective",
89
+ "72": "ADV|_|advmod",
90
+ "73": "ADV|_|advmod:adj",
91
+ "74": "ADV|_|advmod:dir",
92
+ "75": "ADV|_|advmod:neg",
93
+ "76": "ADV|_|appos:nmod",
94
+ "77": "ADV|_|case",
95
+ "78": "ADV|_|compound",
96
+ "79": "ADV|_|compound:apr",
97
+ "80": "ADV|_|compound:atov",
98
+ "81": "ADV|_|compound:dir",
99
+ "82": "ADV|_|compound:prt",
100
+ "83": "ADV|_|compound:redup",
101
+ "84": "ADV|_|compound:svc",
102
+ "85": "ADV|_|conj",
103
+ "86": "ADV|_|discourse",
104
+ "87": "ADV|_|fixed",
105
+ "88": "ADV|_|flat:redup",
106
+ "89": "ADV|_|mark",
107
+ "90": "ADV|_|nmod",
108
+ "91": "ADV|_|obj",
109
+ "92": "ADV|_|obl",
110
+ "93": "ADV|_|obl:adv",
111
+ "94": "ADV|_|obl:tmod",
112
+ "95": "ADV|_|root",
113
+ "96": "ADV|_|xcomp",
114
+ "97": "AUX|_|aux",
115
+ "98": "AUX|_|aux:pass",
116
+ "99": "AUX|_|compound",
117
+ "100": "AUX|_|cop",
118
+ "101": "AUX|_|discourse",
119
+ "102": "AUX|_|parataxis",
120
+ "103": "AUX|_|root",
121
+ "104": "AUX|_|xcomp",
122
+ "105": "CCONJ|_|case",
123
+ "106": "CCONJ|_|cc",
124
+ "107": "CCONJ|_|flat",
125
+ "108": "CCONJ|_|mark",
126
+ "109": "DET|_|advmod:adj",
127
+ "110": "DET|_|clf:det",
128
+ "111": "DET|_|det",
129
+ "112": "DET|_|discourse",
130
+ "113": "DET|_|nmod:poss",
131
+ "114": "DET|_|nsubj",
132
+ "115": "DET|_|obj",
133
+ "116": "DET|_|obl:tmod",
134
+ "117": "INTJ|_|discourse",
135
+ "118": "INTJ|_|root",
136
+ "119": "NOUN|_|acl",
137
+ "120": "NOUN|_|acl:subj",
138
+ "121": "NOUN|_|acl:tmod",
139
+ "122": "NOUN|_|advcl",
140
+ "123": "NOUN|_|advcl:objective",
141
+ "124": "NOUN|_|amod",
142
+ "125": "NOUN|_|appos",
143
+ "126": "NOUN|_|appos:nmod",
144
+ "127": "NOUN|_|case",
145
+ "128": "NOUN|_|ccomp",
146
+ "129": "NOUN|_|clf",
147
+ "130": "NOUN|_|clf:det",
148
+ "131": "NOUN|_|compound",
149
+ "132": "NOUN|_|compound:amod",
150
+ "133": "NOUN|_|compound:dir",
151
+ "134": "NOUN|_|compound:verbnoun",
152
+ "135": "NOUN|_|compound:vmod",
153
+ "136": "NOUN|_|conj",
154
+ "137": "NOUN|_|csubj",
155
+ "138": "NOUN|_|csubj:pass",
156
+ "139": "NOUN|_|csubj:vsubj",
157
+ "140": "NOUN|_|dep",
158
+ "141": "NOUN|_|discourse",
159
+ "142": "NOUN|_|dislocated",
160
+ "143": "NOUN|_|fixed",
161
+ "144": "NOUN|_|flat",
162
+ "145": "NOUN|_|flat:name",
163
+ "146": "NOUN|_|flat:number",
164
+ "147": "NOUN|_|flat:time",
165
+ "148": "NOUN|_|iobj",
166
+ "149": "NOUN|_|list",
167
+ "150": "NOUN|_|nmod",
168
+ "151": "NOUN|_|nmod:poss",
169
+ "152": "NOUN|_|nsubj",
170
+ "153": "NOUN|_|nsubj:nn",
171
+ "154": "NOUN|_|nsubj:pass",
172
+ "155": "NOUN|_|nsubj:xsubj",
173
+ "156": "NOUN|_|nummod",
174
+ "157": "NOUN|_|obj",
175
+ "158": "NOUN|_|obl",
176
+ "159": "NOUN|_|obl:about",
177
+ "160": "NOUN|_|obl:adj",
178
+ "161": "NOUN|_|obl:adv",
179
+ "162": "NOUN|_|obl:agent",
180
+ "163": "NOUN|_|obl:comp",
181
+ "164": "NOUN|_|obl:iobj",
182
+ "165": "NOUN|_|obl:tmod",
183
+ "166": "NOUN|_|obl:with",
184
+ "167": "NOUN|_|parataxis",
185
+ "168": "NOUN|_|root",
186
+ "169": "NOUN|_|vocative",
187
+ "170": "NOUN|_|xcomp",
188
+ "171": "NUM|_|amod",
189
+ "172": "NUM|_|appos",
190
+ "173": "NUM|_|appos:nmod",
191
+ "174": "NUM|_|clf",
192
+ "175": "NUM|_|clf:det",
193
+ "176": "NUM|_|compound",
194
+ "177": "NUM|_|compound:verbnoun",
195
+ "178": "NUM|_|conj",
196
+ "179": "NUM|_|flat:date",
197
+ "180": "NUM|_|flat:name",
198
+ "181": "NUM|_|flat:number",
199
+ "182": "NUM|_|flat:time",
200
+ "183": "NUM|_|nmod",
201
+ "184": "NUM|_|nsubj",
202
+ "185": "NUM|_|nummod",
203
+ "186": "NUM|_|obj",
204
+ "187": "NUM|_|obl",
205
+ "188": "NUM|_|obl:comp",
206
+ "189": "NUM|_|obl:tmod",
207
+ "190": "NUM|_|parataxis",
208
+ "191": "NUM|_|root",
209
+ "192": "PART|_|advcl",
210
+ "193": "PART|_|advmod",
211
+ "194": "PART|_|amod",
212
+ "195": "PART|_|case",
213
+ "196": "PART|_|clf:det",
214
+ "197": "PART|_|compound",
215
+ "198": "PART|_|compound:prt",
216
+ "199": "PART|_|discourse",
217
+ "200": "PART|_|fixed",
218
+ "201": "PART|_|mark",
219
+ "202": "PART|_|obl",
220
+ "203": "PART|_|parataxis",
221
+ "204": "PRON|_|acl:tmod",
222
+ "205": "PRON|_|advcl",
223
+ "206": "PRON|_|appos:nmod",
224
+ "207": "PRON|_|ccomp",
225
+ "208": "PRON|_|compound",
226
+ "209": "PRON|_|compound:pron",
227
+ "210": "PRON|_|compound:prt",
228
+ "211": "PRON|_|conj",
229
+ "212": "PRON|_|det",
230
+ "213": "PRON|_|det:pmod",
231
+ "214": "PRON|_|discourse",
232
+ "215": "PRON|_|expl",
233
+ "216": "PRON|_|fixed",
234
+ "217": "PRON|_|iobj",
235
+ "218": "PRON|_|nmod",
236
+ "219": "PRON|_|nmod:poss",
237
+ "220": "PRON|_|nsubj",
238
+ "221": "PRON|_|nsubj:nn",
239
+ "222": "PRON|_|nsubj:pass",
240
+ "223": "PRON|_|nsubj:xsubj",
241
+ "224": "PRON|_|obj",
242
+ "225": "PRON|_|obl",
243
+ "226": "PRON|_|obl:about",
244
+ "227": "PRON|_|obl:adj",
245
+ "228": "PRON|_|obl:comp",
246
+ "229": "PRON|_|obl:iobj",
247
+ "230": "PRON|_|obl:tmod",
248
+ "231": "PRON|_|obl:with",
249
+ "232": "PRON|_|parataxis",
250
+ "233": "PRON|_|root",
251
+ "234": "PROPN|_|acl:subj",
252
+ "235": "PROPN|_|advcl",
253
+ "236": "PROPN|_|appos",
254
+ "237": "PROPN|_|appos:nmod",
255
+ "238": "PROPN|_|ccomp",
256
+ "239": "PROPN|_|compound",
257
+ "240": "PROPN|_|compound:verbnoun",
258
+ "241": "PROPN|_|conj",
259
+ "242": "PROPN|_|csubj:pass",
260
+ "243": "PROPN|_|dep",
261
+ "244": "PROPN|_|flat",
262
+ "245": "PROPN|_|flat:name",
263
+ "246": "PROPN|_|iobj",
264
+ "247": "PROPN|_|list",
265
+ "248": "PROPN|_|nmod",
266
+ "249": "PROPN|_|nmod:poss",
267
+ "250": "PROPN|_|nsubj",
268
+ "251": "PROPN|_|nsubj:nn",
269
+ "252": "PROPN|_|nsubj:pass",
270
+ "253": "PROPN|_|nsubj:xsubj",
271
+ "254": "PROPN|_|obj",
272
+ "255": "PROPN|_|obl",
273
+ "256": "PROPN|_|obl:agent",
274
+ "257": "PROPN|_|obl:comp",
275
+ "258": "PROPN|_|obl:iobj",
276
+ "259": "PROPN|_|obl:with",
277
+ "260": "PROPN|_|parataxis",
278
+ "261": "PROPN|_|root",
279
+ "262": "PROPN|_|vocative",
280
+ "263": "PUNCT|_|punct",
281
+ "264": "SCONJ|_|advcl",
282
+ "265": "SCONJ|_|case",
283
+ "266": "SCONJ|_|cc",
284
+ "267": "SCONJ|_|compound",
285
+ "268": "SCONJ|_|compound:svc",
286
+ "269": "SCONJ|_|discourse",
287
+ "270": "SCONJ|_|fixed",
288
+ "271": "SCONJ|_|mark",
289
+ "272": "SCONJ|_|obl",
290
+ "273": "SCONJ|_|parataxis",
291
+ "274": "SCONJ|_|root",
292
+ "275": "SCONJ|_|vocative",
293
+ "276": "SYM|_|advcl",
294
+ "277": "SYM|_|appos:nmod",
295
+ "278": "SYM|_|compound",
296
+ "279": "SYM|_|compound:z",
297
+ "280": "SYM|_|discourse",
298
+ "281": "SYM|_|flat",
299
+ "282": "SYM|_|flat:date",
300
+ "283": "SYM|_|flat:name",
301
+ "284": "SYM|_|flat:number",
302
+ "285": "SYM|_|flat:time",
303
+ "286": "SYM|_|nmod",
304
+ "287": "SYM|_|nsubj",
305
+ "288": "SYM|_|obj",
306
+ "289": "VERB|_|acl",
307
+ "290": "VERB|_|acl:relcl",
308
+ "291": "VERB|_|acl:subj",
309
+ "292": "VERB|_|acl:tmod",
310
+ "293": "VERB|_|acl:tonp",
311
+ "294": "VERB|_|advcl",
312
+ "295": "VERB|_|advcl:objective",
313
+ "296": "VERB|_|advmod",
314
+ "297": "VERB|_|amod",
315
+ "298": "VERB|_|appos",
316
+ "299": "VERB|_|appos:nmod",
317
+ "300": "VERB|_|case",
318
+ "301": "VERB|_|ccomp",
319
+ "302": "VERB|_|compound",
320
+ "303": "VERB|_|compound:amod",
321
+ "304": "VERB|_|compound:atov",
322
+ "305": "VERB|_|compound:dir",
323
+ "306": "VERB|_|compound:prt",
324
+ "307": "VERB|_|compound:redup",
325
+ "308": "VERB|_|compound:svc",
326
+ "309": "VERB|_|compound:verbnoun",
327
+ "310": "VERB|_|compound:vmod",
328
+ "311": "VERB|_|conj",
329
+ "312": "VERB|_|csubj",
330
+ "313": "VERB|_|csubj:pass",
331
+ "314": "VERB|_|csubj:vsubj",
332
+ "315": "VERB|_|discourse",
333
+ "316": "VERB|_|fixed",
334
+ "317": "VERB|_|flat:redup",
335
+ "318": "VERB|_|iobj",
336
+ "319": "VERB|_|mark",
337
+ "320": "VERB|_|mark:pcomp",
338
+ "321": "VERB|_|nmod",
339
+ "322": "VERB|_|nmod:poss",
340
+ "323": "VERB|_|nsubj",
341
+ "324": "VERB|_|nsubj:pass",
342
+ "325": "VERB|_|nsubj:xsubj",
343
+ "326": "VERB|_|obj",
344
+ "327": "VERB|_|obl",
345
+ "328": "VERB|_|obl:about",
346
+ "329": "VERB|_|obl:comp",
347
+ "330": "VERB|_|obl:iobj",
348
+ "331": "VERB|_|obl:tmod",
349
+ "332": "VERB|_|parataxis",
350
+ "333": "VERB|_|root",
351
+ "334": "VERB|_|vocative",
352
+ "335": "VERB|_|xcomp",
353
+ "336": "VERB|_|xcomp:adj",
354
+ "337": "VERB|_|xcomp:vcomp",
355
+ "338": "X|_|acl",
356
+ "339": "X|_|acl:subj",
357
+ "340": "X|_|acl:tonp",
358
+ "341": "X|_|advcl",
359
+ "342": "X|_|amod",
360
+ "343": "X|_|case",
361
+ "344": "X|_|cc",
362
+ "345": "X|_|ccomp",
363
+ "346": "X|_|compound",
364
+ "347": "X|_|compound:adj",
365
+ "348": "X|_|compound:prt",
366
+ "349": "X|_|compound:vmod",
367
+ "350": "X|_|compound:z",
368
+ "351": "X|_|conj",
369
+ "352": "X|_|discourse",
370
+ "353": "X|_|dislocated",
371
+ "354": "X|_|goeswith",
372
+ "355": "X|_|mark",
373
+ "356": "X|_|nmod",
374
+ "357": "X|_|nmod:poss",
375
+ "358": "X|_|nsubj",
376
+ "359": "X|_|obj",
377
+ "360": "X|_|obl",
378
+ "361": "X|_|obl:about",
379
+ "362": "X|_|obl:comp",
380
+ "363": "X|_|obl:tmod",
381
+ "364": "X|_|parataxis",
382
+ "365": "X|_|root",
383
+ "366": "X|_|xcomp"
384
+ },
385
+ "initializer_range": 0.02,
386
+ "intermediate_size": 3072,
387
+ "label2id": {
388
+ "-|_|dep": 0,
389
+ "ADJ|_|acl": 1,
390
+ "ADJ|_|acl:subj": 2,
391
+ "ADJ|_|acl:tmod": 3,
392
+ "ADJ|_|acl:tonp": 4,
393
+ "ADJ|_|advcl": 5,
394
+ "ADJ|_|advcl:objective": 6,
395
+ "ADJ|_|advmod": 7,
396
+ "ADJ|_|advmod:adj": 8,
397
+ "ADJ|_|advmod:neg": 9,
398
+ "ADJ|_|amod": 10,
399
+ "ADJ|_|appos": 11,
400
+ "ADJ|_|appos:nmod": 12,
401
+ "ADJ|_|ccomp": 13,
402
+ "ADJ|_|compound": 14,
403
+ "ADJ|_|compound:adj": 15,
404
+ "ADJ|_|compound:amod": 16,
405
+ "ADJ|_|compound:apr": 17,
406
+ "ADJ|_|compound:atov": 18,
407
+ "ADJ|_|compound:dir": 19,
408
+ "ADJ|_|compound:prt": 20,
409
+ "ADJ|_|compound:svc": 21,
410
+ "ADJ|_|compound:verbnoun": 22,
411
+ "ADJ|_|compound:vmod": 23,
412
+ "ADJ|_|conj": 24,
413
+ "ADJ|_|csubj": 25,
414
+ "ADJ|_|csubj:asubj": 26,
415
+ "ADJ|_|dep": 27,
416
+ "ADJ|_|discourse": 28,
417
+ "ADJ|_|dislocated": 29,
418
+ "ADJ|_|fixed": 30,
419
+ "ADJ|_|flat": 31,
420
+ "ADJ|_|flat:name": 32,
421
+ "ADJ|_|nmod": 33,
422
+ "ADJ|_|nsubj": 34,
423
+ "ADJ|_|obj": 35,
424
+ "ADJ|_|obl": 36,
425
+ "ADJ|_|obl:about": 37,
426
+ "ADJ|_|obl:adj": 38,
427
+ "ADJ|_|obl:comp": 39,
428
+ "ADJ|_|obl:tmod": 40,
429
+ "ADJ|_|obl:with": 41,
430
+ "ADJ|_|parataxis": 42,
431
+ "ADJ|_|root": 43,
432
+ "ADJ|_|xcomp": 44,
433
+ "ADJ|_|xcomp:adj": 45,
434
+ "ADP|_|acl:tmod": 46,
435
+ "ADP|_|advcl": 47,
436
+ "ADP|_|case": 48,
437
+ "ADP|_|cc": 49,
438
+ "ADP|_|ccomp": 50,
439
+ "ADP|_|compound": 51,
440
+ "ADP|_|compound:atov": 52,
441
+ "ADP|_|compound:dir": 53,
442
+ "ADP|_|compound:prt": 54,
443
+ "ADP|_|compound:svc": 55,
444
+ "ADP|_|conj": 56,
445
+ "ADP|_|csubj": 57,
446
+ "ADP|_|dep": 58,
447
+ "ADP|_|discourse": 59,
448
+ "ADP|_|fixed": 60,
449
+ "ADP|_|mark": 61,
450
+ "ADP|_|mark:pcomp": 62,
451
+ "ADP|_|nmod": 63,
452
+ "ADP|_|obl": 64,
453
+ "ADP|_|obl:tmod": 65,
454
+ "ADP|_|parataxis": 66,
455
+ "ADP|_|root": 67,
456
+ "ADP|_|xcomp": 68,
457
+ "ADV|_|acl:subj": 69,
458
+ "ADV|_|advcl": 70,
459
+ "ADV|_|advcl:objective": 71,
460
+ "ADV|_|advmod": 72,
461
+ "ADV|_|advmod:adj": 73,
462
+ "ADV|_|advmod:dir": 74,
463
+ "ADV|_|advmod:neg": 75,
464
+ "ADV|_|appos:nmod": 76,
465
+ "ADV|_|case": 77,
466
+ "ADV|_|compound": 78,
467
+ "ADV|_|compound:apr": 79,
468
+ "ADV|_|compound:atov": 80,
469
+ "ADV|_|compound:dir": 81,
470
+ "ADV|_|compound:prt": 82,
471
+ "ADV|_|compound:redup": 83,
472
+ "ADV|_|compound:svc": 84,
473
+ "ADV|_|conj": 85,
474
+ "ADV|_|discourse": 86,
475
+ "ADV|_|fixed": 87,
476
+ "ADV|_|flat:redup": 88,
477
+ "ADV|_|mark": 89,
478
+ "ADV|_|nmod": 90,
479
+ "ADV|_|obj": 91,
480
+ "ADV|_|obl": 92,
481
+ "ADV|_|obl:adv": 93,
482
+ "ADV|_|obl:tmod": 94,
483
+ "ADV|_|root": 95,
484
+ "ADV|_|xcomp": 96,
485
+ "AUX|_|aux": 97,
486
+ "AUX|_|aux:pass": 98,
487
+ "AUX|_|compound": 99,
488
+ "AUX|_|cop": 100,
489
+ "AUX|_|discourse": 101,
490
+ "AUX|_|parataxis": 102,
491
+ "AUX|_|root": 103,
492
+ "AUX|_|xcomp": 104,
493
+ "CCONJ|_|case": 105,
494
+ "CCONJ|_|cc": 106,
495
+ "CCONJ|_|flat": 107,
496
+ "CCONJ|_|mark": 108,
497
+ "DET|_|advmod:adj": 109,
498
+ "DET|_|clf:det": 110,
499
+ "DET|_|det": 111,
500
+ "DET|_|discourse": 112,
501
+ "DET|_|nmod:poss": 113,
502
+ "DET|_|nsubj": 114,
503
+ "DET|_|obj": 115,
504
+ "DET|_|obl:tmod": 116,
505
+ "INTJ|_|discourse": 117,
506
+ "INTJ|_|root": 118,
507
+ "NOUN|_|acl": 119,
508
+ "NOUN|_|acl:subj": 120,
509
+ "NOUN|_|acl:tmod": 121,
510
+ "NOUN|_|advcl": 122,
511
+ "NOUN|_|advcl:objective": 123,
512
+ "NOUN|_|amod": 124,
513
+ "NOUN|_|appos": 125,
514
+ "NOUN|_|appos:nmod": 126,
515
+ "NOUN|_|case": 127,
516
+ "NOUN|_|ccomp": 128,
517
+ "NOUN|_|clf": 129,
518
+ "NOUN|_|clf:det": 130,
519
+ "NOUN|_|compound": 131,
520
+ "NOUN|_|compound:amod": 132,
521
+ "NOUN|_|compound:dir": 133,
522
+ "NOUN|_|compound:verbnoun": 134,
523
+ "NOUN|_|compound:vmod": 135,
524
+ "NOUN|_|conj": 136,
525
+ "NOUN|_|csubj": 137,
526
+ "NOUN|_|csubj:pass": 138,
527
+ "NOUN|_|csubj:vsubj": 139,
528
+ "NOUN|_|dep": 140,
529
+ "NOUN|_|discourse": 141,
530
+ "NOUN|_|dislocated": 142,
531
+ "NOUN|_|fixed": 143,
532
+ "NOUN|_|flat": 144,
533
+ "NOUN|_|flat:name": 145,
534
+ "NOUN|_|flat:number": 146,
535
+ "NOUN|_|flat:time": 147,
536
+ "NOUN|_|iobj": 148,
537
+ "NOUN|_|list": 149,
538
+ "NOUN|_|nmod": 150,
539
+ "NOUN|_|nmod:poss": 151,
540
+ "NOUN|_|nsubj": 152,
541
+ "NOUN|_|nsubj:nn": 153,
542
+ "NOUN|_|nsubj:pass": 154,
543
+ "NOUN|_|nsubj:xsubj": 155,
544
+ "NOUN|_|nummod": 156,
545
+ "NOUN|_|obj": 157,
546
+ "NOUN|_|obl": 158,
547
+ "NOUN|_|obl:about": 159,
548
+ "NOUN|_|obl:adj": 160,
549
+ "NOUN|_|obl:adv": 161,
550
+ "NOUN|_|obl:agent": 162,
551
+ "NOUN|_|obl:comp": 163,
552
+ "NOUN|_|obl:iobj": 164,
553
+ "NOUN|_|obl:tmod": 165,
554
+ "NOUN|_|obl:with": 166,
555
+ "NOUN|_|parataxis": 167,
556
+ "NOUN|_|root": 168,
557
+ "NOUN|_|vocative": 169,
558
+ "NOUN|_|xcomp": 170,
559
+ "NUM|_|amod": 171,
560
+ "NUM|_|appos": 172,
561
+ "NUM|_|appos:nmod": 173,
562
+ "NUM|_|clf": 174,
563
+ "NUM|_|clf:det": 175,
564
+ "NUM|_|compound": 176,
565
+ "NUM|_|compound:verbnoun": 177,
566
+ "NUM|_|conj": 178,
567
+ "NUM|_|flat:date": 179,
568
+ "NUM|_|flat:name": 180,
569
+ "NUM|_|flat:number": 181,
570
+ "NUM|_|flat:time": 182,
571
+ "NUM|_|nmod": 183,
572
+ "NUM|_|nsubj": 184,
573
+ "NUM|_|nummod": 185,
574
+ "NUM|_|obj": 186,
575
+ "NUM|_|obl": 187,
576
+ "NUM|_|obl:comp": 188,
577
+ "NUM|_|obl:tmod": 189,
578
+ "NUM|_|parataxis": 190,
579
+ "NUM|_|root": 191,
580
+ "PART|_|advcl": 192,
581
+ "PART|_|advmod": 193,
582
+ "PART|_|amod": 194,
583
+ "PART|_|case": 195,
584
+ "PART|_|clf:det": 196,
585
+ "PART|_|compound": 197,
586
+ "PART|_|compound:prt": 198,
587
+ "PART|_|discourse": 199,
588
+ "PART|_|fixed": 200,
589
+ "PART|_|mark": 201,
590
+ "PART|_|obl": 202,
591
+ "PART|_|parataxis": 203,
592
+ "PRON|_|acl:tmod": 204,
593
+ "PRON|_|advcl": 205,
594
+ "PRON|_|appos:nmod": 206,
595
+ "PRON|_|ccomp": 207,
596
+ "PRON|_|compound": 208,
597
+ "PRON|_|compound:pron": 209,
598
+ "PRON|_|compound:prt": 210,
599
+ "PRON|_|conj": 211,
600
+ "PRON|_|det": 212,
601
+ "PRON|_|det:pmod": 213,
602
+ "PRON|_|discourse": 214,
603
+ "PRON|_|expl": 215,
604
+ "PRON|_|fixed": 216,
605
+ "PRON|_|iobj": 217,
606
+ "PRON|_|nmod": 218,
607
+ "PRON|_|nmod:poss": 219,
608
+ "PRON|_|nsubj": 220,
609
+ "PRON|_|nsubj:nn": 221,
610
+ "PRON|_|nsubj:pass": 222,
611
+ "PRON|_|nsubj:xsubj": 223,
612
+ "PRON|_|obj": 224,
613
+ "PRON|_|obl": 225,
614
+ "PRON|_|obl:about": 226,
615
+ "PRON|_|obl:adj": 227,
616
+ "PRON|_|obl:comp": 228,
617
+ "PRON|_|obl:iobj": 229,
618
+ "PRON|_|obl:tmod": 230,
619
+ "PRON|_|obl:with": 231,
620
+ "PRON|_|parataxis": 232,
621
+ "PRON|_|root": 233,
622
+ "PROPN|_|acl:subj": 234,
623
+ "PROPN|_|advcl": 235,
624
+ "PROPN|_|appos": 236,
625
+ "PROPN|_|appos:nmod": 237,
626
+ "PROPN|_|ccomp": 238,
627
+ "PROPN|_|compound": 239,
628
+ "PROPN|_|compound:verbnoun": 240,
629
+ "PROPN|_|conj": 241,
630
+ "PROPN|_|csubj:pass": 242,
631
+ "PROPN|_|dep": 243,
632
+ "PROPN|_|flat": 244,
633
+ "PROPN|_|flat:name": 245,
634
+ "PROPN|_|iobj": 246,
635
+ "PROPN|_|list": 247,
636
+ "PROPN|_|nmod": 248,
637
+ "PROPN|_|nmod:poss": 249,
638
+ "PROPN|_|nsubj": 250,
639
+ "PROPN|_|nsubj:nn": 251,
640
+ "PROPN|_|nsubj:pass": 252,
641
+ "PROPN|_|nsubj:xsubj": 253,
642
+ "PROPN|_|obj": 254,
643
+ "PROPN|_|obl": 255,
644
+ "PROPN|_|obl:agent": 256,
645
+ "PROPN|_|obl:comp": 257,
646
+ "PROPN|_|obl:iobj": 258,
647
+ "PROPN|_|obl:with": 259,
648
+ "PROPN|_|parataxis": 260,
649
+ "PROPN|_|root": 261,
650
+ "PROPN|_|vocative": 262,
651
+ "PUNCT|_|punct": 263,
652
+ "SCONJ|_|advcl": 264,
653
+ "SCONJ|_|case": 265,
654
+ "SCONJ|_|cc": 266,
655
+ "SCONJ|_|compound": 267,
656
+ "SCONJ|_|compound:svc": 268,
657
+ "SCONJ|_|discourse": 269,
658
+ "SCONJ|_|fixed": 270,
659
+ "SCONJ|_|mark": 271,
660
+ "SCONJ|_|obl": 272,
661
+ "SCONJ|_|parataxis": 273,
662
+ "SCONJ|_|root": 274,
663
+ "SCONJ|_|vocative": 275,
664
+ "SYM|_|advcl": 276,
665
+ "SYM|_|appos:nmod": 277,
666
+ "SYM|_|compound": 278,
667
+ "SYM|_|compound:z": 279,
668
+ "SYM|_|discourse": 280,
669
+ "SYM|_|flat": 281,
670
+ "SYM|_|flat:date": 282,
671
+ "SYM|_|flat:name": 283,
672
+ "SYM|_|flat:number": 284,
673
+ "SYM|_|flat:time": 285,
674
+ "SYM|_|nmod": 286,
675
+ "SYM|_|nsubj": 287,
676
+ "SYM|_|obj": 288,
677
+ "VERB|_|acl": 289,
678
+ "VERB|_|acl:relcl": 290,
679
+ "VERB|_|acl:subj": 291,
680
+ "VERB|_|acl:tmod": 292,
681
+ "VERB|_|acl:tonp": 293,
682
+ "VERB|_|advcl": 294,
683
+ "VERB|_|advcl:objective": 295,
684
+ "VERB|_|advmod": 296,
685
+ "VERB|_|amod": 297,
686
+ "VERB|_|appos": 298,
687
+ "VERB|_|appos:nmod": 299,
688
+ "VERB|_|case": 300,
689
+ "VERB|_|ccomp": 301,
690
+ "VERB|_|compound": 302,
691
+ "VERB|_|compound:amod": 303,
692
+ "VERB|_|compound:atov": 304,
693
+ "VERB|_|compound:dir": 305,
694
+ "VERB|_|compound:prt": 306,
695
+ "VERB|_|compound:redup": 307,
696
+ "VERB|_|compound:svc": 308,
697
+ "VERB|_|compound:verbnoun": 309,
698
+ "VERB|_|compound:vmod": 310,
699
+ "VERB|_|conj": 311,
700
+ "VERB|_|csubj": 312,
701
+ "VERB|_|csubj:pass": 313,
702
+ "VERB|_|csubj:vsubj": 314,
703
+ "VERB|_|discourse": 315,
704
+ "VERB|_|fixed": 316,
705
+ "VERB|_|flat:redup": 317,
706
+ "VERB|_|iobj": 318,
707
+ "VERB|_|mark": 319,
708
+ "VERB|_|mark:pcomp": 320,
709
+ "VERB|_|nmod": 321,
710
+ "VERB|_|nmod:poss": 322,
711
+ "VERB|_|nsubj": 323,
712
+ "VERB|_|nsubj:pass": 324,
713
+ "VERB|_|nsubj:xsubj": 325,
714
+ "VERB|_|obj": 326,
715
+ "VERB|_|obl": 327,
716
+ "VERB|_|obl:about": 328,
717
+ "VERB|_|obl:comp": 329,
718
+ "VERB|_|obl:iobj": 330,
719
+ "VERB|_|obl:tmod": 331,
720
+ "VERB|_|parataxis": 332,
721
+ "VERB|_|root": 333,
722
+ "VERB|_|vocative": 334,
723
+ "VERB|_|xcomp": 335,
724
+ "VERB|_|xcomp:adj": 336,
725
+ "VERB|_|xcomp:vcomp": 337,
726
+ "X|_|acl": 338,
727
+ "X|_|acl:subj": 339,
728
+ "X|_|acl:tonp": 340,
729
+ "X|_|advcl": 341,
730
+ "X|_|amod": 342,
731
+ "X|_|case": 343,
732
+ "X|_|cc": 344,
733
+ "X|_|ccomp": 345,
734
+ "X|_|compound": 346,
735
+ "X|_|compound:adj": 347,
736
+ "X|_|compound:prt": 348,
737
+ "X|_|compound:vmod": 349,
738
+ "X|_|compound:z": 350,
739
+ "X|_|conj": 351,
740
+ "X|_|discourse": 352,
741
+ "X|_|dislocated": 353,
742
+ "X|_|goeswith": 354,
743
+ "X|_|mark": 355,
744
+ "X|_|nmod": 356,
745
+ "X|_|nmod:poss": 357,
746
+ "X|_|nsubj": 358,
747
+ "X|_|obj": 359,
748
+ "X|_|obl": 360,
749
+ "X|_|obl:about": 361,
750
+ "X|_|obl:comp": 362,
751
+ "X|_|obl:tmod": 363,
752
+ "X|_|parataxis": 364,
753
+ "X|_|root": 365,
754
+ "X|_|xcomp": 366
755
+ },
756
+ "layer_norm_eps": 1e-12,
757
+ "max_position_embeddings": 512,
758
+ "model_type": "bert",
759
+ "num_attention_heads": 12,
760
+ "num_hidden_layers": 12,
761
+ "output_past": true,
762
+ "pad_token_id": 0,
763
+ "pooler_fc_size": 768,
764
+ "pooler_num_attention_heads": 12,
765
+ "pooler_num_fc_layers": 3,
766
+ "pooler_size_per_head": 128,
767
+ "pooler_type": "first_token_transform",
768
+ "position_embedding_type": "absolute",
769
+ "tokenizer_class": "BertTokenizer",
770
+ "torch_dtype": "float32",
771
+ "transformers_version": "4.22.1",
772
+ "type_vocab_size": 2,
773
+ "use_cache": true,
774
+ "vocab_size": 38168
775
+ }
maker.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/python3
2
+ src="FPTAI/vibert-base-cased"
3
+ tgt="KoichiYasuoka/bert-base-vietnamese-ud-goeswith"
4
+ import os
5
+ url="https://github.com/UniversalDependencies/UD_Vietnamese-VTB"
6
+ d=os.path.basename(url)
7
+ os.system("test -d "+d+" || git clone --depth=1 "+url)
8
+ os.system("for F in train dev test ; do cp "+d+"/*-$F.conllu $F.conllu ; done")
9
+ class UDgoeswithDataset(object):
10
+ def __init__(self,conllu,tokenizer):
11
+ self.ids,self.tags,label=[],[],set()
12
+ with open(conllu,"r",encoding="utf-8") as r:
13
+ cls,sep,msk=tokenizer.cls_token_id,tokenizer.sep_token_id,tokenizer.mask_token_id
14
+ dep,c="-|_|dep",[]
15
+ for s in r:
16
+ t=s.split("\t")
17
+ if len(t)==10 and t[0].isdecimal():
18
+ c.append(t)
19
+ elif c!=[]:
20
+ v=tokenizer([t[1] for t in c],add_special_tokens=False)["input_ids"]
21
+ for i in range(len(v)-1,-1,-1):
22
+ for j in range(1,len(v[i])):
23
+ c.insert(i+1,[c[i][0],"_","_","X","_","_",c[i][0],"goeswith","_","_"])
24
+ y=["0"]+[t[0] for t in c]
25
+ h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(c,1)]
26
+ p,v=[t[3]+"|"+t[5]+"|"+t[7] for t in c],sum(v,[])
27
+ if len(v)<tokenizer.model_max_length-3:
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 BertTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
45
+ tkz=BertTokenizer.from_pretrained(src,do_lower_case=False,strip_accents=False,model_max_length=512)
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:e211e589a5b4e4388d9d77ae598125e3b9bb176ddeba9b9a4cdc9c6decfd68b5
3
+ size 460254385
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "do_basic_tokenize": true,
4
+ "do_lower_case": false,
5
+ "mask_token": "[MASK]",
6
+ "model_max_length": 512,
7
+ "never_split": null,
8
+ "pad_token": "[PAD]",
9
+ "sep_token": "[SEP]",
10
+ "special_tokens_map_file": null,
11
+ "strip_accents": false,
12
+ "tokenize_chinese_chars": true,
13
+ "tokenizer_class": "BertTokenizer",
14
+ "unk_token": "[UNK]"
15
+ }
ud.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ if "aggregation_strategy" in kwargs and kwargs["aggregation_strategy"]!="none":
31
+ for i,j in reversed(list(enumerate(q[1:],1))):
32
+ if j[-1]=="goeswith" and set([t[-1] for t in q[h[i]+1:i+1]])=={"goeswith"}:
33
+ h=[b if i>b else b-1 for a,b in enumerate(h) if i!=a]
34
+ v[i-1]=(v[i-1][0],v.pop(i)[1])
35
+ q.pop(i)
36
+ t=model_output["sentence"].replace("\n"," ")
37
+ u="# text = "+t+"\n"
38
+ for i,(s,e) in enumerate(v):
39
+ u+="\t".join([str(i+1),t[s:e],"_",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"
40
+ return u+"\n"
41
+ def chu_liu_edmonds(self,matrix):
42
+ import numpy
43
+ h=numpy.nanargmax(matrix,axis=0)
44
+ x=[-1 if i==j else j for i,j in enumerate(h)]
45
+ 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]]:
46
+ y=[]
47
+ while x!=y:
48
+ y=list(x)
49
+ for i,j in enumerate(x):
50
+ x[i]=b(x,i,j)
51
+ if max(x)<0:
52
+ return h
53
+ y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
54
+ z=matrix-numpy.nanmax(matrix,axis=0)
55
+ 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])]])
56
+ 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))]
57
+ h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
58
+ i=y[numpy.nanargmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
59
+ h[i]=x[k[-1]] if k[-1]<len(x) else i
60
+ return h
vocab.txt ADDED
The diff for this file is too large to render. See raw diff