KoichiYasuoka commited on
Commit
27dfa14
1 Parent(s): d93c336

initial release

Browse files
Files changed (9) hide show
  1. README.md +74 -0
  2. config.json +281 -0
  3. maker.py +50 -0
  4. pytorch_model.bin +3 -0
  5. special_tokens_map.json +9 -0
  6. spm.model +3 -0
  7. tokenizer.json +0 -0
  8. tokenizer_config.json +14 -0
  9. ud.py +60 -0
README.md ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "ain"
4
+ tags:
5
+ - "ainu"
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
+ ---
14
+
15
+ # deberta-base-ainu-ud-goeswith
16
+
17
+ ## Model Description
18
+
19
+ This is a DeBERTa(V2) model pre-trained on Ainu texts (both カタカナ and romanized) for POS-tagging and dependency-parsing (using `goeswith` for subwords), derived from [deberta-base-ainu-upos](https://huggingface.co/KoichiYasuoka/deberta-base-ainu-upos).
20
+
21
+ ## How to Use
22
+
23
+ ```py
24
+ class UDgoeswith(object):
25
+ def __init__(self,bert):
26
+ from transformers import AutoTokenizer,AutoModelForTokenClassification
27
+ self.tokenizer=AutoTokenizer.from_pretrained(bert)
28
+ self.model=AutoModelForTokenClassification.from_pretrained(bert)
29
+ def __call__(self,text):
30
+ import numpy,torch,ufal.chu_liu_edmonds
31
+ w=self.tokenizer(text,return_offsets_mapping=True)
32
+ v=w["input_ids"]
33
+ x=[v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[j] for i,j in enumerate(v[1:-1],1)]
34
+ with torch.no_grad():
35
+ e=self.model(input_ids=torch.tensor(x)).logits.numpy()[:,1:-2,:]
36
+ r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
37
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
38
+ g=self.model.config.label2id["X|_|goeswith"]
39
+ r=numpy.tri(e.shape[0])
40
+ for i in range(e.shape[0]):
41
+ for j in range(i+2,e.shape[1]):
42
+ r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
43
+ e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
44
+ m=numpy.full((e.shape[0]+1,e.shape[1]+1),numpy.nan)
45
+ m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
46
+ p=numpy.zeros(m.shape)
47
+ p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
48
+ for i in range(1,m.shape[0]):
49
+ m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
50
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
51
+ if [0 for i in h if i==0]!=[0]:
52
+ m[:,0]+=numpy.where(m[:,0]==numpy.nanmax(m[[i for i,j in enumerate(h) if j==0],0]),0,numpy.nan)
53
+ 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)]
54
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
55
+ u="# text = "+text+"\n"
56
+ v=[(s,e) for s,e in w["offset_mapping"] if s<e]
57
+ for i,(s,e) in enumerate(v,1):
58
+ q=self.model.config.id2label[p[i,h[i]]].split("|")
59
+ 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"
60
+ return u+"\n"
61
+
62
+ nlp=UDgoeswith("KoichiYasuoka/deberta-base-ainu-ud-goeswith")
63
+ print(nlp("itak=as awa pon rupne aynu ene itaki"))
64
+ ```
65
+
66
+ with [ufal.chu-liu-edmonds](https://pypi.org/project/ufal.chu-liu-edmonds/).
67
+ Or without ufal.chu-liu-edmonds:
68
+
69
+ ```
70
+ from transformers import pipeline
71
+ nlp=pipeline("universal-dependencies","KoichiYasuoka/deberta-base-ainu-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
72
+ print(nlp("itak=as awa pon rupne aynu ene itaki"))
73
+ ```
74
+
config.json ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DebertaV2ForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "custom_pipelines": {
8
+ "universal-dependencies": {
9
+ "impl": "ud.UniversalDependenciesPipeline"
10
+ }
11
+ },
12
+ "eos_token_id": 2,
13
+ "hidden_act": "gelu",
14
+ "hidden_dropout_prob": 0.1,
15
+ "hidden_size": 768,
16
+ "id2label": {
17
+ "0": "-|_|dep",
18
+ "1": "ADP|\u526f\u52a9\u8a5e|case",
19
+ "2": "ADP|\u526f\u52a9\u8a5e|mark",
20
+ "3": "ADP|\u526f\u52a9\u8a5e|root",
21
+ "4": "ADP|\u5f8c\u7f6e\u526f\u8a5e|root",
22
+ "5": "ADP|\u683c\u52a9\u8a5e|case",
23
+ "6": "ADV|\u526f\u8a5e|acl",
24
+ "7": "ADV|\u526f\u8a5e|advcl",
25
+ "8": "ADV|\u526f\u8a5e|advmod",
26
+ "9": "ADV|\u526f\u8a5e|amod",
27
+ "10": "ADV|\u526f\u8a5e|conj",
28
+ "11": "ADV|\u526f\u8a5e|parataxis",
29
+ "12": "ADV|\u526f\u8a5e|root",
30
+ "13": "AUX|\u30c7\u30a2\u30eb\u52d5\u8a5e|cop",
31
+ "14": "AUX|\u52a9\u52d5\u8a5e|aux",
32
+ "15": "AUX|\u52a9\u52d5\u8a5e|case",
33
+ "16": "CCONJ|\u63a5\u7d9a\u52a9\u8a5e|cc",
34
+ "17": "CCONJ|\u63a5\u7d9a\u52a9\u8a5e|root",
35
+ "18": "CCONJ|\u63a5\u7d9a\u8a5e|cc",
36
+ "19": "CCONJ|\u63a5\u7d9a\u8a5e|mark",
37
+ "20": "DET|\u9023\u4f53\u8a5e|acl",
38
+ "21": "DET|\u9023\u4f53\u8a5e|det",
39
+ "22": "DET|\u9023\u4f53\u8a5e|parataxis",
40
+ "23": "DET|\u9023\u4f53\u8a5e|root",
41
+ "24": "INTJ|\u9593\u6295\u8a5e|conj",
42
+ "25": "INTJ|\u9593\u6295\u8a5e|discource",
43
+ "26": "INTJ|\u9593\u6295\u8a5e|discourse",
44
+ "27": "INTJ|\u9593\u6295\u8a5e|parataxis",
45
+ "28": "INTJ|\u9593\u6295\u8a5e|root",
46
+ "29": "NOUN|\u4ee3\u540d\u8a5e|nsubj",
47
+ "30": "NOUN|\u4ee3\u540d\u8a5e|parataxis",
48
+ "31": "NOUN|\u4ee3\u540d\u8a5e|root",
49
+ "32": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|iobj",
50
+ "33": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|nmod",
51
+ "34": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|nsubj",
52
+ "35": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|obj",
53
+ "36": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|obl",
54
+ "37": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|parataxis",
55
+ "38": "NOUN|\u4f4d\u7f6e\u540d\u8a5e|root",
56
+ "39": "NOUN|\u540d\u8a5e|acl",
57
+ "40": "NOUN|\u540d\u8a5e|advcl",
58
+ "41": "NOUN|\u540d\u8a5e|appos",
59
+ "42": "NOUN|\u540d\u8a5e|compound",
60
+ "43": "NOUN|\u540d\u8a5e|conj",
61
+ "44": "NOUN|\u540d\u8a5e|iobj",
62
+ "45": "NOUN|\u540d\u8a5e|nmod",
63
+ "46": "NOUN|\u540d\u8a5e|nsubj",
64
+ "47": "NOUN|\u540d\u8a5e|obj",
65
+ "48": "NOUN|\u540d\u8a5e|obl",
66
+ "49": "NOUN|\u540d\u8a5e|parataxis",
67
+ "50": "NOUN|\u540d\u8a5e|root",
68
+ "51": "NOUN|\u540d\u8a5e|vocative",
69
+ "52": "NOUN|\u5f62\u5f0f\u540d\u8a5e|advcl",
70
+ "53": "NOUN|\u5f62\u5f0f\u540d\u8a5e|conj",
71
+ "54": "NOUN|\u5f62\u5f0f\u540d\u8a5e|nmod",
72
+ "55": "NOUN|\u5f62\u5f0f\u540d\u8a5e|nsubj",
73
+ "56": "NOUN|\u5f62\u5f0f\u540d\u8a5e|obj",
74
+ "57": "NOUN|\u5f62\u5f0f\u540d\u8a5e|obl",
75
+ "58": "NOUN|\u5f62\u5f0f\u540d\u8a5e|parataxis",
76
+ "59": "NOUN|\u5f62\u5f0f\u540d\u8a5e|root",
77
+ "60": "NUM|\u6570\u8a5e|conj",
78
+ "61": "NUM|\u6570\u8a5e|nummod",
79
+ "62": "NUM|\u6570\u8a5e|root",
80
+ "63": "PART|\u4eba\u79f0\u63a5\u8f9e|det",
81
+ "64": "PART|\u4eba\u79f0\u63a5\u8f9e|expl",
82
+ "65": "PART|\u4eba\u79f0\u63a5\u8f9e|iobj",
83
+ "66": "PART|\u4eba\u79f0\u63a5\u8f9e|nsubj",
84
+ "67": "PART|\u4eba\u79f0\u63a5\u8f9e|obj",
85
+ "68": "PART|\u4eba\u79f0\u63a5\u8f9e|parataxis",
86
+ "69": "PART|\u4eba\u79f0\u63a5\u8f9e|root",
87
+ "70": "PART|\u63a5\u5c3e\u8f9e|conj",
88
+ "71": "PART|\u63a5\u5c3e\u8f9e|fixed",
89
+ "72": "PART|\u63a5\u5c3e\u8f9e|obl",
90
+ "73": "PART|\u63a5\u5c3e\u8f9e|root",
91
+ "74": "PART|\u63a5\u982d\u8f9e|compound",
92
+ "75": "PART|\u63a5\u982d\u8f9e|det",
93
+ "76": "PART|\u63a5\u982d\u8f9e|fixed",
94
+ "77": "PART|\u63a5\u982d\u8f9e|iobj",
95
+ "78": "PART|\u63a5\u982d\u8f9e|parataxis",
96
+ "79": "PART|\u7d42\u52a9\u8a5e|discourse",
97
+ "80": "PART|\u7d42\u52a9\u8a5e|mark",
98
+ "81": "PART|\u7d42\u52a9\u8a5e|root",
99
+ "82": "PRON|\u4ee3\u540d\u8a5e|compound",
100
+ "83": "PRON|\u4ee3\u540d\u8a5e|nsubj",
101
+ "84": "PRON|\u4ee3\u540d\u8a5e|root",
102
+ "85": "PROPN|\u56fa\u6709\u540d\u8a5e|nsubj",
103
+ "86": "PROPN|\u56fa\u6709\u540d\u8a5e|root",
104
+ "87": "PUNCT|\u8a18\u53f7|punct",
105
+ "88": "SCONJ|\u5f8c\u7f6e\u526f\u8a5e|case",
106
+ "89": "SCONJ|\u5f8c\u7f6e\u526f\u8a5e|parataxis",
107
+ "90": "SCONJ|\u5f8c\u7f6e\u526f\u8a5e|root",
108
+ "91": "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|advmod",
109
+ "92": "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|case",
110
+ "93": "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|cc",
111
+ "94": "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|mark",
112
+ "95": "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|root",
113
+ "96": "SCONJ|\u63a5\u7d9a\u8a5e|case",
114
+ "97": "SCONJ|\u63a5\u7d9a\u8a5e|mark",
115
+ "98": "VERB|\u4ed6\u52d5\u8a5e|acl",
116
+ "99": "VERB|\u4ed6\u52d5\u8a5e|advcl",
117
+ "100": "VERB|\u4ed6\u52d5\u8a5e|amod",
118
+ "101": "VERB|\u4ed6\u52d5\u8a5e|ccomp",
119
+ "102": "VERB|\u4ed6\u52d5\u8a5e|conj",
120
+ "103": "VERB|\u4ed6\u52d5\u8a5e|parataxis",
121
+ "104": "VERB|\u4ed6\u52d5\u8a5e|root",
122
+ "105": "VERB|\u4ed6\u52d5\u8a5e\uff1f|root",
123
+ "106": "VERB|\u5b8c\u5168\u52d5\u8a5e|acl",
124
+ "107": "VERB|\u5b8c\u5168\u52d5\u8a5e|advcl",
125
+ "108": "VERB|\u5b8c\u5168\u52d5\u8a5e|parataxis",
126
+ "109": "VERB|\u5b8c\u5168\u52d5\u8a5e|root",
127
+ "110": "VERB|\u81ea\u52d5\u8a5e|acl",
128
+ "111": "VERB|\u81ea\u52d5\u8a5e|advcl",
129
+ "112": "VERB|\u81ea\u52d5\u8a5e|amod",
130
+ "113": "VERB|\u81ea\u52d5\u8a5e|ccomp",
131
+ "114": "VERB|\u81ea\u52d5\u8a5e|conj",
132
+ "115": "VERB|\u81ea\u52d5\u8a5e|parataxis",
133
+ "116": "VERB|\u81ea\u52d5\u8a5e|root",
134
+ "117": "VERB|\u81ea\u52d5\u8a5e\uff1f|root",
135
+ "118": "X|_|goeswith"
136
+ },
137
+ "initializer_range": 0.02,
138
+ "intermediate_size": 3072,
139
+ "label2id": {
140
+ "-|_|dep": 0,
141
+ "ADP|\u526f\u52a9\u8a5e|case": 1,
142
+ "ADP|\u526f\u52a9\u8a5e|mark": 2,
143
+ "ADP|\u526f\u52a9\u8a5e|root": 3,
144
+ "ADP|\u5f8c\u7f6e\u526f\u8a5e|root": 4,
145
+ "ADP|\u683c\u52a9\u8a5e|case": 5,
146
+ "ADV|\u526f\u8a5e|acl": 6,
147
+ "ADV|\u526f\u8a5e|advcl": 7,
148
+ "ADV|\u526f\u8a5e|advmod": 8,
149
+ "ADV|\u526f\u8a5e|amod": 9,
150
+ "ADV|\u526f\u8a5e|conj": 10,
151
+ "ADV|\u526f\u8a5e|parataxis": 11,
152
+ "ADV|\u526f\u8a5e|root": 12,
153
+ "AUX|\u30c7\u30a2\u30eb\u52d5\u8a5e|cop": 13,
154
+ "AUX|\u52a9\u52d5\u8a5e|aux": 14,
155
+ "AUX|\u52a9\u52d5\u8a5e|case": 15,
156
+ "CCONJ|\u63a5\u7d9a\u52a9\u8a5e|cc": 16,
157
+ "CCONJ|\u63a5\u7d9a\u52a9\u8a5e|root": 17,
158
+ "CCONJ|\u63a5\u7d9a\u8a5e|cc": 18,
159
+ "CCONJ|\u63a5\u7d9a\u8a5e|mark": 19,
160
+ "DET|\u9023\u4f53\u8a5e|acl": 20,
161
+ "DET|\u9023\u4f53\u8a5e|det": 21,
162
+ "DET|\u9023\u4f53\u8a5e|parataxis": 22,
163
+ "DET|\u9023\u4f53\u8a5e|root": 23,
164
+ "INTJ|\u9593\u6295\u8a5e|conj": 24,
165
+ "INTJ|\u9593\u6295\u8a5e|discource": 25,
166
+ "INTJ|\u9593\u6295\u8a5e|discourse": 26,
167
+ "INTJ|\u9593\u6295\u8a5e|parataxis": 27,
168
+ "INTJ|\u9593\u6295\u8a5e|root": 28,
169
+ "NOUN|\u4ee3\u540d\u8a5e|nsubj": 29,
170
+ "NOUN|\u4ee3\u540d\u8a5e|parataxis": 30,
171
+ "NOUN|\u4ee3\u540d\u8a5e|root": 31,
172
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|iobj": 32,
173
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|nmod": 33,
174
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|nsubj": 34,
175
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|obj": 35,
176
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|obl": 36,
177
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|parataxis": 37,
178
+ "NOUN|\u4f4d\u7f6e\u540d\u8a5e|root": 38,
179
+ "NOUN|\u540d\u8a5e|acl": 39,
180
+ "NOUN|\u540d\u8a5e|advcl": 40,
181
+ "NOUN|\u540d\u8a5e|appos": 41,
182
+ "NOUN|\u540d\u8a5e|compound": 42,
183
+ "NOUN|\u540d\u8a5e|conj": 43,
184
+ "NOUN|\u540d\u8a5e|iobj": 44,
185
+ "NOUN|\u540d\u8a5e|nmod": 45,
186
+ "NOUN|\u540d\u8a5e|nsubj": 46,
187
+ "NOUN|\u540d\u8a5e|obj": 47,
188
+ "NOUN|\u540d\u8a5e|obl": 48,
189
+ "NOUN|\u540d\u8a5e|parataxis": 49,
190
+ "NOUN|\u540d\u8a5e|root": 50,
191
+ "NOUN|\u540d\u8a5e|vocative": 51,
192
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|advcl": 52,
193
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|conj": 53,
194
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|nmod": 54,
195
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|nsubj": 55,
196
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|obj": 56,
197
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|obl": 57,
198
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|parataxis": 58,
199
+ "NOUN|\u5f62\u5f0f\u540d\u8a5e|root": 59,
200
+ "NUM|\u6570\u8a5e|conj": 60,
201
+ "NUM|\u6570\u8a5e|nummod": 61,
202
+ "NUM|\u6570\u8a5e|root": 62,
203
+ "PART|\u4eba\u79f0\u63a5\u8f9e|det": 63,
204
+ "PART|\u4eba\u79f0\u63a5\u8f9e|expl": 64,
205
+ "PART|\u4eba\u79f0\u63a5\u8f9e|iobj": 65,
206
+ "PART|\u4eba\u79f0\u63a5\u8f9e|nsubj": 66,
207
+ "PART|\u4eba\u79f0\u63a5\u8f9e|obj": 67,
208
+ "PART|\u4eba\u79f0\u63a5\u8f9e|parataxis": 68,
209
+ "PART|\u4eba\u79f0\u63a5\u8f9e|root": 69,
210
+ "PART|\u63a5\u5c3e\u8f9e|conj": 70,
211
+ "PART|\u63a5\u5c3e\u8f9e|fixed": 71,
212
+ "PART|\u63a5\u5c3e\u8f9e|obl": 72,
213
+ "PART|\u63a5\u5c3e\u8f9e|root": 73,
214
+ "PART|\u63a5\u982d\u8f9e|compound": 74,
215
+ "PART|\u63a5\u982d\u8f9e|det": 75,
216
+ "PART|\u63a5\u982d\u8f9e|fixed": 76,
217
+ "PART|\u63a5\u982d\u8f9e|iobj": 77,
218
+ "PART|\u63a5\u982d\u8f9e|parataxis": 78,
219
+ "PART|\u7d42\u52a9\u8a5e|discourse": 79,
220
+ "PART|\u7d42\u52a9\u8a5e|mark": 80,
221
+ "PART|\u7d42\u52a9\u8a5e|root": 81,
222
+ "PRON|\u4ee3\u540d\u8a5e|compound": 82,
223
+ "PRON|\u4ee3\u540d\u8a5e|nsubj": 83,
224
+ "PRON|\u4ee3\u540d\u8a5e|root": 84,
225
+ "PROPN|\u56fa\u6709\u540d\u8a5e|nsubj": 85,
226
+ "PROPN|\u56fa\u6709\u540d\u8a5e|root": 86,
227
+ "PUNCT|\u8a18\u53f7|punct": 87,
228
+ "SCONJ|\u5f8c\u7f6e\u526f\u8a5e|case": 88,
229
+ "SCONJ|\u5f8c\u7f6e\u526f\u8a5e|parataxis": 89,
230
+ "SCONJ|\u5f8c\u7f6e\u526f\u8a5e|root": 90,
231
+ "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|advmod": 91,
232
+ "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|case": 92,
233
+ "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|cc": 93,
234
+ "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|mark": 94,
235
+ "SCONJ|\u63a5\u7d9a\u52a9\u8a5e|root": 95,
236
+ "SCONJ|\u63a5\u7d9a\u8a5e|case": 96,
237
+ "SCONJ|\u63a5\u7d9a\u8a5e|mark": 97,
238
+ "VERB|\u4ed6\u52d5\u8a5e|acl": 98,
239
+ "VERB|\u4ed6\u52d5\u8a5e|advcl": 99,
240
+ "VERB|\u4ed6\u52d5\u8a5e|amod": 100,
241
+ "VERB|\u4ed6\u52d5\u8a5e|ccomp": 101,
242
+ "VERB|\u4ed6\u52d5\u8a5e|conj": 102,
243
+ "VERB|\u4ed6\u52d5\u8a5e|parataxis": 103,
244
+ "VERB|\u4ed6\u52d5\u8a5e|root": 104,
245
+ "VERB|\u4ed6\u52d5\u8a5e\uff1f|root": 105,
246
+ "VERB|\u5b8c\u5168\u52d5\u8a5e|acl": 106,
247
+ "VERB|\u5b8c\u5168\u52d5\u8a5e|advcl": 107,
248
+ "VERB|\u5b8c\u5168\u52d5\u8a5e|parataxis": 108,
249
+ "VERB|\u5b8c\u5168\u52d5\u8a5e|root": 109,
250
+ "VERB|\u81ea\u52d5\u8a5e|acl": 110,
251
+ "VERB|\u81ea\u52d5\u8a5e|advcl": 111,
252
+ "VERB|\u81ea\u52d5\u8a5e|amod": 112,
253
+ "VERB|\u81ea\u52d5\u8a5e|ccomp": 113,
254
+ "VERB|\u81ea\u52d5\u8a5e|conj": 114,
255
+ "VERB|\u81ea\u52d5\u8a5e|parataxis": 115,
256
+ "VERB|\u81ea\u52d5\u8a5e|root": 116,
257
+ "VERB|\u81ea\u52d5\u8a5e\uff1f|root": 117,
258
+ "X|_|goeswith": 118
259
+ },
260
+ "layer_norm_eps": 1e-07,
261
+ "max_position_embeddings": 512,
262
+ "max_relative_positions": -1,
263
+ "model_type": "deberta-v2",
264
+ "num_attention_heads": 12,
265
+ "num_hidden_layers": 12,
266
+ "pad_token_id": 1,
267
+ "pooler_dropout": 0,
268
+ "pooler_hidden_act": "gelu",
269
+ "pooler_hidden_size": 768,
270
+ "pos_att_type": [
271
+ "p2c",
272
+ "c2p"
273
+ ],
274
+ "position_biased_input": false,
275
+ "relative_attention": true,
276
+ "tokenizer_class": "DebertaV2TokenizerFast",
277
+ "torch_dtype": "float32",
278
+ "transformers_version": "4.22.1",
279
+ "type_vocab_size": 0,
280
+ "vocab_size": 5093
281
+ }
maker.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/python3
2
+ src="KoichiYasuoka/deberta-base-ainu-upos"
3
+ tgt="KoichiYasuoka/deberta-base-ainu-ud-goeswith"
4
+ import os
5
+ url="https://github.com/KoichiYasuoka/UD-Ainu"
6
+ d=os.path.basename(url)
7
+ os.system("test -d {} || git clone --depth=1 {}".format(d,url))
8
+ s='{if($0==""){if(u~/\\t0\\troot\\t/)print u;u=""}else u=u$0"\\n"}'
9
+ os.system("nawk -F'\\t' '{}' {}/ain_*-ud-*.conllu > train.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
+ for x in [1,2]:
22
+ d=list(c)
23
+ v=tokenizer([t[x] for t in d],add_special_tokens=False)["input_ids"]
24
+ for i in range(len(v)-1,-1,-1):
25
+ for j in range(1,len(v[i])):
26
+ d.insert(i+1,[d[i][0],"_","_","X","_","_",d[i][0],"goeswith","_","_"])
27
+ y=["0"]+[t[0] for t in d]
28
+ h=[i if t[6]=="0" else y.index(t[6]) for i,t in enumerate(d,1)]
29
+ p,v=[t[3]+"|"+t[4]+"|"+t[7] for t in d],sum(v,[])
30
+ if len(v)<tokenizer.model_max_length-3:
31
+ self.ids.append([cls]+v+[sep])
32
+ self.tags.append([dep]+p+[dep])
33
+ label=set(sum([self.tags[-1],list(label)],[]))
34
+ for i,k in enumerate(v):
35
+ self.ids.append([cls]+v[0:i]+[msk]+v[i+1:]+[sep,k])
36
+ self.tags.append([dep]+[t if h[j]==i+1 else dep for j,t in enumerate(p)]+[dep,dep])
37
+ c=[]
38
+ self.label2id={l:i for i,l in enumerate(sorted(label))}
39
+ __len__=lambda self:len(self.ids)
40
+ __getitem__=lambda self,i:{"input_ids":self.ids[i],"labels":[self.label2id[t] for t in self.tags[i]]}
41
+ from transformers import AutoTokenizer,AutoConfig,AutoModelForTokenClassification,DataCollatorForTokenClassification,TrainingArguments,Trainer
42
+ tkz=AutoTokenizer.from_pretrained(src)
43
+ trainDS=UDgoeswithDataset("train.conllu",tkz)
44
+ lid=trainDS.label2id
45
+ cfg=AutoConfig.from_pretrained(src,num_labels=len(lid),label2id=lid,id2label={i:l for l,i in lid.items()},ignore_mismatched_sizes=True)
46
+ arg=TrainingArguments(num_train_epochs=3,per_device_train_batch_size=16,output_dir="/tmp",overwrite_output_dir=True,save_total_limit=2,learning_rate=5e-05,warmup_ratio=0.1)
47
+ trn=Trainer(args=arg,data_collator=DataCollatorForTokenClassification(tkz),model=AutoModelForTokenClassification.from_pretrained(src,config=cfg,ignore_mismatched_sizes=True),train_dataset=trainDS)
48
+ trn.train()
49
+ trn.save_model(tgt)
50
+ tkz.save_pretrained(tgt)
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:57c22aa92af730ccaff2fd03db9f0a08580693be630a1424307005a0bf9956f3
3
+ size 416166099
special_tokens_map.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "[CLS]",
3
+ "cls_token": "[CLS]",
4
+ "eos_token": "[SEP]",
5
+ "mask_token": "[MASK]",
6
+ "pad_token": "[PAD]",
7
+ "sep_token": "[SEP]",
8
+ "unk_token": "[UNK]"
9
+ }
spm.model ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
3
+ size 1
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
+ "bos_token": "[CLS]",
3
+ "cls_token": "[CLS]",
4
+ "do_lower_case": true,
5
+ "eos_token": "[SEP]",
6
+ "keep_accents": false,
7
+ "mask_token": "[MASK]",
8
+ "model_max_length": 512,
9
+ "pad_token": "[PAD]",
10
+ "sep_token": "[SEP]",
11
+ "split_by_punct": true,
12
+ "tokenizer_class": "DebertaV2TokenizerFast",
13
+ "unk_token": "[UNK]"
14
+ }
ud.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TokenClassificationPipeline
2
+
3
+ class UniversalDependenciesPipeline(TokenClassificationPipeline):
4
+ def _forward(self,model_inputs):
5
+ import torch
6
+ v=model_inputs["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)],device=self.device))
9
+ return {"logits":e.logits[:,1:-2,:],**model_inputs}
10
+ def postprocess(self,model_outputs,**kwargs):
11
+ import numpy
12
+ e=model_outputs["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_outputs["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_outputs["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