From 57d881279c1267c6d2fa98b90fb1496f138f466a Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 15:07:00 +0100 Subject: Added Urban Dictionary definitions --- bladictionary.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'bladictionary.py') diff --git a/bladictionary.py b/bladictionary.py index 6cdaa03..b5e3099 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -6,8 +6,9 @@ import shlex import optparse from lxml import etree import sqlite3 +import requests -VERSION = "2.2.2" +VERSION = "2.3.0" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -152,7 +153,7 @@ def parse_args(): options, args = parser.parse_args( args ) types = ["n", "noun", "v", "verb", "adj", "adjective", "adv", "adverb", "tech"] - dicts = ["wn", "wordnet", "oed", "db", "foldoc"] + dicts = ["wn", "wordnet", "oed", "db", "foldoc", "urban"] word = "" word_type = "" @@ -413,7 +414,20 @@ def parse_foldoc(word, refer = None): items.append(item) return items +def parse_urban(word): + r = requests.get("http://urbanscraper.herokuapp.com/search/" + word) + json = r.json() + items = [] + id = 1 + + for json_item in json: + if json_item['definition'] != "" and json_item['definition'] is not None: + item = Definition(word, id, "urban", "urban", json_item['definition'], [json_item['example']], [], []) + items.append(item) + id += 1 + + return items def main(): @@ -435,6 +449,8 @@ def main(): items = parse_xml(xml) elif word_dict == "foldoc" or word_type == "tech": items = parse_foldoc(word) + elif word_dict == "urban": + items = parse_urban(word) else: if word_dict is None or word_dict == "": word_dict = "db" @@ -514,7 +530,8 @@ def main(): #Print usage examples if they exist if len(item.uses) > 0: - definition += "; \""+item.uses[0]['quote']+"\" " + definition = definition.rstrip(". ") + definition += "; \""+item.uses[0]+"\" " if not all_types and line_length + len(definition) < (max_length * (page_num -1)): line_length += len(definition) +1 -- cgit v1.2.3 From 1e81f6bbad20ca5e821f40b7024ee95588f07465 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 15:31:43 +0100 Subject: Added item uses from wordnet database --- bladictionary.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'bladictionary.py') diff --git a/bladictionary.py b/bladictionary.py index b5e3099..a46137d 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -8,7 +8,7 @@ from lxml import etree import sqlite3 import requests -VERSION = "2.3.0" +VERSION = "2.3.1" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -147,7 +147,7 @@ def parse_args(): if not args: args = shlex.split(sys.stdin.read()) - parser = optparse.OptionParser( usage = "!define " ) + parser = optparse.OptionParser( usage = "!define [type] [dictionary] | types: noun, verb, adjective, adverb | dictionaries: wordnet, oed, foldoc, urban" ) parser.add_option( "-v", "--version", action = "store_true", help = "Print the version number" ) parser.add_option( "-c", "--channel", action = "store", help = "The IRC channel of the request") options, args = parser.parse_args( args ) @@ -294,11 +294,22 @@ def get_sql(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 = ?", [row['id']]) - rows = cur.fetchall() - item.uses = rows + id = row['id'] + word = row['word'] + sub_id = row['sub_id'] + type = types[row['type_id']-1] + definition = row['definition'] + synset_id = row['synset_id'] + + cur.execute("SELECT * from uses where definition_id = ?", [id]) + use_rows = cur.fetchall() + uses = [] + + for use in use_rows: + uses.append(use['quote']) + + item = Definition(word, sub_id, "wn", type,definition, uses, synset_id, []) items.append(item) return items @@ -453,7 +464,7 @@ def main(): items = parse_urban(word) else: if word_dict is None or word_dict == "": - word_dict = "db" + word_dict = "wn" items = get_sql(word) foldoc_items = parse_foldoc(word) if foldoc_items is not None and len(foldoc_items) > 0: @@ -475,7 +486,7 @@ def main(): else: max_length = 460 - types = ["n", "v", "adj", "adv", "tech"] + types = ["n", "v", "adj", "adv", "tech", "urban"] type_id = 0 all_types = word_type is "" -- cgit v1.2.3 From 9d6a2b3992693a10b2cd3feb43d8e92dda718943 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 16:12:16 +0100 Subject: Add urban dictionary definitions when no dictionary is specified --- bladictionary.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'bladictionary.py') diff --git a/bladictionary.py b/bladictionary.py index a46137d..7a3112a 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -8,7 +8,7 @@ from lxml import etree import sqlite3 import requests -VERSION = "2.3.1" +VERSION = "2.3.2" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -466,10 +466,15 @@ def main(): if word_dict is None or word_dict == "": word_dict = "wn" items = get_sql(word) + foldoc_items = parse_foldoc(word) if foldoc_items is not None and len(foldoc_items) > 0: items += foldoc_items + urban_items = parse_urban(word) + if urban_items is not None and len(urban_items) > 0: + items += urban_items + if items is None or len(items) == 0: print "No definitions found for "+word sys.exit( 1 ) @@ -501,7 +506,6 @@ def main(): word_type = word_types[0] all_types = False - for item in items: #If no type is specified, we display one of each if all_types: @@ -514,6 +518,7 @@ def main(): continue #Definitions should be ordered by type, so loop through all the types until we find a match for cur_type in types[type_id:]: + if item.word_type == cur_type: word_type = cur_type type_id = cur_id @@ -523,7 +528,9 @@ def main(): #If there were no matches, stick with the current type for now if not found_type: - word_type = types[type_id] + + if type_id < len(types): + word_type = types[type_id] if word_type == item.word_type: #Keep track of how many we haven't printed -- cgit v1.2.3 From 9b9606137f48284aae9918b04769d7485d417239 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 16:36:52 +0100 Subject: Added error checking for JSON parsing --- bladictionary.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'bladictionary.py') diff --git a/bladictionary.py b/bladictionary.py index 7a3112a..c3ee7db 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -8,7 +8,7 @@ from lxml import etree import sqlite3 import requests -VERSION = "2.3.2" +VERSION = "2.3.3" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list @@ -426,13 +426,17 @@ def parse_foldoc(word, refer = None): return items def parse_urban(word): - r = requests.get("http://urbanscraper.herokuapp.com/search/" + word) - json = r.json() + try: + r = requests.get("http://urbanscraper.herokuapp.com/search/" + word) + json = r.json() + except: + #Should probably print an error, but then that would break printing other definitions, so let's not + return items = [] id = 1 - for json_item in json: + print json_item if json_item['definition'] != "" and json_item['definition'] is not None: item = Definition(word, id, "urban", "urban", json_item['definition'], [json_item['example']], [], []) items.append(item) -- cgit v1.2.3 From 2c5652f1d098aa5b7590ee1bda13b9968d27258a Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 17:45:24 +0100 Subject: Fix multi word definitions for urban dictionary --- bladictionary.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bladictionary.py') diff --git a/bladictionary.py b/bladictionary.py index c3ee7db..5e87b71 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -426,6 +426,9 @@ def parse_foldoc(word, refer = None): return items def parse_urban(word): + + word = word.replace(" ", "+") + try: r = requests.get("http://urbanscraper.herokuapp.com/search/" + word) json = r.json() @@ -436,7 +439,6 @@ def parse_urban(word): items = [] id = 1 for json_item in json: - print json_item if json_item['definition'] != "" and json_item['definition'] is not None: item = Definition(word, id, "urban", "urban", json_item['definition'], [json_item['example']], [], []) items.append(item) -- cgit v1.2.3 From bae7d4777f46144750ed3d709e45da283c1f3516 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 23 Sep 2014 17:45:40 +0100 Subject: Bump version --- bladictionary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bladictionary.py') diff --git a/bladictionary.py b/bladictionary.py index 5e87b71..6d16240 100755 --- a/bladictionary.py +++ b/bladictionary.py @@ -8,7 +8,7 @@ from lxml import etree import sqlite3 import requests -VERSION = "2.3.3" +VERSION = "2.3.4" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list -- cgit v1.2.3