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