KoichiYasuoka
commited on
Commit
•
dacea24
1
Parent(s):
08a9c4f
without ufal.chu-liu.edmonds
Browse files
README.md
CHANGED
@@ -22,52 +22,6 @@ This is a RoBERTa model for POS-tagging and dependency-parsing (using `goeswith`
|
|
22 |
## How to Use
|
23 |
|
24 |
```py
|
25 |
-
class UDgoeswith(object):
|
26 |
-
def __init__(self,bert):
|
27 |
-
from transformers import AutoTokenizer,AutoModelForTokenClassification
|
28 |
-
self.tokenizer=AutoTokenizer.from_pretrained(bert)
|
29 |
-
self.model=AutoModelForTokenClassification.from_pretrained(bert)
|
30 |
-
def __call__(self,text):
|
31 |
-
import numpy,torch,ufal.chu_liu_edmonds
|
32 |
-
w=self.tokenizer(text,return_offsets_mapping=True)
|
33 |
-
v=[self.tokenizer.cls_token_id]+[t for t,(s,e) in zip(w["input_ids"],w["offset_mapping"]) if s<e]+[self.tokenizer.sep_token_id]
|
34 |
-
x=[v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[j] for i,j in enumerate(v[1:-1],1)]
|
35 |
-
with torch.no_grad():
|
36 |
-
e=self.model(input_ids=torch.tensor(x)).logits.numpy()[:,1:-2,:]
|
37 |
-
r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
|
38 |
-
e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
|
39 |
-
g=self.model.config.label2id["X|_|goeswith"]
|
40 |
-
r=numpy.tri(e.shape[0])
|
41 |
-
for i in range(e.shape[0]):
|
42 |
-
for j in range(i+2,e.shape[1]):
|
43 |
-
r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
|
44 |
-
e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
|
45 |
-
m=numpy.full((e.shape[0]+1,e.shape[1]+1),numpy.nan)
|
46 |
-
m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
|
47 |
-
p=numpy.zeros(m.shape)
|
48 |
-
p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
|
49 |
-
for i in range(1,m.shape[0]):
|
50 |
-
m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
|
51 |
-
h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
|
52 |
-
if [0 for i in h if i==0]!=[0]:
|
53 |
-
m[:,0]+=numpy.where(m[:,0]==numpy.nanmax(m[[i for i,j in enumerate(h) if j==0],0]),0,numpy.nan)
|
54 |
-
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)]
|
55 |
-
h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
|
56 |
-
u="# text = "+text+"\n"
|
57 |
-
v=[(s,e) for s,e in w["offset_mapping"] if s<e]
|
58 |
-
for i,(s,e) in enumerate(v,1):
|
59 |
-
q=self.model.config.id2label[p[i,h[i]]].split("|")
|
60 |
-
u+="\t".join([str(i),text[s:e],"_",q[0],"_","|".join(q[1:-1]),str(h[i]),q[-1],"_","_" if i<len(v) and e<v[i][0] else "SpaceAfter=No"])+"\n"
|
61 |
-
return u+"\n"
|
62 |
-
|
63 |
-
nlp=UDgoeswith("KoichiYasuoka/roberta-base-english-ud-goeswith")
|
64 |
-
print(nlp("I saw a horse yesterday which had no name"))
|
65 |
-
```
|
66 |
-
|
67 |
-
with [ufal.chu-liu-edmonds](https://pypi.org/project/ufal.chu-liu-edmonds/).
|
68 |
-
Or without ufal.chu-liu-edmonds:
|
69 |
-
|
70 |
-
```
|
71 |
from transformers import pipeline
|
72 |
nlp=pipeline("universal-dependencies","KoichiYasuoka/roberta-base-english-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
|
73 |
print(nlp("I saw a horse yesterday which had no name"))
|
|
|
22 |
## How to Use
|
23 |
|
24 |
```py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
from transformers import pipeline
|
26 |
nlp=pipeline("universal-dependencies","KoichiYasuoka/roberta-base-english-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
|
27 |
print(nlp("I saw a horse yesterday which had no name"))
|