KoichiYasuoka
commited on
Commit
•
8c1a347
1
Parent(s):
edf781f
without ufal.chu-liu.edmonds
Browse files
README.md
CHANGED
@@ -24,70 +24,6 @@ This is a PhoBERT model pre-trained on Vietnamese texts for POS-tagging and depe
|
|
24 |
## How to Use
|
25 |
|
26 |
```py
|
27 |
-
class UDgoeswithViNLP(object):
|
28 |
-
def __init__(self,bert):
|
29 |
-
from transformers import AutoTokenizer,AutoModelForTokenClassification
|
30 |
-
from ViNLP import word_tokenize
|
31 |
-
self.tokenizer=AutoTokenizer.from_pretrained(bert)
|
32 |
-
self.model=AutoModelForTokenClassification.from_pretrained(bert)
|
33 |
-
self.vinlp=word_tokenize
|
34 |
-
def __call__(self,text):
|
35 |
-
import numpy,torch,ufal.chu_liu_edmonds
|
36 |
-
t=self.vinlp(text)
|
37 |
-
w=self.tokenizer(t,add_special_tokens=False)["input_ids"]
|
38 |
-
z=[]
|
39 |
-
for i,j in enumerate(t):
|
40 |
-
if j.find("_")>0 and [k for k in w[i] if k==self.tokenizer.unk_token_id]!=[]:
|
41 |
-
w[i]=self.tokenizer(j.replace("_"," "))["input_ids"][1:-1]
|
42 |
-
if [k for k in w[i] if k==self.tokenizer.unk_token_id]!=[]:
|
43 |
-
w[i]=[self.tokenizer.unk_token_id]
|
44 |
-
z.append(j)
|
45 |
-
v=[self.tokenizer.cls_token_id]+sum(w,[])+[self.tokenizer.sep_token_id]
|
46 |
-
x=[v[0:i]+[self.tokenizer.mask_token_id]+v[i+1:]+[j] for i,j in enumerate(v[1:-1],1)]
|
47 |
-
with torch.no_grad():
|
48 |
-
e=self.model(input_ids=torch.tensor(x)).logits.numpy()[:,1:-2,:]
|
49 |
-
r=[1 if i==0 else -1 if j.endswith("|root") else 0 for i,j in sorted(self.model.config.id2label.items())]
|
50 |
-
e+=numpy.where(numpy.add.outer(numpy.identity(e.shape[0]),r)==0,0,numpy.nan)
|
51 |
-
g=self.model.config.label2id["X|_|goeswith"]
|
52 |
-
r=numpy.tri(e.shape[0])
|
53 |
-
for i in range(e.shape[0]):
|
54 |
-
for j in range(i+2,e.shape[1]):
|
55 |
-
r[i,j]=r[i,j-1] if numpy.nanargmax(e[i,j-1])==g else 1
|
56 |
-
e[:,:,g]+=numpy.where(r==0,0,numpy.nan)
|
57 |
-
m=numpy.full((e.shape[0]+1,e.shape[1]+1),numpy.nan)
|
58 |
-
m[1:,1:]=numpy.nanmax(e,axis=2).transpose()
|
59 |
-
p=numpy.zeros(m.shape)
|
60 |
-
p[1:,1:]=numpy.nanargmax(e,axis=2).transpose()
|
61 |
-
for i in range(1,m.shape[0]):
|
62 |
-
m[i,0],m[i,i],p[i,0]=m[i,i],numpy.nan,p[i,i]
|
63 |
-
h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
|
64 |
-
if [0 for i in h if i==0]!=[0]:
|
65 |
-
m[:,0]+=numpy.where(m[:,0]==numpy.nanmax(m[[i for i,j in enumerate(h) if j==0],0]),0,numpy.nan)
|
66 |
-
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)]
|
67 |
-
h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
|
68 |
-
u="# text = "+text+"\n"
|
69 |
-
q=[self.model.config.id2label[p[i,j]].split("|") for i,j in enumerate(h)]
|
70 |
-
t=[i.replace("_"," ") for i in t]
|
71 |
-
if len(t)!=len(v)-2:
|
72 |
-
t=[z.pop(0) if i==self.tokenizer.unk_token else i.replace("_"," ") for i in self.tokenizer.convert_ids_to_tokens(v[1:-1])]
|
73 |
-
for i,j in reversed(list(enumerate(q[2:],2))):
|
74 |
-
if j[-1]=="goeswith" and set([k[-1] for k in q[h[i]+1:i+1]])=={"goeswith"}:
|
75 |
-
h=[b if i>b else b-1 for a,b in enumerate(h) if i!=a]
|
76 |
-
t[i-2]=(t[i-2][0:-2] if t[i-2].endswith("@@") else t[i-2]+" ")+t.pop(i-1)
|
77 |
-
q.pop(i)
|
78 |
-
t=[i[0:-2].strip() if i.endswith("@@") else i.strip() for i in t]
|
79 |
-
for i,j in enumerate(t,1):
|
80 |
-
u+="\t".join([str(i),j,"_",q[i][0],"_","|".join(q[i][1:-1]),str(h[i]),q[i][-1],"_","_"])+"\n"
|
81 |
-
return u+"\n"
|
82 |
-
|
83 |
-
nlp=UDgoeswithViNLP("KoichiYasuoka/phobert-base-vietnamese-ud-goeswith")
|
84 |
-
print(nlp("Hai cái đầu thì tốt hơn một."))
|
85 |
-
```
|
86 |
-
|
87 |
-
with [ufal.chu-liu-edmonds](https://pypi.org/project/ufal.chu-liu-edmonds/) and [ViNLP](https://pypi.org/project/ViNLP/).
|
88 |
-
Or without them:
|
89 |
-
|
90 |
-
```
|
91 |
from transformers import pipeline
|
92 |
nlp=pipeline("universal-dependencies","KoichiYasuoka/phobert-base-vietnamese-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
|
93 |
print(nlp("Hai cái đầu thì tốt hơn một."))
|
|
|
24 |
## How to Use
|
25 |
|
26 |
```py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
from transformers import pipeline
|
28 |
nlp=pipeline("universal-dependencies","KoichiYasuoka/phobert-base-vietnamese-ud-goeswith",trust_remote_code=True,aggregation_strategy="simple")
|
29 |
print(nlp("Hai cái đầu thì tốt hơn một."))
|