-
Notifications
You must be signed in to change notification settings - Fork 32
/
translate.py
46 lines (35 loc) · 1.12 KB
/
translate.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
import subprocess
import re
class Blurb(object):
def __init__(self, x, y, w, h, text, confidence=100.0):
self.x=x
self.y=y
self.w=w
self.h=h
self.text = text
self.confidence = confidence
def clean_text(self):
text = self.text
text = re.sub(r"\n", "", text)
return text
def __unicode__(self):
return str(self.x)+','+str(self.y)+' '+str(self.w)+'x'+str(self.h)+' '+ str(self.confidence)+'% :'+ self.text
class TranslatedBlurb(Blurb):
def __init__(self, x, y, w, h, text, confidence, translation):
Blurb.__init__(self, x, y, w, h, text, confidence)
self.translation = translation
@classmethod
def as_translated(cls, parent, translation):
return cls(parent.x,
parent.y,
parent.w,
parent.h,
parent.text,
parent.confidence,
translation)
def translate_text(text):
bs = subprocess.check_output(["node", "translate.js", text])
return bs
def translate_blurb(blurb):
translation = translate_text(blurb.clean_text())
return TranslatedBlurb.as_translated(blurb, translation)