summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2014-09-19 01:03:09 +0100
committerJoe Robinson <joe@lc8n.com>2014-09-19 01:03:09 +0100
commit7c1a35a236152855b8cd1235e0bc07566fe81e8d (patch)
tree275ff7c03e11a7652e9b90b0e53aebca15abecf9
parentac2251f199398b813f394bdb875bc2723557781c (diff)
Add function for getting WordNet definitions from database
-rwxr-xr-xbladictionary.py47
1 files changed, 40 insertions, 7 deletions
diff --git a/bladictionary.py b/bladictionary.py
index e19e8bd..1b04a78 100755
--- a/bladictionary.py
+++ b/bladictionary.py
@@ -1,13 +1,13 @@
#!/usr/bin/python2
# -*- coding: utf-8 -*-
-import requests
import sys
import shlex
import optparse
from lxml import etree
+import MySQLdb as mysql
-VERSION = "2.0.12b"
+VERSION = "2.1.2b"
class Definition(object):
#ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list
@@ -16,15 +16,17 @@ class Definition(object):
dictionary = ""
word_type = ""
definition = ""
+ uses = []
synonyms = []
antonyms = []
- def __init__(self, word, id, dictionary, word_type, definition, synonyms, antonyms):
+ def __init__(self, word, id, dictionary, word_type, definition, uses, synonyms, antonyms):
self.word = word
self.id = id
self.dictionary = dictionary
self.word_type = word_type
self.definition = definition
+ self.uses = uses
self.synonyms = synonyms
self.antonyms = antonyms
@@ -77,7 +79,7 @@ def parse_xml(xml):
if id_line:
if id > 0:
#Add a previous line to the array of definitions, if there is one
- item = Definition(word, id, "wn", word_type, definition, synonyms, antonyms)
+ item = Definition(word, id, "wn", word_type, definition, [], synonyms, antonyms)
items.append(item)
synonyms = []
antonyms = []
@@ -149,7 +151,7 @@ def parse_args():
options, args = parser.parse_args( args )
types = ["n", "noun", "v", "verb", "adj", "adjective", "adv", "adverb"]
- dicts = ["wn", "wordnet", "oed"]
+ dicts = ["wn", "wordnet", "oed", "db"]
word = ""
word_type = ""
@@ -191,14 +193,14 @@ def parse_oed(word):
if part in types:
if id > 0:
- item = Definition(word, id, "oed", word_type, definition, [], [])
+ item = Definition(word, id, "oed", word_type, definition, [], [], [])
items.append(item)
definition = ""
word_type = part
elif part.isdigit() and prev_part is not None and (prev_part[-1] == "." or prev_part[-1] == ")"):
- item = Definition(word, id, "oed", word_type, definition, [], [])
+ item = Definition(word, id, "oed", word_type, definition, [], [], [])
items.append(item)
definition = ""
id = part
@@ -213,6 +215,33 @@ def parse_oed(word):
items.append(item)
return items
+def get_mysql(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])
+
+ 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 = %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
def main():
word, word_type, word_dict, options = parse_args()
@@ -223,6 +252,8 @@ def main():
if word_dict == "oed":
items = parse_oed(word)
+ elif word_dict == "db":
+ items = get_mysql(word)
else:
if word_dict == "" or word_dict is None:
word_dict = "wn"
@@ -253,6 +284,8 @@ def main():
num_more+=1
else:
definition = item.word_type + " " + str(item.id) + ": " + item.definition
+ if len(item.uses) > 0:
+ definition += "; \""+item.uses[0]['quote']+"\""
if line_length + len(definition) > max_length:
suppress_print = True
num_more+= 1