Learn Python The Hard Way学习(49) - 创建句子
2012-07-12 14:26
323 查看
>>> from ex48 import lexicon >>> print lexicon.scan("go north") [('verb', 'go'), ('direction', 'north')] >>> print lexicon.scan("kill the princess") [('verb', 'kill'), ('stop', 'the'), ('noun', 'princess')] >>> print lexicon.scan("eat the bear") [('verb', 'eat'), ('stop', 'the'), ('noun', 'bear')] >>> print lexicon.scan("open the door and smack the bear in the nose") [('error', 'open'), ('stop', 'the'), ('noun', 'door'), ('error', 'and'), ('error', 'smack'), ('stop', 'the'), ('noun', 'bear'), ('stop', 'in'), ('stop', 'the'), ('error', 'nose')] >>>
现在让我们转换为游戏可以使用的东西,我们需要一个Sentence类。
一个句子大概的结构是这样的:
主语 谓语 宾语
当然有更加复杂的句子,但是游戏中我们只使用这三个类型组成句子。
匹配和Peek
做这些事情,我们需要四个工具:
循环元组列表的方法。
匹配不同类型的元组的方法。
一个peel元组的方法,以便我们做出决定。
一个跳过不需要词语的方法。
我们使用peek函数寻找元组列表的下一个元素,然后做匹配工作:
def peek(word_list): if word_list: word = word_list[0] return word[0] else: return None
[/code]
匹配函数如下:
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
[/code]
跳过函数:
def skip(word_list, word_type): while peek(word_list) == word_type: match(word_list, word_type)
[/code]
句子语法
用上面的工具我们可以开始组建句子了。过程是这样的:
使用peek函数定义下一个词语。
如果这个词语和我们的语法匹配,那么就用一个叫parse_subject的方法处理。
如果不匹配,就抛出一个错误。
完成后,我们就能得到一个句子了。
上面的练习是给出你测试代码,下面我给出运行代码,你要去写测试代码。
class ParserError(Exception):
pass
class Sentence(object):
def __init__(self, subject, 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.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 ParserError("Expected 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 ParserError("Expected 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':
# 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)
[/code]
关于异常
我们已经学习了一些关于异常的知识,但是没学怎么抛出他们。我们定义了ParserError类,使用raise抛出异常。
你的测试代码也能检测到一些异常。
什么是你应该测试的?
为这个练习写一个完整的测试代码。
使用assert_raises函数检测一个异常。从nose文档中学习关于assert_raises的知识。
当你完成这个工作的时候,你要知道它的工作原理,并且知道怎么为别人的代码写测试用例。相信我,这是很有用的技能。
加分练习
改变parse_开头的函数,放到一个类中。
提高parser的错误抵抗能力,当更好的忽略错误的输入。
改进语法,让它能出来更多事情,比如数字。
想想Sentence类能在游戏中做什么有趣的事情。
相关文章推荐
- Learn Python The Hard Way学习(16) - 读写文件
- Learn Python The Hard Way学习(24) - 更多的练习
- Learn Python The Hard Way学习(50) - 你的第一个网站
- Learn Python The Hard Way学习(17) - 更多的文件操作
- Learn Python The Hard Way学习(25) - 甚至更多的练习
- Learn Python The Hard Way学习(4) - 变量和命名
- Learn Python The Hard Way学习(51) - 从浏览器取得输入
- Learn Python The Hard Way学习(26) - 恭喜,可以进行期中考试了!
- Learn Python The Hard Way学习(5) - 更多的变量和打印
- Learn Python The Hard Way学习(8) - 打印,还是打印
- Learn Python The Hard Way学习(29) - 什么是If
- Learn Python The Hard Way学习(9) - 打印,打印,打印
- Learn Python The Hard Way学习(38) - 列表操作
- Learn Python The Hard Way学习(3) - 数字和数学计算
- Learn Python The Hard Way学习(10) - 那是什么?
- Learn Python The Hard Way学习(30) - Else和If
- Learn Python The Hard Way学习(31) - 做出选择
- Learn Python The Hard Way学习(39) - 可爱的字典
- Learn Python The Hard Way学习(1) - 好的开始
- Learn Python The Hard Way学习(6) - 字符串和文本