【原创】shadowebdict开发日记:基于linux的简明英汉字典(四)

全系列目录:

实习的工作都这么忙,真是的。。

好不容易有时间写点博客,一鼓作气完成算了

 

承接上文

本文完成对本地数据库模块的开发。

 

由于只是非常轻量级的应用,就不劳mysql大驾了,来个sqlite就可以了,本地db文件也好读取。

 

这里我们可以多实现一个功能:难词系统。

定义一个难词为查询多次达到某个阀值的词,并记录本地数据库中所有词汇的查询次数。

当有词语达到这个标准时,将该词添加到本地数据库中的难词表,并提供一个方法供其他模块查询难词表。

 

可以看出,难词系统决定了本地数据模块这个部分和response模块不同,需要提供多个模块供上层操作,包括:

0、提供查询难词表的接口。

1、提供更新每个词汇查询次数的接口。

2、提供插入词汇的接口。

3、和response模块一样,给定一个词汇,查询其含义的接口。

 

具体代码如下:

# -*- coding:utf-8 -*-
__author__ = 'wmydx'

import sqlite3

class LocalDict:
    def __init__(self):
        self.con = None
        self.limit = 4
        self.setup_connect()

    def setup_connect(self):
        self.con = sqlite3.connect('./word.db')
        create_table = '''CREATE TABLE IF NOT EXISTS words
        (
            word text,
            explain text,
            net_explain text,
            sentence text,
            times int
        );
        '''
        self.con.execute(create_table)
        create_table = '''CREATE TABLE IF NOT EXISTS hard
        (
            word text,
            explain text,
            net_explain text,
            sentence text,
            times int
        );
        '''
        self.con.execute(create_table)
        self.con.text_factory = str

    def is_a_hard_word(self, diction):
        print diction['times'] == self.limit
        return diction['times'] == self.limit   # prevent mutiple insert, so use == instead of >=

    def update_word_times(self, diction):
        curs = self.con.cursor()
        update_sql = '''
            UPDATE words SET times=? WHERE word=?
        '''
        curs.execute(update_sql,(diction['times'], diction['word']))

    def get_hard_word(self):
        curs = self.con.cursor()
        select_sql = '''
            SELECT word FROM hard;
        '''
        curs.execute(select_sql)
        names = [d[0] for d in curs.description]
        rows = [dict(zip(names, row)) for row in curs.fetchall()]
        return rows

    def get_eng_word_from_db(self, word):
        curs = self.con.cursor()
        select_sql = '''
            SELECT * FROM words WHERE word=\'%s\';
        ''' % word
        curs.execute(select_sql)
        names = [d[0] for d in curs.description]
        rows = [dict(zip(names, row)) for row in curs.fetchall()]
        return rows

    def process_dict(self, diction):
        for key in diction.keys():
            if diction[key] == -1:
                diction[key] = ''
        return diction

    # before pass diction to this method, u need to add word and times in diction
    def insert_word_to_db(self, diction, table):
        diction = self.process_dict(diction)
        insert_sql = '''
            INSERT INTO %s (word,explain,net_explain,sentence,times) VALUES
            (?,?,?,?,?);
        ''' % table
        self.con.execute(insert_sql, (diction['word'], diction['explain'],
                                      diction['net_explain'], diction['sentence'], diction['times']))

    def turn_off_db(self):
        self.con.commit()
        self.con.close()


if __name__ == '__main__':
    db = LocalDict()
    db.setup_connect()

 

到此为止,我们开发了一个完整的webdict,平常看英文原著的时候,快捷命令一打就随手查吧

免得开浏览器然后。。。=_+  

 

posted on 2015-03-13 19:57  shadowmydx'sLab  阅读(363)  评论(0编辑  收藏  举报

导航