《笨办法学Python》笔记36-----创建句子

创建句子

上节是拆分句子,这节要将拆出的词汇再次组成句子。

一个简单句子是由

主语(subject)+谓语(动词 verb)+宾语(object)

构成的

class ParseError(Exception):
    pass

class Sentence(object):

    def __init__(self, subject, verb, object):
        self.subject = subject[1]
        self.verb = verb[1]
        self.object = object[1]
    
    def peek(word_list):
        if word_list:
            word = word_list[0]
            return word[0]
        else:
            return None

    def match(word_list,expecting):
        if word_list:
            word = word_list.pop(0)
            if word[0] == expecting:
                return word
            else:
                return None
        else:
                return None

    def skip(word_list,word_type):
        while peek(word_list) == word_type:
            match(word_list,word_type)

    def parse_verb(word_list):
        skip(word_list,'stop')

        if peek(word_list) == 'verb':
            return match(word_list,'verb')
        else:
            raise ParseError("Excepted a verb next.")

    def parse_object(word_list):
        skip(word_list,'stop')
        next = peek(word_list)
        if next == 'noun':
            return match(word_list,'noun')
        if next == 'direction':
            return match(word_list,'direction')
        else:
            raise ParseError("Excepted a noun or direction next.")

    def parse_subject(word_list,subj):
        verb = parse_verb(word_list)
        obj = parse_object(word_list)
        return Sentence(subj,verb,obj)

    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':
            return parse_subject(word_list,('noun','player'))
        else:
            raise ParseError("must start with subject, object, or verb not : %s" % start)

你可能感兴趣的:(《笨办法学Python》笔记36-----创建句子)