1
yomichan-anki/lang/japanese/dictionary.py

67 lines
1.9 KiB
Python
Raw Normal View History

2011-08-28 18:01:32 +00:00
# -*- coding: utf-8 -*-
2011-10-27 15:22:26 +00:00
2011-08-28 18:01:32 +00:00
# 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 <http://www.gnu.org/licenses/>.
import sqlite3
import re
class Dictionary:
def __init__(self, filename=None):
if filename == None:
self.close()
else:
self.load(filename)
def close(self):
2011-10-08 16:32:51 +00:00
self.connection = None
2011-08-28 18:01:32 +00:00
def load(self, filename):
2011-10-08 16:32:51 +00:00
self.connection = sqlite3.connect(filename)
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def find(self, exp, args):
if self.connection == None:
2011-08-28 18:01:32 +00:00
return list()
entries = list()
2011-10-08 16:32:51 +00:00
for kanji, kana, entry in self.connection.cursor().execute(exp, args):
2011-08-28 18:01:32 +00:00
meanings = entry.split('/')
if not meanings[-1]:
items = map(unicode.strip, re.split('[\[\]]', meanings[0]))
if len(items) > 1:
kanji, kana, = items[0], items[1]
else:
kanji, kana = None, items[0]
del meanings[0], meanings[-1]
entry = u'; '.join(meanings)
expression = kanji or kana
reading = kana if kanji else None
entries.append((expression, reading, entry))
return entries
def findWord(self, word):
2011-10-08 16:32:51 +00:00
return self.find(u'select * from dict where kanji=? or kana=? limit 100', (word,) * 2)