-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.py
152 lines (141 loc) · 5.1 KB
/
dictionary.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
#!/usr/bin/env python
# coding:utf-8
"""
Author: G.K.
Purpose:
Created: 2016/3/3
"""
import json
import urllib.request as urllib2
"""
API key: 779070219
keyfrom: GK-Blog
doctype: json
"""
api = u"http://fanyi.youdao.com/openapi.do?keyfrom=GK-Blog&key=779070219&type=data&doctype=json&version=1.1&q="
somo = u"(ಥ_ಥ)"
bq = """
-@o +}
#dM BW
$#W ha
W ?)@
&W W+}
8#v ?#8
&m& +L#88m 8b
M%h + ~ZBM
IamGK* + @#&WIamGK
8 B 8x &
+ & M + (k o+
# 8&
W + 8
+ 8 + W}
8 ~ &%%%B88fdb&W d&&kW&B&[u+ %
# M8 8 b % 8_ B
# %W # M
B% 8~ Bo@
@8 &jm W&+
@8 m ~ &W 8
%c /MM j8 #
+ ~& #8&&8&8&#&M&#Wm#&&M&8*~ ]
~ W8 M&
8 8 r]_ &#
# & # M *}8
* 8 8- M M o
8+ ML M_#W%Mu+% W B
W%& qbbWBB/W #0
818 kQoo%h8# 8
%&W
"""
def translate(word):
t_api = api + urllib2.quote(word)
out = ""
try:
content = urllib2.urlopen(t_api, timeout=3).read()
except:
# print "There might be a network problem.\nPlease try later!"
out = somo + u" There might be a network problem.\nPlease try later!\n"
out += bq
return out
try:
content = json.loads(content)
out = parse(content)
except:
# print "There might be an unhandled exception.\nPlease try later!"
out = somo + u" There might be an unhandled exception.\nPlease try later!\n"
out += bq
return out
def tryget(content, strs):
cur = content
for ss in strs:
try:
cur = cur[ss]
except KeyError:
return None
return cur
def parse(content):
code = content['errorCode']
out = u""
if code == 0: # Success
u = tryget(content, ['basic', 'us-phonetic'])
e = tryget(content, ['basic', 'uk-phonetic'])
explains = tryget(content, ['basic', 'explains'])
# print content['query'], ':', content['translation'][0]
if content['query'] == content['translation'][0]:
out += somo + u" Sorry, no translation result !\n"
out += bq
else:
out += content['query']
out += " : "
out += content['translation'][0]
out += "\n"
if u is not None:
out += "US: ["
out += u
out += "] "
if e is not None:
out += "UK: ["
out += e
out += "]\n"
if explains is not None:
out += "----------------------------------------\n"
out += '[Explains]\n'
for i in range(len(explains)):
out += " "
out += explains[i]
out += "\n"
web = tryget(content, ['web'])
if web is not None:
out += "----------------------------------------\n"
out += "[More..]\n"
for eachweb in web:
key = eachweb["key"]
values = eachweb["value"]
out += key + ": "
for v in values:
out += v + ", "
out = out[:-2]
out += "\n"
elif code == 20:
# print "The Input is too long!"
out += somo + u" The Input is too long!\n"
out += bq
elif code == 30:
# print "Failed to translate!"
out += somo + u" Failed to translate!\n"
out += bq
elif code == 40:
# print "Unsupported language!"
out += somo + u" Unsupported language!\n"
out += bq
elif code == 50:
# print "Unsupported input!"
out += somo + u" Unsupported input!\n"
out += bq
elif code == 60:
# print "Sorry, no translation result!"
out += somo + u" Sorry, no translation result !\n"
out += bq
return out
if __name__ == "__main__":
word = "smile"
translate(word)