From 4329d6c047aabf1ec417e709ec2fb776a9b099d0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 9 Nov 2013 15:42:02 -0800 Subject: [PATCH] Switching dictionary to return values in python dictionaries instead of tuples Former-commit-id: 3f0682e88a24faed24f9e12e7c9cee338e817672 --- yomi_base/japanese/__init__.py | 15 +++++---- yomi_base/japanese/deinflect.py | 2 +- yomi_base/japanese/dictionary.py | 20 +++++++++-- yomi_base/japanese/translate.py | 25 +++++++------- yomi_base/reader.py | 14 ++++---- yomi_base/reader_util.py | 58 +++++++++++++++----------------- 6 files changed, 74 insertions(+), 60 deletions(-) diff --git a/yomi_base/japanese/__init__.py b/yomi_base/japanese/__init__.py index 8ccf44c..cd8a6b4 100644 --- a/yomi_base/japanese/__init__.py +++ b/yomi_base/japanese/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2011 Alex Yatskov +# Copyright (C) 2013 Alex Yatskov # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,10 +16,10 @@ # along with this program. If not, see . +import deinflect +import dictionary import os.path -from dictionary import Dictionary -from deinflect import Deinflector -from translate import Translator +import translate def buildRelPath(path): @@ -28,6 +28,7 @@ def buildRelPath(path): def initLanguage(): - deinflector = Deinflector(buildRelPath('data/deinflect.json')) - dictionary = Dictionary(buildRelPath('data/dictionary.db')) - return Translator(deinflector, dictionary) + return translate.Translator( + deinflect.Deinflector(buildRelPath('data/deinflect.json')), + dictionary.Dictionary(buildRelPath('data/dictionary.db')) + ) diff --git a/yomi_base/japanese/deinflect.py b/yomi_base/japanese/deinflect.py index d139a3a..4fa6743 100644 --- a/yomi_base/japanese/deinflect.py +++ b/yomi_base/japanese/deinflect.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2011 Alex Yatskov +# Copyright (C) 2013 Alex Yatskov # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by diff --git a/yomi_base/japanese/dictionary.py b/yomi_base/japanese/dictionary.py index 1ae1766..b7985f7 100644 --- a/yomi_base/japanese/dictionary.py +++ b/yomi_base/japanese/dictionary.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2011 Alex Yatskov +# Copyright (C) 2013 Alex Yatskov # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -35,7 +35,12 @@ class Dictionary: results = list() for expression, reading, definitions, tags in cursor.fetchall(): - results.append((expression, reading, definitions, tags.split())) + results.append({ + 'expression': expression, + 'reading': reading, + 'definitions': definitions, + 'tags': tags.split() + }) return results @@ -43,7 +48,16 @@ class Dictionary: def findCharacter(self, character): cursor = self.db.cursor() cursor.execute('SELECT * FROM Kanji WHERE character=? LIMIT 1', character) - return cursor.fetchone() + + query = cursor.fetchone() + if query is not None: + character, kunyomi, onyomi, meanings = result + return { + 'character': character, + 'kunyomi': kunyomi, + 'onyomi': onyomi, + 'meanings': meanings + } def findCharacterVisually(self, characters): diff --git a/yomi_base/japanese/translate.py b/yomi_base/japanese/translate.py index 57d1e70..94ca936 100644 --- a/yomi_base/japanese/translate.py +++ b/yomi_base/japanese/translate.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2011 Alex Yatskov +# Copyright (C) 2013 Alex Yatskov # This module is based on Rikaichan code written by Jonathan Zarate # # This program is free software: you can redistribute it and/or modify @@ -41,11 +41,11 @@ class Translator: results = map(self.formatResult, groups.items()) results = filter(operator.truth, results) - results = sorted(results, key=lambda x: len(x[0]), reverse=True) + results = sorted(results, key=lambda x: len(x['source']), reverse=True) length = 0 - for expression, reading, definition, rules, source in results: - length = max(length, len(source)) + for result in results: + length = max(length, len(result['source'])) return results, length @@ -54,20 +54,21 @@ class Translator: root = root or source for entry in self.dictionary.findTerm(root, partial): - expression, reading, definition, tags = entry - key = expression, reading, definition + key = entry['expression'], entry['reading'], entry['definitions'] if key not in groups: groups[key] = entry, source, rules def formatResult(self, group): (expression, reading, definition), (entry, source, rules) = group - return expression, reading, definition, rules, source + return { + 'expression': expression, + 'reading': reading, + 'definitions': definition, + 'rules': rules, + 'source': source + } def validator(self, term): - results = list() - for expression, reading, definitions, tags in self.dictionary.findTerm(term): - results.append(tags) - - return results + return [d['tags'] for d in self.dictionary.findTerm(term)] diff --git a/yomi_base/reader.py b/yomi_base/reader.py index 12eaccb..95745ab 100644 --- a/yomi_base/reader.py +++ b/yomi_base/reader.py @@ -250,17 +250,17 @@ class MainWindowReader(QtGui.QMainWindow, reader_ui.Ui_MainWindowReader): if command == 'addExpression': markup = reader_util.buildFactMarkupExpression( - definition.expression, - definition.reading, - definition.glossary, - definition.sentence + definition['expression'], + definition['reading'], + definition['definitions'], + definition['sentence'] ) self.ankiAddFact(markup) if command == 'addReading': markup = reader_util.buildFactMarkupReading( - definition.reading, - definition.glossary, - definition.sentence + definition['reading'], + definition['definitions'], + definition['sentence'] ) self.ankiAddFact(markup) elif command == 'copyDefinition': diff --git a/yomi_base/reader_util.py b/yomi_base/reader_util.py index 8f9a096..191953a 100644 --- a/yomi_base/reader_util.py +++ b/yomi_base/reader_util.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2011 Alex Yatskov +# Copyright (C) 2013 Alex Yatskov # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,18 +16,8 @@ # along with this program. If not, see . -import re from PyQt4 import QtGui - - -class Definition: - def __init__(self, expression, reading, glossary, conjugations, source, sentence): - self.expression = expression - self.reading = reading - self.glossary = glossary - self.conjugations = conjugations - self.source = source - self.sentence = sentence +import re def decodeContent(content): @@ -105,15 +95,21 @@ def replaceMarkupInFields(fields, markup): return result -def buildFactMarkupExpression(expression, reading, glossary, sentence=None): +def buildFactMarkupExpression(expression, reading, definitions, sentence=None): return { - '%e': expression, '%r': reading, '%g': glossary, '%s': sentence + '%e': expression, + '%r': reading, + '%g': definitions, + '%s': sentence } -def buildFactMarkupReading(reading, glossary, sentence=None): +def buildFactMarkupReading(reading, definitions, sentence=None): return { - '%e': reading, '%r': None, '%g': glossary, '%s': sentence + '%e': reading, + '%r': None, + '%g': definitions, + '%s': sentence } @@ -122,38 +118,40 @@ def splitTags(tags): def convertDefinitions(definitions, sentence=None): - return [ - Definition(*(definition + (sentence,))) for definition in definitions - ] + if sentence is not None: + for definition in definitions: + definition['sentence'] = sentence + return definitions + def copyDefinitions(definitions): text = unicode() for definition in definitions: - if definition.reading: - text += u'{0}\t{1}\t{2}\n'.format(definition.expression, definition.reading, definition.glossary) + if definition['reading']: + text += u'{expression}\t{reading}\t{definitions}\n'.format(**definition) else: - text += u'{0}\t{1}\n'.format(definition.expression, definition.glossary) + text += u'{expression}\t{meanings}\n'.format(**definition) QtGui.QApplication.clipboard().setText(text) def buildDefinitionHtml(definition, factIndex, factQuery): reading = unicode() - if definition.reading: - reading = u'[{0}]'.format(definition.reading) + if definition['reading']: + reading = u'[{0}]'.format(definition['reading']) conjugations = unicode() - if len(definition.conjugations) > 0: - conjugations = u' :: '.join(definition.conjugations) + if len(definition['rules']) > 0: + conjugations = u' :: '.join(definition['rules']) conjugations = '<{0}>
'.format(conjugations) links = ''.format(factIndex) if factQuery: - if factQuery(buildFactMarkupExpression(definition.expression, definition.reading, definition.glossary)): + if factQuery(buildFactMarkupExpression(definition['expression'], definition['reading'], definition['definitions'])): links += ''.format(factIndex) - if factQuery(buildFactMarkupReading(definition.reading, definition.glossary)): + if factQuery(buildFactMarkupReading(definition['reading'], definition['definitions'])): links += ''.format(factIndex) html = u""" @@ -161,7 +159,7 @@ def buildDefinitionHtml(definition, factIndex, factQuery): {1} {2}
{3}
{4} -
""".format(links, definition.expression, reading, definition.glossary, conjugations) +
""".format(links, definition['expression'], reading, definition['definitions'], conjugations) return html @@ -177,7 +175,7 @@ def buildDefinitionsHtml(definitions, factQuery): span.expression {{ font-size: 15pt; }} """.format(toolTipBg, toolTipFg) - if definitions: + if len(definitions) > 0: for i, definition in enumerate(definitions): html += buildDefinitionHtml(definition, i, factQuery) else: