From e84387a29ca3227760cc2025015361a197d010a1 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Fri, 19 Sep 2014 14:13:01 +0100 Subject: Changed wordnet database to sqlite --- bladictionary.py | 40 ++++++++++++++++++++-------------------- convert.py | 27 ++++++++++++++++----------- 2 files changed, 36 insertions(+), 31 deletions(-) mode change 100644 => 100755 convert.py diff --git a/bladictionary.py b/bladictionary.py index 1b04a78..39b4917 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -5,9 +5,9 @@ import sys import shlex import optparse from lxml import etree -import MySQLdb as mysql +import sqlite3 -VERSION = "2.1.2b" +VERSION = "2.1.3b" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -134,7 +134,7 @@ def parse_xml(xml): antonyms.append(ant.strip("{},] ")) ant_line = True - item = Definition(word, id, "wn", word_type, definition, synonyms, antonyms) + item = Definition(word, id, "wn", word_type, definition, [], synonyms, antonyms) items.append(item) return items @@ -215,31 +215,31 @@ def parse_oed(word): items.append(item) return items -def get_mysql(word): +def get_sql(word): items = [] types = ["n", "v", "adj", "adv"] - try: - con = mysql.connect('localhost', 'wordnet', 'words', 'wordnet'); - with con: - cur = con.cursor(mysql.cursors.DictCursor) - cur.execute("SELECT * from definitions where word = %s ORDER BY type_id,sub_id;", [word]) + con = sqlite3.connect('dictionaries/wordnet.db'); + con.row_factory = sqlite3.Row - rows = cur.fetchall() + with con: + + cur = con.cursor() + cur.execute("SELECT * from definitions where word = ? ORDER BY type_id,sub_id;", [word]) - for row in rows: - item = Definition(row['word'], row['sub_id'], "db", types[row['type_id']-1], row['definition'], [], row['synset_id'], []) + rows = cur.fetchall() + + for row in rows: + item = Definition(row['word'], row['sub_id'], "db", types[row['type_id']-1], row['definition'], [], row['synset_id'], []) + + cur.execute("SELECT * from uses where definition_id = ?", [row['id']]) + rows = cur.fetchall() + item.uses = rows + items.append(item) - cur.execute("SELECT * from uses where definition_id = %s", [row['id']]) - rows = cur.fetchall() - item.uses = rows - items.append(item) - except mysql.Error, e: - print "Database Error %d: %s" % (e.args[0],e.args[1]) - sys.exit(1) return items @@ -253,7 +253,7 @@ def main(): if word_dict == "oed": items = parse_oed(word) elif word_dict == "db": - items = get_mysql(word) + items = get_sql(word) else: if word_dict == "" or word_dict is None: word_dict = "wn" diff --git a/convert.py b/convert.py old mode 100644 new mode 100755 index 9e8bba4..1a07fb2 --- a/convert.py +++ b/convert.py @@ -1,18 +1,23 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + import MySQLdb as mysql +import sqlite3 +import sys def create(): try: - con = mysql.connect('localhost', 'wordnet', 'words', 'wordnet'); + con = sqlite3.connect('dictionaries/wordnet.db'); with con: - cur = con.cursor(mysql.cursors.DictCursor) - cur.execute("CREATE TABLE types (id int not null auto_increment, type text, abbreviation text, primary key(id))") + cur = con.cursor() + cur.execute("CREATE TABLE types (id integer primary key not null , type text, abbreviation text)") cur.execute("INSERT INTO types (type, abbreviation) VALUES('noun', 'n')") cur.execute("INSERT INTO types (type, abbreviation) VALUES('verb', 'v')") cur.execute("INSERT INTO types (type, abbreviation) VALUES('adjective', 'adj')") cur.execute("INSERT INTO types (type, abbreviation) VALUES('adverb', 'adv')") - cur.execute("CREATE TABLE definitions (id bigint not null auto_increment, word text, type_id int, sub_id int, synset_id bigint, definition text, primary key(id))") - cur.execute("CREATE TABLE uses (id bigint not null auto_increment, definition_id bigint, quote text, primary key(id))") + cur.execute("CREATE TABLE definitions (id integer primary key not null , word text, type_id int, sub_id int, synset_id bigint, definition text)") + cur.execute("CREATE TABLE uses (id integer primary key not null, definition_id bigint, quote text)") except mysql.Error, e: print "Database Error %d: %s" % (e.args[0],e.args[1]) @@ -35,10 +40,10 @@ def select(): def insert(rows): try: - con = mysql.connect('localhost', 'wordnet', 'words', 'wordnet'); + con = sqlite3.connect('dictionaries/wordnet.db'); with con: - cur = con.cursor(mysql.cursors.DictCursor) + cur = con.cursor() for row in rows: if row['pos'] == 'n': @@ -50,7 +55,7 @@ def insert(rows): elif row['pos'] == 'r': type_id = 4 - cur.execute("INSERT INTO definitions(word, type_id, sub_id, synset_id, definition) values(%s, %s, %s, %s, %s)", [row['lemma'], type_id, row['sensenum'], row['synsetid'], row['definition']]) + cur.execute("INSERT INTO definitions(word, type_id, sub_id, synset_id, definition) values(?, ?, ?, ?, ?)", [row['lemma'], type_id, row['sensenum'], row['synsetid'], row['definition']]) row_id = cur.lastrowid @@ -58,10 +63,10 @@ def insert(rows): uses = row['sampleset'].split("|") for use in uses: - cur.execute("INSERT INTO uses(definition_id, quote) values(%s, %s)", [row_id, use]) + cur.execute("INSERT INTO uses(definition_id, quote) values(?, ?)", [row_id, use]) - except mysql.Error, e: - print "Database Error %d: %s" % (e.args[0],e.args[1]) + except sqlite3.Error, e: + print "Database Error %s" % (e.args[0]) sys.exit(1) def main(): -- cgit v1.2.3