-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPTDictionary.py
343 lines (275 loc) · 12.4 KB
/
PTDictionary.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import sys
from PySide2.QtCore import SIGNAL, Slot
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QWidget, QTextEdit, QStatusBar, QGridLayout, QShortcut, QAction, QMainWindow, QLabel
from PySide2.QtWidgets import QApplication, QPushButton, QLineEdit, QMessageBox, QToolButton, QToolBar, QCompleter
from PySide2 import QtGui, QtCore
import sqlite3
class HTMLHelper():
def __init__(self):
self.word = ""
self.meanings = ""
self.meaninglist = []
self.htmlstring = ""
def getHTMLword(self, word):
self.word = "<div><span style='font-weight:bold;font-size:16px;'>"
self.word = self.word + word
self.word = self.word + "</span> ~ <span style='color:red; font-style: italic;'>noun</span></div>"
return self.word
def setmeaningslist(self, meanings):
self.meaninglist = meanings.split('; ')
def getHTMLmeanings(self, mlist): #அக்காரம்
if((mlist.__sizeof__()) == 1):
self.htmlstring = "<div>"
self.htmlstring = self.htmlstring + "<span style='font-weight:bold;color:lightgray'> " + 1 + ". " + "</span>"
self.htmlstring = self.htmlstring + "<span style='font-size:14px;'>" + mlist[0] + "</span><br/>"
self.htmlstring = self.htmlstring + "</div>"
else:
self.htmlstring = "<div>"
for x in range(len(mlist)):
self.htmlstring = self.htmlstring + "<span style='font-weight:bold;color:lightgray'> "+ str(x+1) +". " +"</span>"
self.htmlstring = self.htmlstring + "<span style='font-size:14px;'>" + mlist[x] +"</span><br/>"
self.htmlstring = self.htmlstring + "</div>"
return self.htmlstring
class DBHelper:
def __init__(self):
self.conn = sqlite3.connect("PTTamil.db")
self.c = self.conn.cursor()
self.c.execute("PRAGMA encoding='UTF-8'")
def closeconn(self):
self.c.close()
self.conn.close()
def getconn(self):
return self.conn
def getwordMeaning(self, word):
self.c.execute("SELECT meanings from dictionary where word = ?", (word,))
trow = self.c.fetchone()
while trow is not None:
return trow[0]
def checkwordpresent(self, word):
self.c.execute("SELECT count(*) from dictionary where word = ?", (word,))
trow = self.c.fetchone()
print(trow)
while trow is not None:
return trow[0]
class ClickableLineEdit(QLineEdit):
clicked = QtCore.Signal()
def mousePressEvent(self, event):
super(ClickableLineEdit, self).mousePressEvent(event)
self.clicked.emit()
class myWindow(QMainWindow):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
clicked = QtCore.Signal()
self.primed = 0
self.pointer = None
self.histlist = []
self.HIST_SIZE = 10
self.tb = self.addToolBar("File")
self.tb.setMovable(False)
self.tb.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.tb.actionTriggered[QAction].connect(self.toolbtnpressed)
self.previous = QAction(QIcon("./imgs/56.png"), "Previous", self)
self.tb.addAction(self.previous)
self.next = QAction(QIcon("./imgs/57.png"), "Next", self)
self.tb.addAction(self.next)
self.options = QAction(QIcon("./imgs/46.png"), "Options", self)
self.tb.addAction(self.options)
self.about = QAction(QIcon("./imgs/48.png"), "About", self)
self.about.triggered.connect(self._show_about)
self.tb.addAction(self.about)
if(self.pointer is None):
self.previous.setEnabled(False)
self.next.setEnabled(False)
wdg = QWidget(self)
wdg.setMinimumSize(460, 460)
glayout = QGridLayout()
self.setMinimumSize(600, 600)
self.le = ClickableLineEdit(wdg)
self.le.setStyleSheet('color:maroon;font-size: 14px;')
self.le.setObjectName("searchword")
self.le.setText("Enter word to Search...!")
self.le.clicked.connect(self.le.clear)
self.wordlist = []
self.loadwords()
completer = QCompleter(self.wordlist, self.le)
completer.setCompletionMode(completer.PopupCompletion)
self.le.setCompleter(completer)
self.pb = QPushButton(wdg)
self.pb.setObjectName("go")
self.pb.setText("தேடு")
self.pb.setStyleSheet('color: #0077CC; font-weight:bold; font-size: 14px;')
self.te = QTextEdit(wdg)
self.te.setLineWrapMode(QTextEdit.NoWrap)
self.sbar = QStatusBar()
self.sbar.setStyleSheet('color:maroon;')
self.sbar.showMessage('Ready...!')
glayout.addWidget(QLabel("Find ", self), 1, 1)
glayout.addWidget(self.le, 1, 2)
glayout.addWidget(self.pb, 1, 3)
glayout.addWidget(self.te, 2, 1, 1, 3)
glayout.addWidget(self.sbar, 3, 1, 1, 3)
wdg.setLayout(glayout)
self.setWindowTitle("Power Tamil Dictionary")
app_icon = QtGui.QIcon()
app_icon.addFile('./imgs/PT-Icon.png', QtCore.QSize(16, 16))
self.setWindowIcon(app_icon)
self.connect(self.pb, SIGNAL("clicked()"), self.button_click)
self.le.returnPressed.connect(self.pb.click)
self.hotkey = {}
self.hotkey['my_key'] = QShortcut(QtGui.QKeySequence("Ctrl+E"), self)
self.hotkey['my_key'].activated.connect(self.animate_click)
self.te.selectionChanged.connect(self.handleSelectionChanged)
self.setCentralWidget(wdg)
@Slot()
def button_click(self):
# sword is a QString object
dbh = DBHelper()
sword = self.le.text().strip()
print(sword)
if(sword.strip() == ""):
self.sbar.showMessage("Please type a word to Search!")
elif(dbh.checkwordpresent(sword) == 0):
self.sbar.showMessage("Cannot find :: " + sword)
else:
self.addtohistory(sword)
if(self.primed == 0):
self.primed = 1
self.enablePN()
meanings = dbh.getwordMeaning(sword)
hhelpr = HTMLHelper()
HTML_word = hhelpr.getHTMLword(sword)
hhelpr.setmeaningslist(meanings)
HTML_meanings = hhelpr.getHTMLmeanings(hhelpr.meaninglist)
print("bcPointer :: " + str(self.pointer))
self.te.setText(HTML_word + HTML_meanings)
self.sbar.showMessage("Found :: "+ sword)
@Slot()
def animate_click(self, selword):
print("CALLED.....!!!!!!!!!")
dbh = DBHelper()
sword = selword
print(sword)
presentflag = dbh.checkwordpresent(sword)
if(presentflag==0):
self.sbar.showMessage("Cannot find :: " + sword)
else:
self.addtohistory(sword)
if(self.primed == 0):
self.primed = 1
self.pointer = 0
self.enablePN()
meanings = dbh.getwordMeaning(sword)
hhelpr = HTMLHelper()
HTML_word = hhelpr.getHTMLword(sword)
hhelpr.setmeaningslist(meanings)
HTML_meanings = hhelpr.getHTMLmeanings(hhelpr.meaninglist)
self.le.setText(sword)
self.te.setText(HTML_word + HTML_meanings)
self.sbar.showMessage("Found :: "+ sword)
def histnavigate(self, selword):
dbh = DBHelper()
sword = selword.title()
wmdict = dbh.getwordMeaning(sword)
hhelpr = HTMLHelper()
HTML_T = ""
meanings = dbh.getwordMeaning(sword)
hhelpr = HTMLHelper()
HTML_word = hhelpr.getHTMLword(sword)
hhelpr.setmeaningslist(meanings)
HTML_meanings = hhelpr.getHTMLmeanings(hhelpr.meaninglist)
HTML_T = HTML_word + HTML_meanings
self.te.setText(HTML_T)
self.sbar.showMessage("Found :: " + sword)
def addtohistory(self, hword):
if(self.pointer is None):
if (not hword in self.histlist):
self.histlist.append(hword)
self.pointer = 0
elif(len(self.histlist) == self.HIST_SIZE):
self.histlist.pop(0)
if (not hword in self.histlist):
self.histlist.append(hword)
self.pointer = self.HIST_SIZE - 1
else:
if (not hword in self.histlist):
self.histlist.append(hword)
self.pointer = self.pointer + 1
self.enablePN()
print(self.histlist)
def toolbtnpressed(self, action):
if(action.text()=="Previous"):
print("PREV :: P::" +str(self.pointer) + " LS::" + str(len(self.histlist)))
# edge case P0 L1
if (self.pointer == 0 & len(self.histlist) ==1):
self.pointer = self.pointer - 1
self.le.setText(self.histlist[self.pointer])
self.histnavigate(self.histlist[self.pointer])
#print("previousR:: " + str(self.pointer))
elif(self.pointer == 0):
self.pointer = self.pointer
self.le.setText(self.histlist[self.pointer])
self.histnavigate(self.histlist[self.pointer])
#print("previousL :: " + str(self.pointer))
#print(self.histlist[self.pointer])
# majority case
elif(self.pointer <= len(self.histlist) - 1):
self.pointer = self.pointer - 1
self.le.setText(self.histlist[self.pointer])
self.histnavigate(self.histlist[self.pointer])
#print("previous:: " + str(self.pointer))
elif(action.text()=="Next"):
print("NEXT :: P::" + str(self.pointer) + " LS::" + str(len(self.histlist)))
# edge case
if (self.pointer == len(self.histlist) - 1):
self.pointer = self.pointer
self.le.setText(self.histlist[self.pointer])
self.histnavigate(self.histlist[self.pointer])
#print("nextR:: " + str(self.pointer))
# edge case
elif (self.pointer == 0):
self.pointer = self.pointer + 1
self.le.setText(self.histlist[self.pointer])
self.histnavigate(self.histlist[self.pointer])
#print("nextL:: " + str(self.pointer))
# majority case
elif (self.pointer < len(self.histlist) - 1):
self.pointer = self.pointer + 1
self.le.setText(self.histlist[self.pointer])
self.histnavigate(self.histlist[self.pointer])
#print("next:: " + str(self.pointer))
self.enablePN()
def enablePN(self):
# pointer starts from 0 while len counts from 1
if (self.pointer is None):
pass
elif(self.pointer == len(self.histlist)-1):
self.previous.setEnabled(True)
self.next.setEnabled(False)
elif(self.pointer == 0):
self.previous.setEnabled(False)
self.next.setEnabled(True)
elif(self.pointer < len(self.histlist)-1):
self.previous.setEnabled(True)
self.next.setEnabled(True)
def handleSelectionChanged(self):
cursor = self.te.textCursor()
textSelected = cursor.selectedText()
self.le.setText(textSelected)
if(textSelected != ""):
self.animate_click(textSelected)
def _show_about(self):
QMessageBox.about(self, 'About', '<p><b>PowerTamil Dictionary V1.0</b></p>'
'<p>Copyright © 2020 Rajkumar Palani</p>'
'<p>Power Tamil dictionary is free to use and distribute. </p>'
'<p><a href="http://www.rajkumarpalani.com/software">www.rajkumarpalani.com/software</a></p>')
def loadwords(self):
with open("AllTamilWords.txt", "r", encoding="utf8") as f:
self.wordlist = f.read().split()
def main():
app = QApplication(sys.argv)
ex = myWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()