Skip to content

Commit

Permalink
Show more English words against some Bangla words.
Browse files Browse the repository at this point in the history
  • Loading branch information
chitholian committed Aug 3, 2021
1 parent 095e70d commit 515f49a
Showing 1 changed file with 89 additions and 55 deletions.
144 changes: 89 additions & 55 deletions eng_beng/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(self):

self.types = extract_types(self.db)

self.en_completer = self.make_completer('english')
self.bn_completer = self.make_completer('bn_en')
self.en_completer = self.make_completer('en')
self.bn_completer = self.make_completer('bn')
self.current_completer = None

self.set_up_slots()
Expand All @@ -33,13 +33,17 @@ def __init__(self):

self.init()

def make_completer(self, table, index=1):
model = QSqlTableModel(self, self.db)
model.setTable(table)
model.select()
def make_completer(self, lang, index=0):
model = QSqlQueryModel(self)
if lang == 'en':
query = QSqlQuery("SELECT DISTINCT word FROM english", self.db)
else:
query = QSqlQuery("SELECT word FROM bangla UNION SELECT bangla as word FROM bn_en GROUP BY word", self.db)
model.setQuery(query)
completer = QCompleter(model, self)
completer.setCaseSensitivity(Qt.CaseInsensitive)
completer.setCompletionColumn(index)
completer.setMaxVisibleItems(10)
completer.activated.connect(self.on_completer_done)
return completer

Expand All @@ -51,7 +55,7 @@ def init(self):
def set_up_slots(self):
self.inputLine.textChanged.connect(self.on_input_changed)
self.inputLine.returnPressed.connect(self.on_input_submitted)
self.clearBtn.clicked.connect(lambda: self.inputLine.clear())
self.clearBtn.clicked.connect(self.on_clear_input_clicked)
self.monitorClipCheck.stateChanged.connect(
self.on_clip_monitor_check_changed)
self.autoSpeakCheck.stateChanged.connect(
Expand All @@ -62,6 +66,10 @@ def set_up_slots(self):
self.exitAction.triggered.connect(QApplication.quit)
self.aboutAction.triggered.connect(self.show_about)

def on_clear_input_clicked(self):
self.inputLine.clear()
self.inputLine.setFocus()

def on_completer_done(self, value: str):
self.translate_now(value)

Expand Down Expand Up @@ -115,6 +123,8 @@ def use_clipboard_data(self):
self.translate_now(text)

def translate_now(self, text):
if not text:
return
self.inputLine.setText(text)
self.speak_now(text)
is_eng = len(text) == len(text.encode())
Expand All @@ -126,51 +136,71 @@ def translate_now(self, text):
" phonetic, antonyms, definitions, examples FROM bangla b WHERE b.word LIKE ?", self.db)
query.bindValue(0, text)
query.exec_()
if not query.first():
self.outputBox.setHtml(
'<h4 style="color: red;text-align:center">Nothing Found!</h4>')
found = query.first()
if not found and is_eng:
self.show_not_found()
return
else:
htm = '<html><head><style>*{margin: 0}html{font-size:14px}</style></head><body>'
htm += f"<h3>{html.escape(query.value(1))}</h3>{html.escape(query.value(3))}<hr/>"

ant = query.value(4)
if ant:
words = ', '.join([w.capitalize()
for w in list(json.loads(ant))])
htm += f'<h4 style="text-align:center">Antonyms</h4>{html.escape(words)}<hr/>'

data = query.value(5)
if data:
parts = list(json.loads(data))
lines = ''
for p in parts:
lines += f"<li>{html.escape(p.capitalize())}</li>"
if lines:
htm += f'<h4 style="text-align:center">Definitions</h4><ul>{lines}</ul><hr/>'

types = query.value(2)
if types:
lists = list(json.loads(types))
for i in lists:
type_id = i[0]
type_name = self.types[type_id]
q = QSqlQuery(f"SELECT * FROM bn_en WHERE serial IN ({','.join([str(j) for j in i[1:]])})",
self.db)
q.exec_()
# Try query from bn_en table.
more_en = ''
if not is_eng:
extra = QSqlQuery("SELECT english FROM bn_en WHERE bangla LIKE ?", self.db)
extra.bindValue(0, text)
extra.exec_()
if extra.first():
words = extra.value(0)
if words:
words = list(json.loads(words))
more_en = ', '.join([w.capitalize() for w in words])
if not found and not more_en:
self.show_not_found()
return
elif not found:
htm = f'<h2>{html.escape(more_en)}</h2>'
else:
more = f'<hr/>{html.escape(more_en)}' if more_en else ''
htm = '<html><head><style>*{margin: 0}html{font-size:14px}</style></head><body>'
htm += f"<h3>{html.escape(query.value(1))}</h3>{html.escape(query.value(3))}{more}<hr/>"

