From af417923d869bf1b6114fb86b8e2538e2ce775a9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 8 Nov 2013 12:46:15 -0800 Subject: [PATCH] Dictionary can now search for terms and characters Former-commit-id: 5ec68e00caab86e9a2f84aa5baf86e467b6c34a6 --- yomi_base/japanese2/dictionary.py | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 yomi_base/japanese2/dictionary.py diff --git a/yomi_base/japanese2/dictionary.py b/yomi_base/japanese2/dictionary.py new file mode 100644 index 0000000..cf3c22c --- /dev/null +++ b/yomi_base/japanese2/dictionary.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2011 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import re +import sqlite3 + + +class Dictionary: + def __init__(self, filename, index=True): + self.db = sqlite3.connect(filename) + self.indices = set() + + + def findTerm(self, word): + cursor = self.db.cursor() + + if not self.hasIndex('TermIndex'): + cursor.execute('CREATE INDEX TermIndex ON Terms(expression, reading)') + self.db.commit() + + cursor.execute('SELECT * FROM Terms WHERE expression=? OR reading=?', (word, word)) + return cursor.fetchall() + + + def findCharacter(self, character): + cursor = self.db.cursor() + + if not self.hasIndex('KanjiIndex'): + cursor.execute('CREATE INDEX KanjiIndex ON Kanji(character)') + self.db.commit() + + cursor.execute('SELECT * FROM Kanji WHERE character=?', character) + return cursor.fetchall() + + + def hasIndex(self, name): + if name in self.indices: + return True + + cursor = self.db.cursor() + cursor.execute('SELECT * FROM sqlite_master WHERE name=?', (name, )) + if len(cursor.fetchall()) == 0: + return False + + self.indices.update([name]) + return True