KoichiYasuoka commited on
Commit
ccb418b
·
1 Parent(s): d4e71e8

algorithm improved

Browse files
Files changed (1) hide show
  1. ud.py +11 -12
ud.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from transformers import TokenClassificationPipeline
2
 
3
  class UniversalDependenciesPipeline(TokenClassificationPipeline):
@@ -10,23 +11,22 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
10
  def check_model_type(self,supported_models):
11
  pass
12
  def postprocess(self,model_outputs,**kwargs):
13
- import numpy
14
  if "logits" not in model_outputs:
15
  return "".join(self.postprocess(x,**kwargs) for x in model_outputs)
16
  e=model_outputs["logits"].numpy()
17
  r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
18
- e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
19
  g=self.model.config.label2id["X|_|goeswith"]
20
  r=numpy.tri(e.shape[0])
21
  for i in range(e.shape[0]):
22
  for j in range(i+2,e.shape[1]):
23
- r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
24
- e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
25
- m,p=numpy.nanmax(e,axis=2),numpy.nanargmax(e,axis=2)
26
  h=self.chu_liu_edmonds(m)
27
  z=[i for i,j in enumerate(h) if i==j]
28
  if len(z)>1:
29
- k,h=z[numpy.nanargmax(m[z,z])],numpy.nanmin(m)-numpy.nanmax(m)
30
  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])]
31
  h=self.chu_liu_edmonds(m)
32
  v=[(s,e) for s,e in model_outputs["offset_mapping"][0].tolist() if s<e]
@@ -61,8 +61,7 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
61
  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"
62
  return u+"\n"
63
  def chu_liu_edmonds(self,matrix):
64
- import numpy
65
- h=numpy.nanargmax(matrix,axis=0)
66
  x=[-1 if i==j else j for i,j in enumerate(h)]
67
  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]]:
68
  y=[]
@@ -73,10 +72,10 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
73
  if max(x)<0:
74
  return h
75
  y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
76
- z=matrix-numpy.nanmax(matrix,axis=0)
77
- 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])]])
78
- 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))]
79
  h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
80
- i=y[numpy.nanargmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
81
  h[i]=x[k[-1]] if k[-1]<len(x) else i
82
  return h
 
1
+ import numpy
2
  from transformers import TokenClassificationPipeline
3
 
4
  class UniversalDependenciesPipeline(TokenClassificationPipeline):
 
11
  def check_model_type(self,supported_models):
12
  pass
13
  def postprocess(self,model_outputs,**kwargs):
 
14
  if "logits" not in model_outputs:
15
  return "".join(self.postprocess(x,**kwargs) for x in model_outputs)
16
  e=model_outputs["logits"].numpy()
17
  r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
18
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,-numpy.inf)
19
  g=self.model.config.label2id["X|_|goeswith"]
20
  r=numpy.tri(e.shape[0])
21
  for i in range(e.shape[0]):
22
  for j in range(i+2,e.shape[1]):
23
+ r[i,j]=r[i,j-1] if numpy.argmax(e[i,j-1])==g else 1
24
+ e[:,:,g]+=numpy.where(r==0,0,-numpy.inf)
25
+ m,p=numpy.max(e,axis=2),numpy.argmax(e,axis=2)
26
  h=self.chu_liu_edmonds(m)
27
  z=[i for i,j in enumerate(h) if i==j]
28
  if len(z)>1:
29
+ k,h=z[numpy.argmax(m[z,z])],numpy.min(m)-numpy.max(m)
30
  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])]
31
  h=self.chu_liu_edmonds(m)
32
  v=[(s,e) for s,e in model_outputs["offset_mapping"][0].tolist() if s<e]
 
61
  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"
62
  return u+"\n"
63
  def chu_liu_edmonds(self,matrix):
64
+ h=numpy.argmax(matrix,axis=0)
 
65
  x=[-1 if i==j else j for i,j in enumerate(h)]
66
  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]]:
67
  y=[]
 
72
  if max(x)<0:
73
  return h
74
  y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
75
+ z=matrix-numpy.max(matrix,axis=0)
76
+ 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])]])
77
+ 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))]
78
  h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
79
+ i=y[numpy.argmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
80
  h[i]=x[k[-1]] if k[-1]<len(x) else i
81
  return h