笨办法学Python3 ex49 被测试代码, 分析了ex49 Zed写的源码, 自己学着写了一个有一点不一样, 还没有写最后几个抛错类和sentence类, 不知道直接用书上的,会不会有问题.
不管了, 先试着传入列表,简单的测试一下, 为了看出三个分析者拿到的word_list是什么糖果, 直接在三个函数第一行就打印了拿到的糖果. 测试文件还没写, 感觉有点烧脑子.
下面是运行结果
我在parse_sub里: [('noun', 'fighter'), ('throw', 'will'), ('verb', 'kill'), ('throw', 'the'), ('noun', 'bear'), ('throw', 'and'), ('noun', 'beefs')]
('noun', 'fighter')
我在parse_verb里: [('verb', 'kill'), ('throw', 'the'), ('noun', 'bear'), ('throw', 'and'), ('noun', 'beefs')]
('verb', 'kill')
我在parse_obj里: [('noun', 'bear'), ('throw', 'and'), ('noun', 'beefs')]
('noun', 'bear')
下面是被测试源文件(半成品)
# %%writefile parser.py
li =[('throw', 'the'), ('noun', 'fighter'),('throw', 'will'),('verb', 'kill'),('throw', 'the'),('noun', 'bear')]
def peek(word_list): # [('throw' , 'the')('noun', 'car'), ('verb', 'hit'),('noun', 'me')]
if word_list:
word = word_list[0]
return word[0] # return 'noun'
else:
pass
def match(word_list): # 整理者 分析前整理拿到的列表
if word_list:
word = word_list.pop(0) # 分析一个删除一个,避免后面重复工作
return word
else:
pass
def parse_sub(word_list): # 主语分析者
new_word_list = skip(word_list)
print('我在parse_sub里:', new_word_list)
if peek(new_word_list) == 'noun':
return match(new_word_list)
else:
print('我需要主语')
def parse_verb(word_list): # 谓语分析者
new_word_list = skip(word_list)
print('我在parse_verb里:', new_word_list)
if peek(new_word_list) == 'verb':
return match(new_word_list)
else:
print('我需要谓语')
def parse_obj(word_list): # 宾语分析者
new_word_list = skip(word_list)
print('我在parse_obj里:', new_word_list)
if peek(new_word_list) == 'noun':
return match(new_word_list)
elif peek(new_word_list) == 'direction':
return match(new_word_list)
elif peek(new_word_list) == 'verb':
return ('noun', 'Someone')
else:
print('我需要宾语')
def skip(word_list): #垃圾分析者
while peek(word_list) == 'throw':
word_list.pop(0)
return word_list
print(parse_sub(li))
print(parse_verb(li))
print(parse_obj(li))
晚上又折腾了几小时,终于把ex49的被测试文件与nosetests测试文件也写好了,眼花了.
下面是 被测试代码
# %%writefile parser.py
# li =[('throw', 'the'), ('noun', 'fighter'),('throw', 'will'),('verb', 'kill'),('throw', 'the'),('noun', 'bear')]
class ParserError(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): # [('throw' , 'the')('noun', 'car'), ('verb', 'hit'),('noun', 'me')]
if word_list:
word = word_list[0]
return word[0] # return 'noun'
else:
return None
def match(word_list): # 整理者 分析前整理拿到的列表
if word_list:
word = word_list.pop(0) # 分析一个删除一个,避免后面重复工作
return word
else:
return None
def parse_sub(word_list): # 主语分析者
new_word_list = skip(word_list)
print('我在parse_sub里:', new_word_list)
if peek(new_word_list) == 'noun':
return match(new_word_list)
else:
raise ParserError('我需要主语')
def parse_verb(word_list): # 谓语分析者
new_word_list = skip(word_list)
print('我在parse_verb里:', new_word_list)
if peek(new_word_list) == 'verb':
return match(new_word_list)
else:
raise ParserError('我需要谓语')
def parse_obj(word_list): # 宾语分析者
new_word_list = skip(word_list)
print('我在parse_obj里:', new_word_list)
if peek(new_word_list) == 'noun':
return match(new_word_list)
elif peek(new_word_list) == 'direction':
return match(new_word_list)
elif peek(new_word_list) == 'verb':
return ('noun', 'Someone')
else:
raise ParserError('我需要宾语')
def skip(word_list): #垃圾分析者
while peek(word_list) == 'throw':
word_list.pop(0)
return word_list
def parser_sentence(word_list):
subj = parse_sub(word_list)
vert = parse_verb(word_list)
obj = parse_obj(word_list)
return Sentence(subj, vert, obj)
下面是测试代码
# %%writefile parser_tests.py
from nose.tools import *
from ex49 import parser
li =[ ('noun', 'fighter'),('throw', 'will'),('verb', 'kill'),('throw', 'the'),('noun', 'bear')]
def test_peek():
result = parser.peek([('throw' , 'the'),('noun', 'car'), ('verb', 'hit'),('noun', 'me')])
assert_equal(result, ('throw'))
def test_match():
result = parser.match([('throw' , 'the'), ('noun', 'fighter')])
assert_equal(result, ('throw' , 'the'))
result = parser.match(li)
assert_equal(result, ('noun', 'fighter'))
result = parser.match([])
assert_equal(result, None)
def test_parse_sub():
result = parser.parse_sub([('noun', 'bear'),('verb', 'kill')])
assert_equal(result, ('noun', 'bear'))
result = parser.parse_sub([ ('noun', 'fighter'),('throw', 'will'),('verb', 'kill'),('throw', 'the'),('noun', 'bear')] )
assert_equal(result, ('noun', 'fighter'))
assert_raises(parser.ParserError, parser.parse_sub, [('throw', 'will'),('verb', 'kill'),('throw', 'the')])
def test_parse_verb():
result = parser.parse_verb([('verb', 'kill'),('throw', 'the'),('noun', 'bear')])
assert_equal(result, ('verb', 'kill'))
result = parser.parse_sub([('noun', 'fighter'),('throw', 'will'),('verb', 'kill'),('throw', 'the'),('noun', 'bear')])
assert_equal(result, ('noun', 'fighter'))
result = parser.parse_obj([('throw', 'will'),('verb', 'kill')])
assert_equal(result, ('noun', 'Someone'))
assert_raises(parser.ParserError, parser.parse_verb, [('noun', 'fighter'),('throw', 'will')])
def test_parse_obj():
result = parser.parse_obj([('throw', 'the'),('noun', 'bear')])
assert_equal(result, ('noun', 'bear') )
result = parser.parse_sub([('noun', 'fighter'),('throw', 'will'),('verb', 'kill'),('throw', 'the'),('noun', 'bear')])
assert_equal(result, ('noun', 'fighter'))
result = parser.parse_verb([('verb', 'kill'),('throw', 'the'),('noun', 'bear')])
assert_equal(result, ('verb', 'kill'))
result = parser.parse_obj([('verb', 'kill'),('throw', 'the'),('noun', 'bear')])
assert_equal(result, ('noun', 'Someone'))
assert_raises(parser.ParserError, parser.parse_obj, [('throw', 'will'),('throw', 'the')])
def test_parser_sentence():
result = parser.parser_sentence([('noun', 'i'),('throw', 'will'),('verb', 'go'),('throw', 'to'),('throw', 'the'), ('noun', 'beach')])
assert_equal(result.subject, 'i')
assert_equal(result.verb, 'go')
assert_equal(result.object, 'beach')
下面是nosetests测试结果
(ipthw) PS C:\Users\wggglgggmini\OneDrive\venvs\ipthw\projects\ex49> nosetests
......
----------------------------------------------------------------------
Ran 6 tests in 0.028s
OK
(ipthw) PS C:\Users\wggglgggmini\OneDrive\venvs\ipthw\projects\ex49> nosetests
......
----------------------------------------------------------------------
Ran 6 tests in 0.031s
OK