KoichiYasuoka commited on
Commit
3a52f83
·
1 Parent(s): b672690

algorithm improved

Browse files
Files changed (1) hide show
  1. ud.py +14 -13
ud.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from transformers import TokenClassificationPipeline
2
 
3
  class UniversalDependenciesPipeline(TokenClassificationPipeline):
@@ -8,23 +9,24 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
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
  if "logits" not in model_outputs:
13
  return "".join(self.postprocess(x,**kwargs) for x in model_outputs)
14
  e=model_outputs["logits"].numpy()
15
  r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
16
- e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
17
  g=self.model.config.label2id["X|_|goeswith"]
18
- r=numpy.tri(e.shape[0])
19
  for i in range(e.shape[0]):
20
  for j in range(i+2,e.shape[1]):
21
- r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
22
- e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
23
- m,p=numpy.nanmax(e,axis=2),numpy.nanargmax(e,axis=2)
 
 
24
  h=self.chu_liu_edmonds(m)
25
  z=[i for i,j in enumerate(h) if i==j]
26
  if len(z)>1:
27
- k,h=z[numpy.nanargmax(m[z,z])],numpy.nanmin(m)-numpy.nanmax(m)
28
  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])]
29
  h=self.chu_liu_edmonds(m)
30
  t=model_outputs["sentence"].replace("\n"," ")
@@ -64,8 +66,7 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
64
  u=x+"\t".join([str(i+1),"".join(w),l,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"+u
65
  return "# text = "+t+"\n"+u
66
  def chu_liu_edmonds(self,matrix):
67
- import numpy
68
- h=numpy.nanargmax(matrix,axis=0)
69
  x=[-1 if i==j else j for i,j in enumerate(h)]
70
  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]]:
71
  y=[]
@@ -76,10 +77,10 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
76
  if max(x)<0:
77
  return h
78
  y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
79
- z=matrix-numpy.nanmax(matrix,axis=0)
80
- 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])]])
81
- 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))]
82
  h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
83
- i=y[numpy.nanargmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
84
  h[i]=x[k[-1]] if k[-1]<len(x) else i
85
  return h
 
1
+ import numpy
2
  from transformers import TokenClassificationPipeline
3
 
4
  class UniversalDependenciesPipeline(TokenClassificationPipeline):
 
9
  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))
10
  return {"logits":e.logits[:,1:-2,:],**model_inputs}
11
  def postprocess(self,model_outputs,**kwargs):
 
12
  if "logits" not in model_outputs:
13
  return "".join(self.postprocess(x,**kwargs) for x in model_outputs)
14
  e=model_outputs["logits"].numpy()
15
  r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
16
+ e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,-numpy.inf)
17
  g=self.model.config.label2id["X|_|goeswith"]
18
+ m,r=numpy.max(e,axis=2),numpy.tri(e.shape[0])
19
  for i in range(e.shape[0]):
20
  for j in range(i+2,e.shape[1]):
21
+ r[i,j]=1
22
+ if numpy.argmax(e[i,j-1])==g and numpy.argmax(m[:,j-1])==i:
23
+ r[i,j]=r[i,j-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
  t=model_outputs["sentence"].replace("\n"," ")
 
66
  u=x+"\t".join([str(i+1),"".join(w),l,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"+u
67
  return "# text = "+t+"\n"+u
68
  def chu_liu_edmonds(self,matrix):
69
+ h=numpy.argmax(matrix,axis=0)
 
70
  x=[-1 if i==j else j for i,j in enumerate(h)]
71
  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]]:
72
  y=[]
 
77
  if max(x)<0:
78
  return h
79
  y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
80
+ z=matrix-numpy.max(matrix,axis=0)
81
+ 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])]])
82
+ 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))]
83
  h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
84
+ i=y[numpy.argmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
85
  h[i]=x[k[-1]] if k[-1]<len(x) else i
86
  return h