2013-11-08 20:46:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-11-09 23:42:02 +00:00
|
|
|
# Copyright (C) 2013 Alex Yatskov
|
2013-11-08 20:46:15 +00:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-05-09 19:21:43 +00:00
|
|
|
import operator
|
2013-11-08 20:46:15 +00:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
class Dictionary:
|
|
|
|
def __init__(self, filename, index=True):
|
|
|
|
self.db = sqlite3.connect(filename)
|
|
|
|
self.indices = set()
|
|
|
|
|
|
|
|
|
2016-05-09 21:33:52 +00:00
|
|
|
def findTerm(self, text, wildcards=False):
|
2016-05-09 19:21:43 +00:00
|
|
|
self.requireIndex('Vocab', 'expression')
|
|
|
|
self.requireIndex('Vocab', 'reading')
|
|
|
|
self.requireIndex('VocabGloss', 'vocabId')
|
2013-11-08 20:46:15 +00:00
|
|
|
|
2013-11-08 22:29:54 +00:00
|
|
|
cursor = self.db.cursor()
|
2013-11-08 22:36:21 +00:00
|
|
|
|
2016-05-09 19:21:43 +00:00
|
|
|
definitions = []
|
2016-05-09 21:33:52 +00:00
|
|
|
cursor.execute('SELECT * FROM Vocab WHERE expression {0} ? OR reading=?'.format('LIKE' if wildcards else '='), (text, text))
|
|
|
|
for vocabId, expression, reading, tags in cursor.fetchall():
|
|
|
|
tags = tags.split()
|
|
|
|
|
2016-05-09 19:21:43 +00:00
|
|
|
cursor.execute('SELECT glossary From VocabGloss WHERE vocabId=?', (vocabId,))
|
|
|
|
glossary = map(operator.itemgetter(0), cursor)
|
|
|
|
|
2016-05-09 21:33:52 +00:00
|
|
|
#
|
|
|
|
# TODO: Handle addons through data.
|
|
|
|
#
|
|
|
|
|
|
|
|
addons = []
|
|
|
|
for tag in tags:
|
|
|
|
if tag.startswith('v5') and tag != 'v5':
|
|
|
|
addons.append('v5')
|
|
|
|
elif tag.startswith('vs-'):
|
|
|
|
addons.append('vs')
|
|
|
|
|
2016-05-09 19:21:43 +00:00
|
|
|
definitions.append({
|
2016-05-09 21:33:52 +00:00
|
|
|
'id': vocabId,
|
2013-11-09 23:42:02 +00:00
|
|
|
'expression': expression,
|
2016-05-08 03:24:59 +00:00
|
|
|
'reading': reading,
|
2016-05-18 05:28:56 +00:00
|
|
|
'glossary': glossary,
|
2016-05-09 21:33:52 +00:00
|
|
|
'tags': tags + addons,
|
|
|
|
'addons': addons
|
2013-11-09 23:42:02 +00:00
|
|
|
})
|
2013-11-08 22:36:21 +00:00
|
|
|
|
2016-05-09 19:21:43 +00:00
|
|
|
return definitions
|
2013-11-08 20:46:15 +00:00
|
|
|
|
|
|
|
|
2016-05-09 21:33:52 +00:00
|
|
|
def findKanji(self, text):
|
|
|
|
assert len(text) == 1
|
2016-05-09 19:21:43 +00:00
|
|
|
|
2013-11-11 04:27:25 +00:00
|
|
|
self.requireIndex('Kanji', 'character')
|
2016-05-09 19:21:43 +00:00
|
|
|
self.requireIndex('KanjiGloss', 'kanjiId')
|
2013-11-11 04:27:25 +00:00
|
|
|
|
2013-11-08 20:46:15 +00:00
|
|
|
cursor = self.db.cursor()
|
2013-11-09 23:42:02 +00:00
|
|
|
|
2016-05-09 21:33:52 +00:00
|
|
|
cursor.execute('SELECT * FROM Kanji WHERE character=? LIMIT 1', text)
|
2013-11-09 23:42:02 +00:00
|
|
|
query = cursor.fetchone()
|
2016-05-09 19:21:43 +00:00
|
|
|
if query is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
kanjiId, character, kunyomi, onyomi = query
|
|
|
|
cursor.execute('SELECT glossary From KanjiGloss WHERE kanjiId=?', (kanjiId,))
|
|
|
|
glossary = map(operator.itemgetter(0), cursor)
|
|
|
|
|
|
|
|
return {
|
2016-05-09 21:33:52 +00:00
|
|
|
'id': kanjiId,
|
2016-05-09 19:21:43 +00:00
|
|
|
'character': character,
|
2016-05-21 17:22:03 +00:00
|
|
|
'kunyomi': [] if kunyomi is None else kunyomi.split(),
|
|
|
|
'onyomi': [] if onyomi is None else onyomi.split(),
|
2016-05-18 05:28:56 +00:00
|
|
|
'glossary': glossary
|
2016-05-09 19:21:43 +00:00
|
|
|
}
|
2013-11-08 20:46:15 +00:00
|
|
|
|
|
|
|
|
2013-11-08 22:29:54 +00:00
|
|
|
def requireIndex(self, table, column):
|
|
|
|
name = 'index_{0}_{1}'.format(table, column)
|
|
|
|
if not self.hasIndex(name):
|
|
|
|
self.buildIndex(name, table, column)
|
|
|
|
|
|
|
|
|
|
|
|
def buildIndex(self, name, table, column):
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute('CREATE INDEX {0} ON {1}({2})'.format(name, table, column))
|
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
|
2013-11-08 20:46:15 +00:00
|
|
|
def hasIndex(self, name):
|
|
|
|
if name in self.indices:
|
|
|
|
return True
|
|
|
|
|
|
|
|
cursor = self.db.cursor()
|
2013-11-08 21:46:30 +00:00
|
|
|
cursor.execute('SELECT * FROM sqlite_master WHERE name=?', (name,))
|
2013-11-08 20:46:15 +00:00
|
|
|
if len(cursor.fetchall()) == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.indices.update([name])
|
|
|
|
return True
|