w1w 发表于 2017-5-7 12:03:57

Learn Python The Hard Way学习(49)

>>> 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
return word
else:
return None



匹配函数如下:

def match(word_list, expecting):
if word_list:
word = word_list.pop(0)
if word == 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)




句子语法

用上面的工具我们可以开始组建句子了。过程是这样的:


[*]使用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
self.verb = verb
self.object = object

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

def match(word_list, expecting):
if word_list:
word = word_list.pop(0)
if word == 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)




关于异常

我们已经学习了一些关于异常的知识,但是没学怎么抛出他们。我们定义了ParserError类,使用raise抛出异常。




你的测试代码也能检测到一些异常。




什么是你应该测试的?

为这个练习写一个完整的测试代码。




使用assert_raises函数检测一个异常。从nose文档中学习关于assert_raises的知识。




当你完成这个工作的时候,你要知道它的工作原理,并且知道怎么为别人的代码写测试用例。相信我,这是很有用的技能。




加分练习


[*]改变parse_开头的函数,放到一个类中。
[*]提高parser的错误抵抗能力,当更好的忽略错误的输入。
[*]改进语法,让它能出来更多事情,比如数字。
[*]想想Sentence类能在游戏中做什么有趣的事情。
页: [1]
查看完整版本: Learn Python The Hard Way学习(49)