KoichiYasuoka
commited on
Commit
·
1a91052
1
Parent(s):
f52a6db
algorithm improved
Browse files
ud.py
CHANGED
@@ -5,9 +5,9 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
|
|
5 |
def __init__(self,**kwargs):
|
6 |
super().__init__(**kwargs)
|
7 |
x=self.model.config.label2id
|
8 |
-
self.root=numpy.full((len(x))
|
9 |
-
self.left_arc=numpy.full((len(x))
|
10 |
-
self.right_arc=numpy.full((len(x))
|
11 |
for k,v in x.items():
|
12 |
if k.endswith("|root"):
|
13 |
self.root[v]=0
|
@@ -72,13 +72,13 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
|
|
72 |
r=numpy.tri(e.shape[0])
|
73 |
for i in range(e.shape[0]):
|
74 |
for j in range(i+2,e.shape[1]):
|
75 |
-
r[i,j]=r[i,j-1] if numpy.
|
76 |
-
e[:,:,g]+=numpy.where(r==0,0
|
77 |
-
m,p=numpy.
|
78 |
h=self.chu_liu_edmonds(m)
|
79 |
z=[i for i,j in enumerate(h) if i==j]
|
80 |
if len(z)>1:
|
81 |
-
k,h=z[numpy.
|
82 |
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])]
|
83 |
h=self.chu_liu_edmonds(m)
|
84 |
v=[(s,e) for s,e in off if s<e]
|
@@ -99,7 +99,7 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
|
|
99 |
u+="\t".join([str(i+1),t[s:e],"_",q[i][0],"_","_" if len(q[i])<4 else "|".join(q[i][2:-1]),str(0 if h[i]==i else h[i]+1),"root" if q[i][-1]=="root" else q[i][-1][2:],"_","_" if i+1<len(v) and e<v[i+1][0] else "SpaceAfter=No"])+"\n"
|
100 |
return u+"\n"
|
101 |
def chu_liu_edmonds(self,matrix):
|
102 |
-
h=numpy.
|
103 |
x=[-1 if i==j else j for i,j in enumerate(h)]
|
104 |
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]]:
|
105 |
y=[]
|
@@ -110,10 +110,10 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
|
|
110 |
if max(x)<0:
|
111 |
return h
|
112 |
y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
|
113 |
-
z=matrix-numpy.
|
114 |
-
m=numpy.block([[z[x,:][:,x],numpy.
|
115 |
-
k=[j if i==len(x) else x[j] if j<len(x) else y[numpy.
|
116 |
h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
|
117 |
-
i=y[numpy.
|
118 |
h[i]=x[k[-1]] if k[-1]<len(x) else i
|
119 |
return h
|
|
|
5 |
def __init__(self,**kwargs):
|
6 |
super().__init__(**kwargs)
|
7 |
x=self.model.config.label2id
|
8 |
+
self.root=numpy.full((len(x)),-numpy.inf)
|
9 |
+
self.left_arc=numpy.full((len(x)),-numpy.inf)
|
10 |
+
self.right_arc=numpy.full((len(x)),-numpy.inf)
|
11 |
for k,v in x.items():
|
12 |
if k.endswith("|root"):
|
13 |
self.root[v]=0
|
|
|
72 |
r=numpy.tri(e.shape[0])
|
73 |
for i in range(e.shape[0]):
|
74 |
for j in range(i+2,e.shape[1]):
|
75 |
+
r[i,j]=r[i,j-1] if numpy.argmax(e[i,j-1])==g else 1
|
76 |
+
e[:,:,g]+=numpy.where(r==0,0,-numpy.inf)
|
77 |
+
m,p=numpy.max(e,axis=2),numpy.argmax(e,axis=2)
|
78 |
h=self.chu_liu_edmonds(m)
|
79 |
z=[i for i,j in enumerate(h) if i==j]
|
80 |
if len(z)>1:
|
81 |
+
k,h=z[numpy.argmax(m[z,z])],numpy.min(m)-numpy.max(m)
|
82 |
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])]
|
83 |
h=self.chu_liu_edmonds(m)
|
84 |
v=[(s,e) for s,e in off if s<e]
|
|
|
99 |
u+="\t".join([str(i+1),t[s:e],"_",q[i][0],"_","_" if len(q[i])<4 else "|".join(q[i][2:-1]),str(0 if h[i]==i else h[i]+1),"root" if q[i][-1]=="root" else q[i][-1][2:],"_","_" if i+1<len(v) and e<v[i+1][0] else "SpaceAfter=No"])+"\n"
|
100 |
return u+"\n"
|
101 |
def chu_liu_edmonds(self,matrix):
|
102 |
+
h=numpy.argmax(matrix,axis=0)
|
103 |
x=[-1 if i==j else j for i,j in enumerate(h)]
|
104 |
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]]:
|
105 |
y=[]
|
|
|
110 |
if max(x)<0:
|
111 |
return h
|
112 |
y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
|
113 |
+
z=matrix-numpy.max(matrix,axis=0)
|
114 |
+
m=numpy.block([[z[x,:][:,x],numpy.max(z[x,:][:,y],axis=1).reshape(len(x),1)],[numpy.max(z[y,:][:,x],axis=0),numpy.max(z[y,y])]])
|
115 |
+
k=[j if i==len(x) else x[j] if j<len(x) else y[numpy.argmax(z[y,x[i]])] for i,j in enumerate(self.chu_liu_edmonds(m))]
|
116 |
h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
|
117 |
+
i=y[numpy.argmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
|
118 |
h[i]=x[k[-1]] if k[-1]<len(x) else i
|
119 |
return h
|