-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
186 lines (143 loc) · 4.75 KB
/
Main.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
# -*- coding: utf-8 -*-
__author__ = 'Poc'
import re
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from sys import stdout
import httplib
import urllib, urllib2
from lxml import etree
from jianfan import jtof
from pprint import pprint
from common_lib import *
import requests
SITE=[["愛詞霸","dict-co.iciba.com","/api/dictionary.php?w="],
["QQ辭典","dict.qq.com","/dict?q="]
]
#http://web.iciba.com/partner/api01.shtml
eng_words=[]
def sanitize_word(word):
rpls_list=['\'',"\"",'.','?','!']
for rpls in rpls_list:
word = word.replace(rpls,'')
return word
def read_file(f_name,eng_words):
f = open(f_name)
lines = f.readlines()
for line in lines:
print('')
print(line)
eng_words.append(line)
r1 = re.compile("(.*?)\s*[(=/]")
m1 = r1.match(line)
if m1 is not None:
word = sanitize_word(str( m1.group(1).strip() ))
translate_word(word)
# eng_pattern="[a-Z]"
# if re.findall(eng_pattern,word.lower()):
# translate_word(word)
# else:
else:
words = line.split()
word = str(' '.join(words))
eng_pattern="[a-zA-Z]"
if re.findall(eng_pattern,word.lower()): # English
translate_word(word)
else: # Chinese
translate_word(word)
def isUni(s):
if isinstance(s, str):
print "ordinary string"
elif isinstance(s, unicode):
print "unicode string"
else:
print "not a string"
def cvt_codec(node):
if hasattr( node.text, 'encode'):
val = jtof(node.text.encode('utf-8'))
# val = (node.text.encode('utf-8'))
else:
val = jtof(node.text)
# print(val)‰
# isUni(val)
return val
def got_sentence(sent_node):
for node in sent_node:
tag = node.tag
val = cvt_codec(node)
if "orig" == tag: # 例句
stdout.write(val.strip())
if "trans" == tag: #翻譯
try:
stdout.write(val.strip())
except:
print("錯誤"+val)
continue
def conn2Site(base_url,api,word):
req_url="http://%s%s\"%s\""%(base_url,api,word)
results = requests.get(req_url,
headers={'User-Agent': 'Mozilla/5.0'})
print(req_url)
print((results.text))
try:
#requests , 遇到中文 會自動變成 iso-8859
return results.text.encode('iso-8859-1')
except Exception as e:
return results.text.encode('utf-8')
def translate_word(word):
dflt_params = conn2Site("dict-co.iciba.com","/api/dictionary.php?w=",word)
root = etree.fromstring("%s" % dflt_params,
parser=etree.XMLParser(recover=True))
if type(root) == type(None):
return -1
if len(root.getchildren()) == 1 :
dflt_params = conn2Site("dict.qq.com","/dict?q=",word)
json_data = json.loads(dflt_params)
if 'err' in json_data:
return
else:
if 'netdes' in json_data:
for ln in json_data['netdes']:
for sub_ln in ln['des']:
try:
if "mf" in sub_ln:
# print("mf")
print str(sub_ln['mf']+" "+jtof(sub_ln['d'])).replace('"','')
else:
print(sub_ln['d'])
except :
pass
if 'netsen' in json_data:
for ln in json_data['netsen']:
continue
print ln['cs']
print re.sub('<[^<]+?>', '', ln['es'])#escape html tag
# for k,v in ln.iteritems():
# print k
# print unicode(ln,'utf-8')
return
child = root.getchildren()
for node in child:
tag = node.tag
val = cvt_codec(node)
if "key"==tag:
print("")
if len(val.split()) >=2 :
stdout.write( "句子/片語:"+str(val)+"\r\n")
elif len(val.split()) == 1 :
stdout.write( "單字:"+str(val)+"\r\n")
else :
print("Error")
if "pos"==tag:
stdout.write( "(%s)"%val)
if "acceptation"==tag:
stdout.write(val)
if "sent" == tag:
got_sentence(node)
def main():
global eng_words
read_file('text.txt',eng_words)
if __name__ == '__main__':
main()