diff options
| -rwxr-xr-x | bladictionary.py | 65 | 
1 files changed, 53 insertions, 12 deletions
| diff --git a/bladictionary.py b/bladictionary.py index 6cdaa03..6d16240 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.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 @@ -146,13 +147,13 @@ def parse_args():  	if not args:  		args = shlex.split(sys.stdin.read()) -	parser = optparse.OptionParser( usage = "!define <term> <type> <dictionary>" ) +	parser = optparse.OptionParser( usage = "!define <term> [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 )  	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 = "" @@ -293,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 @@ -413,7 +425,26 @@ def parse_foldoc(word, refer = None):  			items.append(item)  		return items +def parse_urban(word): + +	word = word.replace(" ", "+") + +	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: +		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,14 +466,21 @@ 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" +			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 ) @@ -459,7 +497,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 "" @@ -474,7 +512,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: @@ -487,6 +524,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 @@ -496,7 +534,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 @@ -514,7 +554,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 | 
