#! /usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/7/8 8:33 # @Author : xiongyaokun # @Site : # @File : practice49.py # @Software: PyCharm from lexicon import Lexicon lexicon = Lexicon() print lexicon.scan("go north door smack") print lexicon.scan("eat the bear")
def parse_sentence(word_list): skip(word_list, 'stop') start = peek(word_list) if start == 'noun': subj = match(word_list, 'noun') return parse_subject(word_list, subj) elif start == 'verb': #assume the subject is the player then return parse_subject(word_list, ('noun', 'player')) else: raise ParserError("Must start with subject, object, or verb not: %s" % start) class ParserError(Exception): pass class Sentence(object): def __init__(self, subject, verb, object): # remember we take ('noun', 'princess') tuples ad convert them self.subject = subject[1] self.verb = verb[1] self.object = object[1] lexicon = Lexicon() word_list = lexicon.scan("go north door")