美文网首页
"Learn Python the Hard Way"学习笔记9

"Learn Python the Hard Way"学习笔记9

作者: los_pollos | 来源:发表于2018-01-04 17:27 被阅读0次

    Exercise 49 创建句子

    peek函数的功能:查看元组列表中的下一个成员,做匹配以后再对它做下一步动作

    def peek(word_list):
        if word_list:
            word = word_list[0]
            return word[0]
        else:
            return None
    

    match函数:

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

    skip函数:

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

    有了工具,我们现在可以从元组列表来构建句子(Sentence)对象了。我们的处理流程如下:

    1. 使用 peek 识别下一个单词。
    2. 如果这个单词和我们的语法匹配,我们就调用一个函数来处理这部分语法。假设函数的名字叫parse_subject 好了。
    3. 如果语法不匹配,我们就 raise 一个错误,接下来你会学到这方面的内容。
    4. 全部分析完以后,我们应该能得到一个 Sentence 对象,然后可以将其应用在我们的游戏中。

    作者写的程序如下,我们需要编写测试这部分的程序。

    class ParserError(Exception):
        pass 
    
        
    class Sentence(object):
        
        def __init__(self, object, verb, object):
            #remember we take ('noun','princess') tuples and convert them
            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[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 ParserError("Expected a verb next.")
            
    
    def parse_object(word_list):
        skip(word_list, 'noun')
        next = peek(word_list)
    
        if next == 'noun':
            return match(word_list, 'noun')
        if next == 'direction':
            return match(word_list, 'direction')
        else:
            raise ParserError("Expected a noun or a 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':
            #assume the subject is the player then
            return parse_subject(word_list, ('noun', 'player'))
        else:
            raise ParseError("Must start with subject, object, or verb not : %s" %start)
    

    没搞懂一开始三个函数的功能。。。编不出来:(先跳过吧

    相关文章

      网友评论

          本文标题:"Learn Python the Hard Way"学习笔记9

          本文链接:https://www.haomeiwen.com/subject/idsvvxtx.html