-
Notifications
You must be signed in to change notification settings - Fork 0
/
backendapertiummbasic.py
93 lines (92 loc) · 3.24 KB
/
backendapertiummbasic.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
'''
backend for apertium gui
'''
import sys
import os
import json
import subprocess
from requests import get
from PyQt4 import QtCore, QtGui
from apertiumbasicuimain import Ui_MainWindow
LANG_DICT = {"eng":"english",
"epo":"esparanto",
"cym":"welsh",
"kaz":"kazak",
"tat":"tatar",
}
INV_LANG_DICT = {v: k for k, v in LANG_DICT.items()}
class MyForm(QtGui.QMainWindow):
'''
main window setup
'''
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.input,
QtCore.SIGNAL("textChanged(QString)"), self.translate_func)
QtCore.QObject.connect(self.ui.exitbtn,
QtCore.SIGNAL("clicked()"), self.stop_server)
self.add_pairs()
def stop_server(self):
os.chmod('stopserverscript.sh', 0o755)
subprocess.call("./stopserverscript.sh")
self.destroy()
def add_pairs(self):
'''
extract available language pairs
'''
list1 = []
urllink = 'http://127.0.0.1:2737/listPairs'
response = json.loads(get(urllink).text)["responseData"]
for res in response:
langpair = ''
srclang = res["sourceLanguage"]
trglang = res["targetLanguage"]
for key in LANG_DICT.keys():
if key == srclang:
langpair = langpair + LANG_DICT[key]
if langpair == '':
langpair = langpair + res["sourceLanguage"]
langpair = langpair + ' --> '
for key in LANG_DICT.keys():
if key == trglang:
langpair = langpair + LANG_DICT[key]
if langpair[-1:] == ' ':
langpair = langpair + trglang
list1.append(langpair)
self.ui.select1.addItems(list1)
def translate_func(self):
'''
translate text
'''
if self.ui.select1.currentText():
currentpair = str(self.ui.select1.currentText())
text1 = ''
text2 = ''
flag = 1
for k in range(len(currentpair)):
if currentpair[k] != ' ' and flag == 1:
text1 = text1 + currentpair[k]
elif currentpair[k] == ' ':
flag = 0
elif flag == 0:
text2 = text2 + currentpair[k]
text2 = text2[3:]
for key in INV_LANG_DICT.keys():
if key == text1:
text1 = INV_LANG_DICT[text1]
for key in INV_LANG_DICT.keys():
if key == text2:
text2 = INV_LANG_DICT[text2]
urllink = 'http://127.0.0.1:2737/translate?langpair=%s|%s&q=%s' % (text1, text2, self.ui.input.text())
response = json.loads(get(urllink).text)["responseData"]
translatedstring = response["translatedText"]
self.ui.output.setText(translatedstring)
else:
self.ui.output.setText(self.ui.input.text())
if __name__ == '__main__':
APP = QtGui.QApplication(sys.argv)
ex = MyForm()
ex.show()
sys.exit(APP.exec_())