-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_engine_translator.py
281 lines (248 loc) · 9.19 KB
/
multi_engine_translator.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
__author__ = 'PGY'
import sys
import os.path
import re
import requests
import threading
import urllib.parse
from bs4 import BeautifulSoup
from collections import deque
class fetch_page:
user_agent = 'mozilla/4.0 (compatible; msie 5.5; windows nt)'
headers = {'user-agent' : user_agent}
@classmethod
def getpage(cls, url, keywords):
try:
response = requests.get(url, keywords, headers = cls.headers)
content = response.text
return content
except:
e = sys.exc_info()
print(e)
return None
@classmethod
def getsoup(cls, content):
if not content:
print('Load page failed!')
return None
else:
soup = BeautifulSoup(content, "lxml")
return soup
class strformator:
@staticmethod
def keywordsdict(**kwargs):
return kwargs
@staticmethod
def mergeurl(mainurl, path):
return urllib.parse.urljoin(mainurl, path)
class google:
def __init__(self, inlang, outlang, theword):
self.inlang = inlang
self.outlang = outlang
self.theword = theword
# --------------------------
self.mainurl = 'http://translate.google.com/'
self.path = 'm'
self.keywords = strformator.keywordsdict(sl = self.inlang, tl = self.outlang, ie = 'UTF-8', q = self.theword)
# -------------------------
self.url = strformator.mergeurl(self.mainurl, self.path)
self.page = fetch_page.getpage(self.url, self.keywords)
def getanswer(self):
mark='class="t0">'
content = self.page
if not content:
print('Load page failed!')
return None
else:
startpos = content.index(mark)
remaincont = content[content.find(mark)+len(mark):]
result = remaincont.split('<')[0]
return result
def format_inword(self):
inwordfull = self.theword
return inwordfull
class linguee:
def __init__(self, inlang, outlang, theword):
self.inlang = inlang
self.outlang = outlang
self.theword = theword
# --------------------------
self.mainurl = 'http://www.linguee.com/'
self.path = '/'.join(('-'.join((inlang, outlang)), 'search'))
self.keywords = strformator.keywordsdict(source = 'auto', query = self.theword)
# -------------------------
self.url = strformator.mergeurl(self.mainurl, self.path)
self.page = fetch_page.getpage(self.url, self.keywords)
self.soup = fetch_page.getsoup(self.page)
self.genderdict = {
'masculine' : 'der',
'feminine' : 'die',
'neuter' : 'das',
'plural' : 'die',
'X' : ''
}
def getinword_frompage(self):
try:
inword_tag = self.soup.find('span', class_ = 'dictTerm').string
except AttributeError as e:
inword_tag = self.theword
return inword_tag
def getanswer(self):
answer_tag_list = self.soup.find_all('a', class_ = 'dictLink')
if answer_tag_list and len(answer_tag_list) > 1:
short_tag_list = answer_tag_list[:2]
answerstr = '; '.join([tag.string for tag in short_tag_list])
else:
try:
answerstr = answer_tag_list[0].string
except IndexError:
answerstr = None
return answerstr
def gettype(self):
try:
typestr = self.soup.find('span', class_ = 'tag_wordtype').string
except AttributeError:
typestr = None
return typestr
def format_inword(self):
inword = self.getinword_frompage()
typestr = self.gettype()
if typestr and 'noun' in typestr:
wordtype = typestr.split(',')[0].strip()
noun_gender = typestr.split(',')[-1].strip()
noun_gender_mark = self.genderdict.get(noun_gender, 'X')
inwordfull = '{} {} ({})'.format(noun_gender_mark, inword, wordtype)
elif typestr and ',' in typestr:
wordtype = typestr.split(',')[0].strip()
inwordfull = '{} ({})'.format(inword, wordtype)
elif typestr:
wordtype = typestr
inwordfull = '{} ({})'.format(inword, wordtype)
else:
inwordfull = inword
return inwordfull
class TransCrawler:
'''Web Crawler to get the translated word from google translation'''
def __init__(self, inlang, outlang, theword):
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
self.headers = {'User-Agent' : self.user_agent}
self.inlang = inlang
self.outlang = outlang
self.theword = theword
self.keywords ={'sl': self.inlang, 'tl': self.outlang, 'ie':'UTF-8', 'q': self.theword}
self.url = 'http://translate.google.com/m'
def getPage(self):
url = self.url
keywords = self.keywords
try:
response = requests.get(url, keywords, headers = self.headers)
content = response.text
return content
except requests.exceptions.RequestException as e:
print(e)
return None
def getWord(self):
mark='class="t0">'
content = self.getPage()
if not content:
print('Load page failed!')
return None
else:
#return content
startpos = content.index(mark)
remaincont = content[content.find(mark)+len(mark):]
result = remaincont.split('<')[0]
return result
# This function is useful only when multithread is switch off
# Because multithread will destroy the order later
# If set() is slower than O(n), this can be used for the speed purpose
# def del_dups(seq):
# '''function to delete duplicate while preserve order'''
# seen = {}
# newlist = []
# for item in seq:
# if item not in seen:
# seen[item] = True
# newlist.append(item)
# return newlist
def html_decode(s):
"""
Returns the ASCII decoded version of the given HTML string. This does
NOT remove normal HTML tags like <p>.
"""
htmlCodes = (
("'", '''),
('"', '"'),
('>', '>'),
('<', '<'),
('&', '&')
)
for code in htmlCodes:
s = s.replace(code[1], code[0])
return s
def is_sentance(instr):
if " " in instr.strip():
return True
else:
return False
def transword_writeoutput(inword, inlang, outlang_list, outfilename):
'''read a word string and save the input word and the output translation into a csv file'''
output_list = []
if 'en' in outlang_list and not is_sentance(inword):
trans = linguee(inlang, 'en', inword)
firstelement = trans.format_inword()
else:
firstelement = inword
output_list.append(firstelement)
for lan in outlang_list:
if lan is 'en' and not is_sentance(inword):
transnew = linguee(inlang, lan, inword)
output_list.append(transnew.getanswer())
else:
transnew = google(inlang, lan, inword)
output_list.append(transnew.getanswer())
print(inword, output_list)
outstr = ",".join(output_list) + ",\n"
outstrparsed = html_decode(outstr)
with open(outfilename, 'a', encoding='utf-8') as text_file:
text_file.write(outstrparsed)
# get the input word list
inputfile = sys.argv[1]
dateregex = re.compile('\d{2}\.\d{2}\.\d{4}')
# if the file is saved from AutoNotes then it is a string
# read a string from a file
if '_AutoNotes' in inputfile:
with open(inputfile, 'r', encoding = 'utf-8') as f:
first_line = f.readline()
fieldsepstr = dateregex.search(first_line).group()
inwordlist = first_line.split(fieldsepstr)[1].split()
# if the file is self-created then it contains multi-lines with empty lines
# read the the lines and ignore the empty lines and the header (the line containing dateregex)
else:
with open(inputfile, 'r', encoding = 'utf-8') as f:
linesgen = (line.rstrip() for line in f)
inwordlist = [line for line in linesgen if line and not dateregex.search(line)]
# Delete duplicate in the input list
# Preserve order is only useful when multithread is switch off
# Not sure about the BigO of set(a_list), if it is O(n*log(n))
# del_dups can be used to speed up
#unique_inwordlist = del_dups(inwordlist) # BigO -> O(n)
unique_inwordlist = list(set(inwordlist)) # BigO -> set(a_list): O(n*log(n)) or O(n)???
# define output file name
outfilename = os.path.splitext(inputfile)[0] + '_GoAnki.csv'
# if the output already exists in current direcotry, remove it. Otherwise do nothing.
try:
os.remove(outfilename)
except OSError:
pass
# translate the words in word list and save the results in a csv file with multithread (speed up)
jobs = []
inlang = 'de'
outlang = ['en', 'zh']
for word in unique_inwordlist:
thread = threading.Thread(target=transword_writeoutput, args = (word,), kwargs = strformator.keywordsdict(inlang = inlang, outlang_list = outlang, outfilename = outfilename))
jobs.append(thread)
for j in jobs:
j.start()
for j in jobs:
j.join()