class DefinitionNotFound(TypeError):
	def __str__(self):
		return "No definition found for: " + ", ".join(t.__name__ for t in self.args)

def parse_sentence(sentence):
	g = Graph()
	lastword = None
	for word in reversed(sentence.split()):
		word = classify(word, g)
		if lastword is not None:
			try:
				actions = main_pair_actions[type(word)]
			except KeyError:
				raise(DefinitionNotFound(type(word), AnyWordType))

			try:
				funlist = actions[type(lastword)]
			except KeyError:
				raise(DefinitionNotFound(type(word), type(lastword)))
			
			lastword = run_function_list(funlist, (word,lastword))
		else:
			lastword = word

	return g
	
if __name__ == "__main__":
	res = parse_sentence("the bright green dog chases the blue cat") 
	from magicate import prettify
	print prettify(res)
Beispiel #2
0
while True:
    command = raw_input(": ").lower()
    if "?" == command:
        query = graphmodule.Graph()
        while True:
            line = raw_input("? ")
            if line == "":
                break
            else:
                query.add(*line.split(" "))
        print database.query(query)
    elif command.endswith(".png"):
        withoutdictionary().as_pydot_graph().write_png(command)
    elif "pp" == command:
        print prettify(withoutdictionary())
    elif "exit" == command:
        break
    elif ">" == command:
        while True:
            line = raw_input("> ")
            if line == "":
                break
            words = line.split(" ")
            if last_query is not None:
                words = tuple(last_query[word] if word in last_query else word for word in words)
            database.add(*words)
    else:
        try:
            g = parse_sentence(command.replace(".", ""))
        except WordNotFound as w: