-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtrans.py
executable file
·247 lines (217 loc) · 7.88 KB
/
gtrans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import os
import json
from urllib.parse import quote_plus
import json
import sys
import requests
S_GOOGLEAPIS="translate.googleapis.com"
S_CLIENTS5="clients5.google.com"
S_GOOGLECOM="translate.google.com"
S_GOOGLECH="translate.google.ch"
debug_mode = hasattr(sys, 'gettrace') and sys.gettrace()
def b(service,path):
if path.startswith("/"):path=path[1:]
if path.endswith("/"):path=path[:1]
base=os.path.join(service,path)
if not base.startswith("http"):
base="https://"+base
return base
def getitem(obj,indexs):
for index in indexs:
obj=obj[index]
return obj
def setitem(obj,indexs,value):
getitem(obj,indexs[:-1])[indexs[-1]]=value
def analyse_json(j):
with open("debug.json","w") as f:
json.dump(j,f,indent=4)
return
class GoogleTrans:
def __init__(self,service=S_GOOGLECH,client=["t","gtx","dict-chrome-ex"],path="translate_a/single",input_encoding="UTF-8",output_encoding="UTF-8"):
self.service=service
self.client=[client] if isinstance(client,str) else client
self.path=path
self.input_encoding=input_encoding
self.output_encoding=output_encoding
def _request(self,**params):
base=b(self.service,self.path)
params["ie"]=self.input_encoding
params["oe"]=self.output_encoding
params["dj"]="1"
p="&".join(map(lambda i:(str(i[0])+"="+quote_plus(i[1])),params.items()))
for c in self.client.copy():
url=base+"?client="+c+"&"+p
if debug_mode:
print(url)
r=requests.get(url)
try:
r.raise_for_status()
except Exception as err:
error=err
self.client.append(self.client.pop(0))
continue
j=json.loads(r.text.replace("\u200b",""))
try:
t=j[0]
except:t=None
if isinstance(t,str):return t
return j
raise error
def translate(self,text,dest="en",src="auto",alternative=False):
j=self._request(sl=src,tl=dest,dt=("at" if alternative else "t"),q=text)
if isinstance(j,str):return j
if alternative:
tr=[]
for e in j["alternative_translations"][0]["alternative"]:
tr.append(e["word_postproc"])
return tr
return j["sentences"][0]["trans"]
def transcription(self,word,src="auto"):
j=self._request(sl=src,dt="rm",q=word)
try:
return j["sentences"][0]["src_translit"]
except:
analyse_json(j)
return
def translations(self,text,dest="en",src="auto"):
j=self._request(sl=src,tl=dest,dt="bd",q=text)
d={}
for e in j["dict"][0]["entry"]:
d[e["word"]]=e["reverse_translation"]
return d
def definitions(self,word,src="auto",hl="src",defs=True,syns=True,examples=True,seealso=True):
global df,dr
hl=src if hl=="src" else hl
def add(id,**params):
def rve(indexs,a,v):
if a!=v:
raise ValueError("in add("+repr(id)+") indexs="+repr(indexs)+" "+repr(a)+"!="+repr(v))
def _add(indexs=[]):
pi=getitem(params,indexs)
if not pi:return
diex=True
try:
di=getitem(d,indexs)
assert di!=None
except:
di=None
diex=False
if isinstance(pi,dict) and isinstance(di,dict):
for k in pi:
if k==None or k in ("gram_class","new"):continue
_add(indexs+[k])
return
if isinstance(pi,tuple) and isinstance(di,tuple):
pi=di+pi
diex=False
if diex:
rve(indexs,pi,di)
else:
setitem(d,indexs,pi)
n="new" in params and params["new"]
gc="gram_class" in params
#print((id,params["gram_class"] if gc else "",len(dr)))
dex=True
try:
d=df[id]
except:
d=df[id]=(len(dr)-(0 if n else 1),{})
dex=False
d=d[1]
if n:
dd={"gram_class":None,"definitions":[d]}
if gc:
dd["gram_class"]=params["gram_class"]
else:
del dd["gram_class"]
dr.append(dd)
elif dr:
dr[-1]["definitions"].append(d)
else:
#if gc:
# a=dr[d[0]]["gram_class"]
# if a!=params["gram_class"]:
# rve("gram_class",a,params["gram_class"])
d=d[1]
_add()
def get_labels():
la=[]
ls=[]
if "label_info" not in d:return la,ls
l=d["label_info"]
for label in l.get("register",[])+l.get("labels",[]):
r=label=="rare"
try:
i=df[id][1]
except:
b=True
if r:print("WWW")
else:
try:
l=i["labels"]
except:b=False
else:
if label in l:
if r:print("SSSS")
b=True
else:
b=False
if r:print("DDDDDD")
if r:print((b,"EEEEEE"))
if r and not b:print("TTTT")
#print((b,id not in df))
(la if b else ls).append(label)
return la,ls
#return []
def notls():
if ls:raise ValueError(repr(id)+" has a synonym label")
df={}
dr=[]
if defs:
j=self._request(sl=src,dt="md",q=word,hl=src)
for e in j["definitions"]:
g=e["pos"]
new=1
for d in e["entry"]:
id=d["definition_id"]
la,ls=get_labels()
notls()
add(id,gram_class=g,labels=la,definition=d["gloss"],new=new)
new=0
if syns:
j=self._request(sl=src,dt="ss",q=word,hl=src)
#mypkg.open_data(j)
for e in j["synsets"] if "synsets" in j else []:
g=e["pos"]
new=1
for d in e["entry"]:
id=d["definition_id"]
la,ls=get_labels()
a=[]
syns=d["synonym"]
add(id,gram_class=g,labels=la,synonyms={",".join(ls):syns})
new=0
if examples:
j=self._request(sl=src,dt="ex",q=word)
analyse_json(j)
#mypkg.open_data(j)
if "examples" in j:
for d in j["examples"]["example"]:
id=d["definition_id"]
la,ls=get_labels()
notls()
add(id,labels=la,examples=(d["text"].replace("<b>","").replace("</b>",""),),new=1)
dr={"Word":word,"Definitions":dr}
sa=None
if seealso:
j=self._request(sl=src,dt="rw",q=word)
if "related_words" in j:
sa=j["related_words"]["word"]
if sa:
dr["Seealso"]=sa
return dr
t=GoogleTrans()
#df=t.definitions("salut","fr")
#mypkg.open_data(df)
#print(t.translate("Unité de mesure",alternative=True))
#print(t.translate("Bonjour"))