diff options
author | Joe Robinson <joe@mumsnet.com> | 2014-09-19 14:13:01 +0100 |
---|---|---|
committer | Joe Robinson <joe@mumsnet.com> | 2014-09-19 14:13:01 +0100 |
commit | e84387a29ca3227760cc2025015361a197d010a1 (patch) | |
tree | 7c99499a808d11c40846a2211ee74a1d0e6bd4d7 /bladictionary.py | |
parent | 319e9b25d5ce3908ec83a482ae4d3dc98a7493a4 (diff) |
Changed wordnet database to sqlite
Diffstat (limited to 'bladictionary.py')
-rwxr-xr-x | bladictionary.py | 40 |
1 files changed, 20 insertions, 20 deletions
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" |