1.创建目录结构:
首先创建文件ex49,在ex49中创建skeleton,具体文件如下:
2.parser.py文件中的代码:
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):
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):
result=‘‘
while peek(word_list)==word_type:
result = match(word_list,word_type)
return result
def parse_verb(word_list):
skip(word_list,‘stop‘)
if peek(word_list)==‘verb‘:
return match(word_list,‘verb‘)
else:
raise ParserError("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 ParserError("Excepted a noun or direction next.")
def parse_subject(word_list,subj):
verb=parse_verb(word_list)
obj=parse_object(word_list)
a=Sentence(subj,verb,obj)
return a
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 ParserError("Must start with subject,object,or verb not :%s %start")
3.parse_tests.py中为对parser.py进行单元测试代码:
from nose.tools import *
import sys
sys.path.append(‘H:\\ex49\\skeleton‘)
import ex49.parser
word_listA=[(‘verb‘,‘kill‘),(‘direction‘,‘north‘)]
def test_peek():
result=ex49.parser.peek([(‘verb‘,‘kill‘),(‘direction‘,‘north‘)])
assert_equal(result,‘verb‘)
def test_match():
result=ex49.parser.match(word_listA,‘verb‘)
assert_equal(result,(‘verb‘,‘kill‘))
assert_equal(word_listA,[(‘direction‘,‘north‘)])
result1=ex49.parser.match(word_listA,‘stop‘)
assert_equal(result1,None)
def test_skip():
result=ex49.parser.skip([(‘verb‘,‘kill‘),(‘direction‘,‘north‘)],‘verb‘)
assert_equal(result,(‘verb‘,‘kill‘))
def test_parse_verb():
result=ex49.parser.parse_verb([(‘verb‘,‘kill‘),(‘direction‘,‘north‘)])
assert_equal(result,(‘verb‘,‘kill‘))
assert_raises(ex49.parser.ParserError,ex49.parser.parse_verb,[(‘stop‘,‘the‘),(‘direction‘,‘north‘)])
def test_parse_object():
result=ex49.parser.parse_object([(‘stop‘,‘the‘),(‘noun‘,‘door‘)])
assert_equal(result,(‘noun‘,‘door‘))
result1=ex49.parser.parse_object([(‘stop‘,‘the‘),(‘direction‘,‘north‘)])
assert_equal(result1,(‘direction‘,‘north‘))
assert_raises(ex49.parser.ParserError,ex49.parser.parse_object,[(‘stop‘,‘the‘),(‘verb‘,‘go‘)])
def test_parse_subject():
result=ex49.parser.parse_subject([(‘verb‘,‘go‘),(‘stop‘,‘the‘),(‘noun‘,‘door‘)],(‘m‘,‘k‘))
assert_equal(result.subject,‘k‘)
assert_equal(result.verb,‘go‘)
assert_equal(result.object,‘door‘)
def test_parse_sentence():
result=ex49.parser.parse_sentence([(‘verb‘,‘go‘),(‘stop‘,‘the‘),(‘noun‘,‘door‘)])
assert_equal(result.subject,‘player‘)
assert_equal(result.verb,‘go‘)
assert_equal(result.object,‘door‘)
result1=ex49.parser.parse_sentence([(‘stop‘,‘in‘),(‘noun‘,‘door‘),(‘verb‘,‘go‘),(‘direction‘,‘north‘)])
assert_equal(result1.subject,‘door‘)
assert_equal(result1.verb,‘go‘)
assert_equal(result1.object,‘north‘)
assert_raises(ex49.parser.ParserError,ex49.parser.parse_sentence,[(‘stop‘,‘in‘),(‘stop‘,‘the‘)])
4.遇到的问题:
1)从ex49中导入parse.py时,用from ex49 import parse出错
提示ImportError: No module named ex49
发现是同级目录导入文件方式错误,参考http://blog.chinaunix.net/uid-26000296-id-4372344.html文章中第三点
最后更改为以下导入方式才可行:
import sys
sys.path.append(‘H:\\ex49\\skeleton‘)
import ex49.parser