KoichiYasuoka commited on
Commit
0359194
1 Parent(s): d2b89c4

initial release

Browse files
README.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "ja"
4
+ tags:
5
+ - "japanese"
6
+ - "wikipedia"
7
+ - "cc100"
8
+ - "pos"
9
+ - "dependency-parsing"
10
+ datasets:
11
+ - "universal_dependencies"
12
+ license: "cc-by-sa-4.0"
13
+ pipeline_tag: "token-classification"
14
+ ---
15
+
16
+ # roberta-base-japanese-juman-ud-goeswith
17
+
18
+ ## Model Description
19
+
20
+ This is a RoBERTa model pretrained on Japanese Wikipedia and CC-100 texts for POS-tagging and dependency-parsing (using `goeswith` for subwords), derived from [roberta-base-japanese](https://huggingface.co/nlp-waseda/roberta-base-japanese).
21
+
22
+ ## How to Use
23
+
24
+ ```
25
+ from transformers import pipeline
26
+ nlp=pipeline("universal-dependencies","KoichiYasuoka/roberta-base-japanese-juman-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
27
+ print(nlp("全学年にわたって小学校の国語の教科書に挿し絵が用いられている"))
28
+ ```
29
+
30
+ [fugashi](https://pypi.org/project/fugashi) is required.
config.json ADDED
@@ -0,0 +1,329 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "RobertaForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 2,
7
+ "classifier_dropout": null,
8
+ "custom_pipelines": {
9
+ "universal-dependencies": {
10
+ "impl": "ud.UniversalDependenciesPipeline"
11
+ }
12
+ },
13
+ "eos_token_id": 3,
14
+ "hidden_act": "gelu",
15
+ "hidden_dropout_prob": 0.1,
16
+ "hidden_size": 768,
17
+ "id2label": {
18
+ "0": "-|_|dep",
19
+ "1": "ADJ|Polarity=Neg|acl",
20
+ "2": "ADJ|Polarity=Neg|advcl",
21
+ "3": "ADJ|Polarity=Neg|ccomp",
22
+ "4": "ADJ|Polarity=Neg|root",
23
+ "5": "ADJ|_|acl",
24
+ "6": "ADJ|_|advcl",
25
+ "7": "ADJ|_|amod",
26
+ "8": "ADJ|_|ccomp",
27
+ "9": "ADJ|_|compound",
28
+ "10": "ADJ|_|csubj",
29
+ "11": "ADJ|_|dep",
30
+ "12": "ADJ|_|iobj",
31
+ "13": "ADJ|_|nmod",
32
+ "14": "ADJ|_|nsubj",
33
+ "15": "ADJ|_|obj",
34
+ "16": "ADJ|_|obl",
35
+ "17": "ADJ|_|parataxis",
36
+ "18": "ADJ|_|root",
37
+ "19": "ADP|_|case",
38
+ "20": "ADP|_|dislocated",
39
+ "21": "ADP|_|fixed",
40
+ "22": "ADP|_|mark",
41
+ "23": "ADP|_|root",
42
+ "24": "ADV|_|advcl",
43
+ "25": "ADV|_|advmod",
44
+ "26": "ADV|_|compound",
45
+ "27": "ADV|_|dislocated",
46
+ "28": "ADV|_|iobj",
47
+ "29": "ADV|_|nmod",
48
+ "30": "ADV|_|nsubj",
49
+ "31": "ADV|_|obj",
50
+ "32": "ADV|_|obl",
51
+ "33": "ADV|_|root",
52
+ "34": "AUX|Polarity=Neg|aux",
53
+ "35": "AUX|_|acl",
54
+ "36": "AUX|_|advcl",
55
+ "37": "AUX|_|aux",
56
+ "38": "AUX|_|ccomp",
57
+ "39": "AUX|_|conj",
58
+ "40": "AUX|_|cop",
59
+ "41": "AUX|_|fixed",
60
+ "42": "AUX|_|iobj",
61
+ "43": "AUX|_|obj",
62
+ "44": "AUX|_|obl",
63
+ "45": "AUX|_|root",
64
+ "46": "CCONJ|_|advmod",
65
+ "47": "CCONJ|_|case",
66
+ "48": "CCONJ|_|cc",
67
+ "49": "CCONJ|_|ccomp",
68
+ "50": "CCONJ|_|fixed",
69
+ "51": "CCONJ|_|mark",
70
+ "52": "DET|_|det",
71
+ "53": "DET|_|nsubj",
72
+ "54": "DET|_|obl",
73
+ "55": "DET|_|root",
74
+ "56": "INTJ|_|discourse",
75
+ "57": "INTJ|_|root",
76
+ "58": "NOUN|Polarity=Neg|compound",
77
+ "59": "NOUN|_|acl",
78
+ "60": "NOUN|_|advcl",
79
+ "61": "NOUN|_|advmod",
80
+ "62": "NOUN|_|appos",
81
+ "63": "NOUN|_|ccomp",
82
+ "64": "NOUN|_|compound",
83
+ "65": "NOUN|_|conj",
84
+ "66": "NOUN|_|csubj",
85
+ "67": "NOUN|_|dislocated",
86
+ "68": "NOUN|_|iobj",
87
+ "69": "NOUN|_|list",
88
+ "70": "NOUN|_|nmod",
89
+ "71": "NOUN|_|nsubj",
90
+ "72": "NOUN|_|obj",
91
+ "73": "NOUN|_|obl",
92
+ "74": "NOUN|_|parataxis",
93
+ "75": "NOUN|_|root",
94
+ "76": "NUM|_|advcl",
95
+ "77": "NUM|_|dislocated",
96
+ "78": "NUM|_|iobj",
97
+ "79": "NUM|_|nmod",
98
+ "80": "NUM|_|nsubj",
99
+ "81": "NUM|_|nummod",
100
+ "82": "NUM|_|obj",
101
+ "83": "NUM|_|obl",
102
+ "84": "NUM|_|root",
103
+ "85": "PART|_|acl",
104
+ "86": "PART|_|advcl",
105
+ "87": "PART|_|amod",
106
+ "88": "PART|_|case",
107
+ "89": "PART|_|conj",
108
+ "90": "PART|_|iobj",
109
+ "91": "PART|_|mark",
110
+ "92": "PART|_|nmod",
111
+ "93": "PART|_|nsubj",
112
+ "94": "PART|_|obj",
113
+ "95": "PART|_|obl",
114
+ "96": "PART|_|root",
115
+ "97": "PRON|_|acl",
116
+ "98": "PRON|_|advcl",
117
+ "99": "PRON|_|compound",
118
+ "100": "PRON|_|discourse",
119
+ "101": "PRON|_|dislocated",
120
+ "102": "PRON|_|iobj",
121
+ "103": "PRON|_|nmod",
122
+ "104": "PRON|_|nsubj",
123
+ "105": "PRON|_|obj",
124
+ "106": "PRON|_|obl",
125
+ "107": "PRON|_|root",
126
+ "108": "PROPN|_|acl",
127
+ "109": "PROPN|_|advcl",
128
+ "110": "PROPN|_|compound",
129
+ "111": "PROPN|_|dislocated",
130
+ "112": "PROPN|_|iobj",
131
+ "113": "PROPN|_|nmod",
132
+ "114": "PROPN|_|nsubj",
133
+ "115": "PROPN|_|obj",
134
+ "116": "PROPN|_|obl",
135
+ "117": "PROPN|_|root",
136
+ "118": "PROPN|_|vocative",
137
+ "119": "PUNCT|_|punct",
138
+ "120": "SCONJ|_|advcl",
139
+ "121": "SCONJ|_|fixed",
140
+ "122": "SCONJ|_|mark",
141
+ "123": "SYM|_|compound",
142
+ "124": "SYM|_|nmod",
143
+ "125": "SYM|_|nsubj",
144
+ "126": "SYM|_|obl",
145
+ "127": "SYM|_|punct",
146
+ "128": "VERB|_|acl",
147
+ "129": "VERB|_|advcl",
148
+ "130": "VERB|_|aux",
149
+ "131": "VERB|_|ccomp",
150
+ "132": "VERB|_|compound",
151
+ "133": "VERB|_|conj",
152
+ "134": "VERB|_|csubj",
153
+ "135": "VERB|_|dislocated",
154
+ "136": "VERB|_|fixed",
155
+ "137": "VERB|_|iobj",
156
+ "138": "VERB|_|nmod",
157
+ "139": "VERB|_|nsubj",
158
+ "140": "VERB|_|obj",
159
+ "141": "VERB|_|obl",
160
+ "142": "VERB|_|parataxis",
161
+ "143": "VERB|_|root",
162
+ "144": "X|_|dep",
163
+ "145": "X|_|goeswith",
164
+ "146": "X|_|nmod"
165
+ },
166
+ "initializer_range": 0.02,
167
+ "intermediate_size": 3072,
168
+ "label2id": {
169
+ "-|_|dep": 0,
170
+ "ADJ|Polarity=Neg|acl": 1,
171
+ "ADJ|Polarity=Neg|advcl": 2,
172
+ "ADJ|Polarity=Neg|ccomp": 3,
173
+ "ADJ|Polarity=Neg|root": 4,
174
+ "ADJ|_|acl": 5,
175
+ "ADJ|_|advcl": 6,
176
+ "ADJ|_|amod": 7,
177
+ "ADJ|_|ccomp": 8,
178
+ "ADJ|_|compound": 9,
179
+ "ADJ|_|csubj": 10,
180
+ "ADJ|_|dep": 11,
181
+ "ADJ|_|iobj": 12,
182
+ "ADJ|_|nmod": 13,
183
+ "ADJ|_|nsubj": 14,
184
+ "ADJ|_|obj": 15,
185
+ "ADJ|_|obl": 16,
186
+ "ADJ|_|parataxis": 17,
187
+ "ADJ|_|root": 18,
188
+ "ADP|_|case": 19,
189
+ "ADP|_|dislocated": 20,
190
+ "ADP|_|fixed": 21,
191
+ "ADP|_|mark": 22,
192
+ "ADP|_|root": 23,
193
+ "ADV|_|advcl": 24,
194
+ "ADV|_|advmod": 25,
195
+ "ADV|_|compound": 26,
196
+ "ADV|_|dislocated": 27,
197
+ "ADV|_|iobj": 28,
198
+ "ADV|_|nmod": 29,
199
+ "ADV|_|nsubj": 30,
200
+ "ADV|_|obj": 31,
201
+ "ADV|_|obl": 32,
202
+ "ADV|_|root": 33,
203
+ "AUX|Polarity=Neg|aux": 34,
204
+ "AUX|_|acl": 35,
205
+ "AUX|_|advcl": 36,
206
+ "AUX|_|aux": 37,
207
+ "AUX|_|ccomp": 38,
208
+ "AUX|_|conj": 39,
209
+ "AUX|_|cop": 40,
210
+ "AUX|_|fixed": 41,
211
+ "AUX|_|iobj": 42,
212
+ "AUX|_|obj": 43,
213
+ "AUX|_|obl": 44,
214
+ "AUX|_|root": 45,
215
+ "CCONJ|_|advmod": 46,
216
+ "CCONJ|_|case": 47,
217
+ "CCONJ|_|cc": 48,
218
+ "CCONJ|_|ccomp": 49,
219
+ "CCONJ|_|fixed": 50,
220
+ "CCONJ|_|mark": 51,
221
+ "DET|_|det": 52,
222
+ "DET|_|nsubj": 53,
223
+ "DET|_|obl": 54,
224
+ "DET|_|root": 55,
225
+ "INTJ|_|discourse": 56,
226
+ "INTJ|_|root": 57,
227
+ "NOUN|Polarity=Neg|compound": 58,
228
+ "NOUN|_|acl": 59,
229
+ "NOUN|_|advcl": 60,
230
+ "NOUN|_|advmod": 61,
231
+ "NOUN|_|appos": 62,
232
+ "NOUN|_|ccomp": 63,
233
+ "NOUN|_|compound": 64,
234
+ "NOUN|_|conj": 65,
235
+ "NOUN|_|csubj": 66,
236
+ "NOUN|_|dislocated": 67,
237
+ "NOUN|_|iobj": 68,
238
+ "NOUN|_|list": 69,
239
+ "NOUN|_|nmod": 70,
240
+ "NOUN|_|nsubj": 71,
241
+ "NOUN|_|obj": 72,
242
+ "NOUN|_|obl": 73,
243
+ "NOUN|_|parataxis": 74,
244
+ "NOUN|_|root": 75,
245
+ "NUM|_|advcl": 76,
246
+ "NUM|_|dislocated": 77,
247
+ "NUM|_|iobj": 78,
248
+ "NUM|_|nmod": 79,
249
+ "NUM|_|nsubj": 80,
250
+ "NUM|_|nummod": 81,
251
+ "NUM|_|obj": 82,
252
+ "NUM|_|obl": 83,
253
+ "NUM|_|root": 84,
254
+ "PART|_|acl": 85,
255
+ "PART|_|advcl": 86,
256
+ "PART|_|amod": 87,
257
+ "PART|_|case": 88,
258
+ "PART|_|conj": 89,
259
+ "PART|_|iobj": 90,
260
+ "PART|_|mark": 91,
261
+ "PART|_|nmod": 92,
262
+ "PART|_|nsubj": 93,
263
+ "PART|_|obj": 94,
264
+ "PART|_|obl": 95,
265
+ "PART|_|root": 96,
266
+ "PRON|_|acl": 97,
267
+ "PRON|_|advcl": 98,
268
+ "PRON|_|compound": 99,
269
+ "PRON|_|discourse": 100,
270
+ "PRON|_|dislocated": 101,
271
+ "PRON|_|iobj": 102,
272
+ "PRON|_|nmod": 103,
273
+ "PRON|_|nsubj": 104,
274
+ "PRON|_|obj": 105,
275
+ "PRON|_|obl": 106,
276
+ "PRON|_|root": 107,
277
+ "PROPN|_|acl": 108,
278
+ "PROPN|_|advcl": 109,
279
+ "PROPN|_|compound": 110,
280
+ "PROPN|_|dislocated": 111,
281
+ "PROPN|_|iobj": 112,
282
+ "PROPN|_|nmod": 113,
283
+ "PROPN|_|nsubj": 114,
284
+ "PROPN|_|obj": 115,
285
+ "PROPN|_|obl": 116,
286
+ "PROPN|_|root": 117,
287
+ "PROPN|_|vocative": 118,
288
+ "PUNCT|_|punct": 119,
289
+ "SCONJ|_|advcl": 120,
290
+ "SCONJ|_|fixed": 121,
291
+ "SCONJ|_|mark": 122,
292
+ "SYM|_|compound": 123,
293
+ "SYM|_|nmod": 124,
294
+ "SYM|_|nsubj": 125,
295
+ "SYM|_|obl": 126,
296
+ "SYM|_|punct": 127,
297
+ "VERB|_|acl": 128,
298
+ "VERB|_|advcl": 129,
299
+ "VERB|_|aux": 130,
300
+ "VERB|_|ccomp": 131,
301
+ "VERB|_|compound": 132,
302
+ "VERB|_|conj": 133,
303
+ "VERB|_|csubj": 134,
304
+ "VERB|_|dislocated": 135,
305
+ "VERB|_|fixed": 136,
306
+ "VERB|_|iobj": 137,
307
+ "VERB|_|nmod": 138,
308
+ "VERB|_|nsubj": 139,
309
+ "VERB|_|obj": 140,
310
+ "VERB|_|obl": 141,
311
+ "VERB|_|parataxis": 142,
312
+ "VERB|_|root": 143,
313
+ "X|_|dep": 144,
314
+ "X|_|goeswith": 145,
315
+ "X|_|nmod": 146
316
+ },
317
+ "layer_norm_eps": 1e-12,
318
+ "max_position_embeddings": 514,
319
+ "model_type": "roberta",
320
+ "num_attention_heads": 12,
321
+ "num_hidden_layers": 12,
322
+ "pad_token_id": 0,
323
+ "position_embedding_type": "absolute",
324
+ "torch_dtype": "float32",
325
+ "transformers_version": "4.26.1",
326
+ "type_vocab_size": 2,
327
+ "use_cache": true,
328
+ "vocab_size": 32000
329
+ }
maker.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/python3
2
+ src="nlp-waseda/roberta-base-japanese"
3
+ tgt="KoichiYasuoka/roberta-base-japanese-juman-ud-goeswith"
4
+ url="https://github.com/KoichiYasuoka/SuPar-UniDic/raw/main/suparunidic/suparmodels/ja_gsd_modern.conllu"
5
+ import os
6
+ f=os.path.basename(url)
7
+ os.system("test -f "+f+" || curl -LO "+url)
8
+ class UDgoeswithDataset(object):
9
+ def __init__(self,conllu,tokenizer):
10
+ self.ids,self.tags,label=[],[],set()
11
+ with open(conllu,"r",encoding="utf-8") as r:
12
+ cls,sep,msk=tokenizer.cls_token_id,tokenizer.sep_token_id,tokenizer.mask_token_id
13
+ dep,c="-|_|dep",[]
14
+ for s in r:
15
+ t=s.split("\t")
16
+ if len(t)==10 and t[0].isdecimal():
17
+ c.append(t)
18
+ elif c!=[] and s.strip()=="":
19
+ v=tokenizer([t[1] for t in c],add_special_tokens=False)["input_ids"]
20
+ for i in range(len(v)-1,-1,-1):
21
+ for j in range(1,len(v[i])):
22
+ c.insert(i+1,[c[i][0],"_","_","X","_","_",c[i][0],"goeswith","_","_"])
23
+ y=["0"]+[t[0] for t in c]
24
+ h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(c,1)]
25
+ p,v=[t[3]+"|"+t[5]+"|"+t[7] for t in c],sum(v,[])
26
+ self.ids.append([cls]+v+[sep])
27
+ self.tags.append([dep]+p+[dep])
28
+ label=set(sum([self.tags[-1],list(label)],[]))
29
+ for i,k in enumerate(v):
30
+ self.ids.append([cls]+v[0:i]+[msk]+v[i+1:]+[sep,k])
31
+ self.tags.append([dep]+[t if h[j]==i+1 else dep for j,t in enumerate(p)]+[dep,dep])
32
+ c=[]
33
+ self.label2id={l:i for i,l in enumerate(sorted(label))}
34
+ def __call__(*args):
35
+ label=set(sum([list(t.label2id) for t in args],[]))
36
+ lid={l:i for i,l in enumerate(sorted(label))}
37
+ for t in args:
38
+ t.label2id=lid
39
+ return lid
40
+ __len__=lambda self:len(self.ids)
41
+ __getitem__=lambda self,i:{"input_ids":self.ids[i],"labels":[self.label2id[t] for t in self.tags[i]]}
42
+ from transformers import AutoTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
43
+ tkz=AutoTokenizer.from_pretrained(src)
44
+ trainDS=UDgoeswithDataset(f,tkz)
45
+ lid=trainDS.label2id
46
+ cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()})
47
+ arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=32,output_dir="/tmp",overwrite_output_dir=True,save_total_limit=2,learning_rate=5e-05,warmup_ratio=0.1)
48
+ trn=Trainer(args=arg,data_collator=DataCollatorForTokenClassification(tkz),model=AutoModelForTokenClassification.from_pretrained(src,config=cfg),train_dataset=trainDS)
49
+ trn.train()
50
+ trn.save_model(tgt)
51
+ tkz.save_pretrained(tgt)
mecab-jumandic-utf8.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bbde3e53407df0e50122816df8f936ceb006580c17026e21037518ed542e4cbc
3
+ size 33196897
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b3e9e97a15cd6c75eb42c8fe1059856dade87a4f3bd997f36bcb275933b62b99
3
+ size 440636401
special_tokens_map.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "[CLS]",
3
+ "cls_token": "[CLS]",
4
+ "eos_token": "[SEP]",
5
+ "mask_token": {
6
+ "content": "[MASK]",
7
+ "lstrip": true,
8
+ "normalized": true,
9
+ "rstrip": false,
10
+ "single_word": false
11
+ },
12
+ "pad_token": "[PAD]",
13
+ "sep_token": "[SEP]",
14
+ "unk_token": "[UNK]"
15
+ }
spiece.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a7f87f538d8c73fb0a6a34efb7ba6e3488f920341119c02c208bce7965cf248e
3
+ size 810161
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "auto_map": {"AutoTokenizer":["ud.BertJapaneseTokenizer","ud.JumanAlbertTokenizerFast"]},
3
+ "bos_token": "[CLS]",
4
+ "cls_token": "[CLS]",
5
+ "do_lower_case": false,
6
+ "eos_token": "[SEP]",
7
+ "keep_accents": true,
8
+ "mask_token": {
9
+ "__type": "AddedToken",
10
+ "content": "[MASK]",
11
+ "lstrip": true,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "model_max_length": 514,
17
+ "pad_token": "[PAD]",
18
+ "remove_space": true,
19
+ "sep_token": "[SEP]",
20
+ "sp_model_kwargs": {},
21
+ "special_tokens_map_file": null,
22
+ "subword_tokenizer_type": "sentencepiece",
23
+ "tokenizer_class": "JumanAlbertTokenizerFast",
24
+ "unk_token": "[UNK]",
25
+ "word_tokenizer_type": "jumanpp"
26
+ }
ud.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from transformers import TokenClassificationPipeline,AlbertTokenizerFast,BertJapaneseTokenizer
3
+ from transformers.models.bert_japanese.tokenization_bert_japanese import MecabTokenizer
4
+ try:
5
+ from transformers.utils import cached_file
6
+ except:
7
+ from transformers.file_utils import cached_path,hf_bucket_url
8
+ cached_file=lambda x,y:os.path.join(x,y) if os.path.isdir(x) else cached_path(hf_bucket_url(x,y))
9
+
10
+ class UniversalDependenciesPipeline(TokenClassificationPipeline):
11
+ def _forward(self,model_inputs):
12
+ import torch
13
+ v=model_inputs["input_ids"][0].tolist()
14
+ with torch.no_grad():
15
+ 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)],device=self.device))
16
+ return {"logits":e.logits[:,1:-2,:],**model_inputs}
17
+ def postprocess(self,model_outputs,**kwargs):
18
+ import numpy
19
+ e=model_outputs["logits"].numpy()
20
+ r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
21
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
22
+ g=self.model.config.label2id["X|_|goeswith"]
23
+ r=numpy.tri(e.shape[0])
24
+ for i in range(e.shape[0]):
25
+ for j in range(i+2,e.shape[1]):
26
+ r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
27
+ e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
28
+ m,p=numpy.nanmax(e,axis=2),numpy.nanargmax(e,axis=2)
29
+ h=self.chu_liu_edmonds(m)
30
+ z=[i for i,j in enumerate(h) if i==j]
31
+ if len(z)>1:
32
+ k,h=z[numpy.nanargmax(m[z,z])],numpy.nanmin(m)-numpy.nanmax(m)
33
+ 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])]
34
+ h=self.chu_liu_edmonds(m)
35
+ v=[(s,e) for s,e in model_outputs["offset_mapping"][0].tolist() if s<e]
36
+ q=[self.model.config.id2label[p[j,i]].split("|") for i,j in enumerate(h)]
37
+ if "aggregation_strategy" in kwargs and kwargs["aggregation_strategy"]!="none":
38
+ for i,j in reversed(list(enumerate(q[1:],1))):
39
+ if j[-1]=="goeswith" and set([t[-1] for t in q[h[i]+1:i+1]])=={"goeswith"}:
40
+ h=[b if i>b else b-1 for a,b in enumerate(h) if i!=a]
41
+ v[i-1]=(v[i-1][0],v.pop(i)[1])
42
+ q.pop(i)
43
+ t=model_outputs["sentence"].replace("\n"," ")
44
+ u="# text = "+t+"\n"
45
+ for i,(s,e) in enumerate(v):
46
+ 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"
47
+ return u+"\n"
48
+ def chu_liu_edmonds(self,matrix):
49
+ import numpy
50
+ h=numpy.nanargmax(matrix,axis=0)
51
+ x=[-1 if i==j else j for i,j in enumerate(h)]
52
+ 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]]:
53
+ y=[]
54
+ while x!=y:
55
+ y=list(x)
56
+ for i,j in enumerate(x):
57
+ x[i]=b(x,i,j)
58
+ if max(x)<0:
59
+ return h
60
+ y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
61
+ z=matrix-numpy.nanmax(matrix,axis=0)
62
+ 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])]])
63
+ 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))]
64
+ h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
65
+ i=y[numpy.nanargmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
66
+ h[i]=x[k[-1]] if k[-1]<len(x) else i
67
+ return h
68
+
69
+ class MecabPreTokenizer(MecabTokenizer):
70
+ def mecab_split(self,i,normalized_string):
71
+ t=str(normalized_string)
72
+ e=0
73
+ z=[]
74
+ for c in self.tokenize(t):
75
+ s=t.find(c,e)
76
+ e=e if s<0 else s+len(c)
77
+ z.append((0,0) if s<0 else (s,e))
78
+ return [normalized_string[s:e] for s,e in z if e>0]
79
+ def pre_tokenize(self,pretok):
80
+ pretok.split(self.mecab_split)
81
+
82
+ class JumanAlbertTokenizerFast(AlbertTokenizerFast):
83
+ def __init__(self,**kwargs):
84
+ from tokenizers.pre_tokenizers import PreTokenizer,Metaspace,Sequence
85
+ super().__init__(**kwargs)
86
+ d,r="/var/lib/mecab/dic/juman-utf8","/etc/mecabrc"
87
+ if not (os.path.isdir(d) and os.path.isfile(r)):
88
+ import zipfile
89
+ import tempfile
90
+ self.dicdir=tempfile.TemporaryDirectory()
91
+ d=self.dicdir.name
92
+ with zipfile.ZipFile(cached_file(self.name_or_path,"mecab-jumandic-utf8.zip")) as z:
93
+ z.extractall(d)
94
+ r=os.path.join(d,"mecabrc")
95
+ with open(r,"w",encoding="utf-8") as w:
96
+ print("dicdir =",d,file=w)
97
+ self.custom_pre_tokenizer=Sequence([PreTokenizer.custom(MecabPreTokenizer(mecab_dic=None,mecab_option="-d "+d+" -r "+r)),Metaspace()])
98
+ self._tokenizer.pre_tokenizer=self.custom_pre_tokenizer
99
+ def save_pretrained(self,save_directory,**kwargs):
100
+ import shutil
101
+ from tokenizers.pre_tokenizers import Metaspace
102
+ self._auto_map={"AutoTokenizer":["ud.BertJapaneseTokenizer","ud.JumanAlbertTokenizerFast"]}
103
+ self._tokenizer.pre_tokenizer=Metaspace()
104
+ super().save_pretrained(save_directory,**kwargs)
105
+ self._tokenizer.pre_tokenizer=self.custom_pre_tokenizer
106
+ shutil.copy(os.path.abspath(__file__),os.path.join(save_directory,"ud.py"))
107
+ shutil.copy(cached_file(self.name_or_path,"mecab-jumandic-utf8.zip"),os.path.join(save_directory,"mecab-jumandic-utf8.zip"))