Skip to content

Commit

Permalink
add option to enable lemmatization
Browse files Browse the repository at this point in the history
  • Loading branch information
xxyzz committed Dec 8, 2020
1 parent 397dba1 commit 59b2d90
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def config_widget(self):
return ConfigWidget()

def save_settings(self, config_widget):
pass
config_widget.save_settings()
13 changes: 11 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python3
from calibre.utils.config import JSONConfig
from PyQt5.Qt import QWidget, QPushButton, QVBoxLayout
import webbrowser

from calibre.utils.config import JSONConfig
from PyQt5.Qt import QPushButton, QRadioButton, QVBoxLayout, QWidget

prefs = JSONConfig('plugins/worddumb')
prefs.defaults['lemmatize'] = True


class ConfigWidget(QWidget):
Expand All @@ -13,6 +15,10 @@ def __init__(self):
self.vl = QVBoxLayout()
self.setLayout(self.vl)

self.lemmatize_button = QRadioButton('Lemmatize', self)
self.lemmatize_button.setChecked(prefs['lemmatize'])
self.vl.addWidget(self.lemmatize_button)

self.donate_button = QPushButton('Donate', self)
self.donate_button.clicked.connect(self.donate)
self.vl.addWidget(self.donate_button)
Expand All @@ -26,3 +32,6 @@ def donate(self):

def github(self):
webbrowser.open('https://github.com/xxyzz/WordDumb')

def save_settings(self):
prefs['lemmatize'] = self.lemmatize_button.isChecked()
9 changes: 8 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sqlite3
from pathlib import Path

from calibre_plugins.worddumb.config import prefs


def connect_ww_database():
ww_conn = sqlite3.connect(":memory:")
Expand Down Expand Up @@ -59,8 +61,13 @@ def create_lang_layer(asin, book_path):


def match_lemma(start, word, ll_conn, ww_conn):
word = word.lower()
if prefs['lemmatize']:
from nltk.corpus import wordnet as wn
word = wn.morphy(word)

for result in ww_conn.execute("SELECT * FROM words WHERE lemma = ?",
(word.lower(), )):
(word, )):
(_, sense_id, difficulty) = result
ll_conn.execute('''
INSERT INTO glosses (start, difficulty, sense_id, low_confidence)
Expand Down
11 changes: 5 additions & 6 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ def run(self):

def install_libs(self):
extarct_path = Path(config_dir).joinpath('plugins/worddumb')
if extarct_path.is_dir():
return
with ZipFile(self.plugin_path, 'r') as zf:
for f in zf.namelist():
if '.venv' in f:
zf.extract(f, path=extarct_path)
if not extarct_path.is_dir():
with ZipFile(self.plugin_path, 'r') as zf:
for f in zf.namelist():
if '.venv' in f:
zf.extract(f, path=extarct_path)
for dir in extarct_path.joinpath('.venv/lib').iterdir():
sys.path.append(str(dir.joinpath('site-packages')))
import nltk
Expand Down

0 comments on commit 59b2d90

Please sign in to comment.