ant = query.value(4)
if ant:
words = ', '.join([w.capitalize() for w in list(json.loads(ant))])
htm += f'<h4 style="text-align:center">Antonyms</h4>{html.escape(words)}<hr/>'

data = query.value(5)
if data:
parts = list(json.loads(data))
lines = ''
while q.next():
bn = q.value(1)
parts = list(json.loads(q.value(2)))
words = ', '.join([w.capitalize() for w in parts])
lines += f'<br/><em>{html.escape(bn)}</em><br>{html.escape(words)}<br/>'
htm += f'<h4 style="text-align:center">{html.escape(type_name)}</h4>{lines}<hr/>'
example = query.value(6)
if example:
htm += f'<h4 style="text-align:center">Example</h4>{html.escape(example.capitalize())}'
htm += '</body>'
for p in parts:
lines += f"<li>{html.escape(p.capitalize())}</li>"
if lines:
htm += f'<h4 style="text-align:center">Definitions</h4><ul>{lines}</ul><hr/>'

types = query.value(2)
if types:
lists = list(json.loads(types))
for i in lists:
type_id = i[0]
type_name = self.types[type_id]
q = QSqlQuery(f"SELECT * FROM bn_en WHERE serial IN ({','.join([str(j) for j in i[1:]])})",
self.db)
q.exec_()
lines = ''
while q.next():
bn = q.value(1)
parts = list(json.loads(q.value(2)))
words = ', '.join([w.capitalize() for w in parts])
lines += f'<br/><em>{html.escape(bn)}</em><br>{html.escape(words)}<br/>'
htm += f'<h4 style="text-align:center">{html.escape(type_name)}</h4>{lines}<hr/>'
example = query.value(6)
if example:
htm += f'<h4 style="text-align:center">Example</h4>{html.escape(example.capitalize())}'
htm += '</body>'
self.outputBox.setHtml(htm)

def show_not_found(self):
self.outputBox.setHtml('<h4 style="color: red;text-align:center">Nothing Found!</h4>')

def speak_now(self, text):
# TODO: Implement this
pass
Expand All @@ -180,7 +210,9 @@ def show_about(self):
<div style="text-align: center">
<h2>Bangla Dictionary</h2>
<small>v1.0.0</small><br>
<a href="https://github.com/chitholian/Bangla-Dictionary">https://github.com/chitholian/Bangla-Dictionary</a>
<a href="https://github.com/chitholian/Bangla-Dictionary">
https://github.com/chitholian/Bangla-Dictionary
</a>
</div>
<p style="text-align: justified">
This application helps translating words between <em>Bengali</em> and <em>English</em> languages.
Expand All @@ -194,19 +226,21 @@ def show_about(self):
<li>Phonetic of Bengali words</li>
</ul>
<p>
==> By default this application is configured to stay above other windows; it helps improved experience of
clipboard monitoring feature. Uncheck the "Keep window above" checkbox to turn it off.
==> By default this application is configured to stay above other windows; it helps improved experience
of clipboard monitoring feature. Uncheck the "Keep window above" checkbox to turn it off.
</p>
<p>
==> Just keep this app running and copy some words (Ctrl+C) from anywhere of your device (e.g. from web browser, PDF viewer etc.)
==> Just keep this app running and copy some words (Ctrl+C) from anywhere of your device (e.g. from web
browser, PDF viewer etc.)
</p>
<p>
==> This app will monitor your clipboard and show translation as soon as you copy texts. It does not require you to paste
the selected word inside this app (although you can do so).
==> This app will monitor your clipboard and show translation as soon as you copy texts. It does not
require you to paste the selected word inside this app (although you can do so).
</p>
<p>
==> Linux (X11 Window System) provides access to clipboard selection. In that case you don't even need to copy the text,
just double click the word (or use mouse/touchpad) to select it. This app will capture and show the translation.
==> Linux (X11 Window System) provides access to clipboard selection. In that case you don't even need
to copy the text, just double click the word (or use mouse/touchpad) to select it. This app will
capture and show the translation.
</p>
</p>
<div style="text-align: center">
Expand Down

0 comments on commit 515f49a

Please sign in to comment.