KoichiYasuoka
commited on
Commit
•
0ff8f8f
1
Parent(s):
fc5131d
chu_liu_edmonds included
Browse files
ud.py
CHANGED
@@ -19,17 +19,13 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
|
|
19 |
for j in range(i+2,e.shape[1]):
|
20 |
r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
|
21 |
e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
|
22 |
-
m=numpy.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
m[
|
28 |
-
|
29 |
-
if [0 for i in h if i==0]!=[0]:
|
30 |
-
m[:,0]+=numpy.where(m[:,0]==numpy.nanmax(m[[i for i,j in enumerate(h) if j==0],0]),0,numpy.nan)
|
31 |
-
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)]
|
32 |
-
h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
|
33 |
v=[(s,e) for s,e in model_output["offset_mapping"][0].tolist() if s<e]
|
34 |
q=[self.model.config.id2label[p[i,j]].split("|") for i,j in enumerate(h)]
|
35 |
g="aggregation_strategy" in kwargs and kwargs["aggregation_strategy"]!="none"
|
@@ -44,3 +40,25 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
|
|
44 |
for i,(s,e) in enumerate(v,1):
|
45 |
u+="\t".join([str(i),t[s:e],t[s:e] if g else "_",q[i][0],"_","|".join(q[i][1:-1]),str(h[i]),q[i][-1],"_","_" if i<len(v) and e<v[i][0] else "SpaceAfter=No"])+"\n"
|
46 |
return u+"\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
for j in range(i+2,e.shape[1]):
|
20 |
r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
|
21 |
e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
|
22 |
+
m,p=numpy.nanmax(e,axis=2),numpy.nanargmax(e,axis=2)
|
23 |
+
h=self.chu_liu_edmonds(m)
|
24 |
+
z=[i for i,j in enumerate(h) if i==j]
|
25 |
+
if len(z)>1:
|
26 |
+
k=z[numpy.nanargmax(m[z,z])]
|
27 |
+
m[:,z]+=[[0 if j in z and (i!=j or i==k) else numpy.nan for i in z] for j in range(m.shape[0])]
|
28 |
+
h=self.chu_liu_edmonds(m)
|
|
|
|
|
|
|
|
|
29 |
v=[(s,e) for s,e in model_output["offset_mapping"][0].tolist() if s<e]
|
30 |
q=[self.model.config.id2label[p[i,j]].split("|") for i,j in enumerate(h)]
|
31 |
g="aggregation_strategy" in kwargs and kwargs["aggregation_strategy"]!="none"
|
|
|
40 |
for i,(s,e) in enumerate(v,1):
|
41 |
u+="\t".join([str(i),t[s:e],t[s:e] if g else "_",q[i][0],"_","|".join(q[i][1:-1]),str(h[i]),q[i][-1],"_","_" if i<len(v) and e<v[i][0] else "SpaceAfter=No"])+"\n"
|
42 |
return u+"\n"
|
43 |
+
def chu_liu_edmonds(self,matrix):
|
44 |
+
import numpy
|
45 |
+
h=numpy.nanargmax(matrix,axis=0)
|
46 |
+
x=[-1 if i==j else j for i,j in enumerate(h)]
|
47 |
+
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]]:
|
48 |
+
y=[]
|
49 |
+
while x!=y:
|
50 |
+
y=list(x)
|
51 |
+
for i,j in enumerate(x):
|
52 |
+
x[i]=b(x,i,j)
|
53 |
+
if max(x)<0:
|
54 |
+
return h
|
55 |
+
y,m=[i for i,j in enumerate(x) if j==max(x)],numpy.full((matrix.shape[0]+1,matrix.shape[1]+1),numpy.nan)
|
56 |
+
m[0:-1,0:-1]=z=matrix-numpy.nanmax(matrix,axis=0)
|
57 |
+
m[0:-1,-1],m[-1,0:-1],m[-1,-1]=numpy.nanmax(z[:,y],axis=1),numpy.nanmax(z[y,:],axis=0),numpy.nanmax(z[y,y])
|
58 |
+
m[y,:]=m[:,y]=numpy.nan
|
59 |
+
m[y,y]=0
|
60 |
+
k=self.chu_liu_edmonds(m)
|
61 |
+
j=y[numpy.nanargmax(z[k[-1],y] if k[-1]<z.shape[0] else z[y,y])]
|
62 |
+
i=k[-1] if k[-1]<z.shape[0] else j
|
63 |
+
z[0:i,j]=z[i+1:,j]=numpy.nan
|
64 |
+
return self.chu_liu_edmonds(z)
|