KoichiYasuoka commited on
Commit
8c8de0e
·
1 Parent(s): 8c1b6a2

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
  v=[(s,e) for s,e in model_outputs["offset_mapping"][0].tolist() if s<e]
@@ -42,8 +44,7 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
42
  u+="\t".join([str(i+1),t[s:e],t[s:e] if g else "_",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"
43
  return u+"\n"
44
  def chu_liu_edmonds(self,matrix):
45
- import numpy
46
- h=numpy.nanargmax(matrix,axis=0)
47
  x=[-1 if i==j else j for i,j in enumerate(h)]
48
  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]]:
49
  y=[]
@@ -54,10 +55,10 @@ class UniversalDependenciesPipeline(TokenClassificationPipeline):
54
  if max(x)<0:
55
  return h
56
  y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
57
- z=matrix-numpy.nanmax(matrix,axis=0)
58
- 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])]])
59
- 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))]
60
  h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
61
- i=y[numpy.nanargmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
62
  h[i]=x[k[-1]] if k[-1]<len(x) else i
63
  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
  v=[(s,e) for s,e in model_outputs["offset_mapping"][0].tolist() if s<e]
 
44
  u+="\t".join([str(i+1),t[s:e],t[s:e] if g else "_",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"
45
  return u+"\n"
46
  def chu_liu_edmonds(self,matrix):
47
+ h=numpy.argmax(matrix,axis=0)
 
48
  x=[-1 if i==j else j for i,j in enumerate(h)]
49
  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]]:
50
  y=[]
 
55
  if max(x)<0:
56
  return h
57
  y,x=[i for i,j in enumerate(x) if j==max(x)],[i for i,j in enumerate(x) if j<max(x)]
58
+ z=matrix-numpy.max(matrix,axis=0)
59
+ 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])]])
60
+ 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))]
61
  h=[j if i in y else k[x.index(i)] for i,j in enumerate(h)]
62
+ i=y[numpy.argmax(z[x[k[-1]],y] if k[-1]<len(x) else z[y,y])]
63
  h[i]=x[k[-1]] if k[-1]<len(x) else i
64
  return h