From 515f49afbcdafc1f81c681a9f06bbed101dba55d Mon Sep 17 00:00:00 2001 From: Atikur Rahman Chitholian Date: Tue, 3 Aug 2021 22:22:27 +0600 Subject: [PATCH] Show more English words against some Bangla words. --- eng_beng/main_window.py | 144 +++++++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 55 deletions(-) diff --git a/eng_beng/main_window.py b/eng_beng/main_window.py index b069ab5..8dfe1f3 100644 --- a/eng_beng/main_window.py +++ b/eng_beng/main_window.py @@ -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() @@ -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 @@ -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( @@ -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) @@ -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()) @@ -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( - '

Nothing Found!

') + found = query.first() + if not found and is_eng: + self.show_not_found() return else: - htm = '' - htm += f"

{html.escape(query.value(1))}

{html.escape(query.value(3))}
" - - ant = query.value(4) - if ant: - words = ', '.join([w.capitalize() - for w in list(json.loads(ant))]) - htm += f'

Antonyms

{html.escape(words)}
' - - data = query.value(5) - if data: - parts = list(json.loads(data)) - lines = '' - for p in parts: - lines += f"
  • {html.escape(p.capitalize())}
  • " - if lines: - htm += f'

    Definitions


    ' - - 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'

    {html.escape(more_en)}

    ' + else: + more = f'
    {html.escape(more_en)}' if more_en else '' + htm = '' + htm += f"

    {html.escape(query.value(1))}

    {html.escape(query.value(3))}{more}
    " + + ant = query.value(4) + if ant: + words = ', '.join([w.capitalize() for w in list(json.loads(ant))]) + htm += f'

    Antonyms

    {html.escape(words)}
    ' + + 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'
    {html.escape(bn)}
    {html.escape(words)}
    ' - htm += f'

    {html.escape(type_name)}

    {lines}
    ' - example = query.value(6) - if example: - htm += f'

    Example

    {html.escape(example.capitalize())}' - htm += '' + for p in parts: + lines += f"
  • {html.escape(p.capitalize())}
  • " + if lines: + htm += f'

    Definitions


    ' + + 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'
    {html.escape(bn)}
    {html.escape(words)}
    ' + htm += f'

    {html.escape(type_name)}

    {lines}
    ' + example = query.value(6) + if example: + htm += f'

    Example

    {html.escape(example.capitalize())}' + htm += '' self.outputBox.setHtml(htm) + def show_not_found(self): + self.outputBox.setHtml('

    Nothing Found!

    ') + def speak_now(self, text): # TODO: Implement this pass @@ -180,7 +210,9 @@ def show_about(self):

    Bangla Dictionary

    v1.0.0
    - https://github.com/chitholian/Bangla-Dictionary + + https://github.com/chitholian/Bangla-Dictionary +

    This application helps translating words between Bengali and English languages. @@ -194,19 +226,21 @@ def show_about(self):

  • Phonetic of Bengali words
  • - ==> 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.

    - ==> 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.)

    - ==> 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).

    - ==> 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.