#!/usr/bin/python2 # -*- coding: utf-8 -*- import requests import sys import shlex from lxml import etree version = "2.0.1b" class Definition(object): #ID is relative to the word type, eg noun 1, noun 2, verb 1, verb 2, not to the entire list id = 0 word = "" dictionary = "" word_type = "" definition = "" synonyms = [] def __init__(self, word, id, dictionary, word_type, definition, synonyms, antonyms): self.word = word self.id = id self.dictionary = dictionary self.word_type = word_type self.definition = definition self.synonyms = synonyms def get_xml(): api_url = "http://services.aonaware.com/DictService/DictService.asmx/DefineInDict?dictId=wn&word=red" try: xml = etree.parse("http://services.aonaware.com/DictService/DictService.asmx/DefineInDict?dictId=wn&word=red") except IOError: print "Error: Could not access the Dictionary service." return #Root element tag is WordDefinition, which is the same as the element which contains the definition #That's akward, so let's just for element in xml.iter("{http://services.aonaware.com/webservices/}Definitions"): root = element break; for element in root.iter("{http://services.aonaware.com/webservices/}WordDefinition"): print element.text return root def parse_xml(xml): #Only worrying about one definition for now, add multiples later for element in xml.iter("{http://services.aonaware.com/webservices/}WordDefinition"): definition = element.text break; definition_lines = definition.split("\n") items = [] synonyms = [] antonyms = [] id = 0 syn_line = False ant_line = False for line in definition_lines[1:]: extra_parts = line.split("[") line_parts = extra_parts[0].split(":") #Work out if this is a line that includes identifying information (word type or id) #This is done by checking the indenting - if the first 8 characters are spaces, it's not #It's a bit hacky, maybe change this later id_line = False for char in line_parts[0][0:7]: if char != " ": id_line = True if id_line: if id > 0: #Add a previous line to the array of definitions, if there is one item = Definition(id, "bla", "wn", word_type, definition, [], []) items.append(item) synonyms = [] antonyms = [] syn_line = False ant_line = False id_parts = line_parts[0].strip().split(" ") if id_parts[0] == "[also": #This line comes at the end, don't worry about it for now break; if id_parts[0].isdigit() != True: word_type = id_parts[0] id = id_parts[1] definition = line_parts[1].strip() + " " else: id = id_parts[0] definition = line_parts[1].strip() + " " for word in line_parts[2:]: definition += word + " " else: definition += line_parts[0].strip() + " " if len(line_parts) > 1: for part in line_parts[1:-1]: definition += part + ":" definition += line_parts[-1] if len(extra_parts) > 0: for part in extra_parts: extra_words = part.split(" ") if extra_words[0] == "syn:": for syn in extra_words[1:]: synonyms.append(syn.strip("{},]")) syn_line = True elif extra_words[0] == "ant:": for ant in extra_words[1:]: antonyms.append(ant.strip("{},]")) ant_line = True return items def parse_args(): args = sys.argv[1:] if not args: args = shlex.split(sys.stdin.read()) types = ["n", "noun", "v", "verb", "adj", "adjective", "adv", "adverb"] dicts = ["wn", "wordnet", "oed"] word = args[0] word_type = "" word_dict = "" if len(args) > 1: for arg in args[1:]: if arg in types: word_type = arg elif arg in dicts: word_dict = arg return word, word_type, word_dict def parse_oed(word): types = ["n.", "—n.", "v.", "—v.", "adj.", "—adj.", "adv.", "—adv."] file = open("oed.txt") for line in file: word_part = line.split(" ")[0] if word_part == word: word_line = line break; if word_line is None: return print line prev_part = "" items = [] id = 0 definition = "" for part in line.split(" ")[1:]: if part in types: if id > 0: item = Definition(id, word, "oed", word_type, definition, [], []) items.append(item) definition = "" word_type = part elif part.isdigit() and prev_part is not None and prev_part[-1] == ".": item = Definition(id, word, "oed", word_type, definition, [], []) items.append(item) definition = "" id = part else: definition += part + " " prev_part = part return items def main(): word, word_type, word_dict = parse_args() if word_dict == "oed": items = parse_oed(word) else: xml = get_xml() if xml is None: return items = parse_xml(xml) for item in items: print item.definition print "-" if __name__ == "__main__": main()