- 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):
- 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) # 执行 class Sentence
- def parse_sentence(word_list):
- skip(word_list, 'stop')
- start = peek(word_list)
- if start == 'noun':
- subj = match(word_list, 'noun')
- return parse_sentence(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)
复制代码 以上是需要测试的代码。
这是我写的测试代码,其中两部分错误较相似:- word_listA = [('verb','elsker'),('direction','ikke'),('noun','Jag')]
- def test_sentence():
- sentence = Sentence([('noun','Jag'), ('verb', 'elsker'), ('direction', 'ikke')])
- assert_equal(sentence.subject, 'Jag')
复制代码- def test_parse_sentence():
- result = parse_sentence([('verb','elsker'),('noun','Jag')])
- assert_equal(result.subject, 'player')
- assert_equal(result.verb, 'elsker')
- assert_equal(result.object, 'Jag')
- result1 = parse_sentence([('noun','Jag'), ('verb','elsker'),('direction','ikke')])
- assert_equal(result1.subject, 'Jag')
- assert_equal(result1.verb, 'elsker')
- assert_equal(result1.object, 'ikke')
- assert_raises(ParserError, parse_object,[('stop','in'),('stop','the')])
复制代码 执行nosetests后,报错:
Traceback (most recent call last): File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest self.test(*self.arg) File "/Users/admin/mystuff/test/ex49_tests.py", line 6, in test_sentence sentence = Sentence([('noun','Jag'), ('verb', 'elsker'), ('direction', 'ikke')]) TypeError: __init__() takes exactly 4 arguments (2 given) ======================================================================
Traceback (most recent call last): File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest self.test(*self.arg) File "/Users/admin/mystuff/test/ex49_tests.py", line 68, in test_parse_sentence result1 = parse_sentence([('noun','Jag'), ('verb','elsker'),('direction','ikke')]) File "/Users/admin/mystuff/test/ex49.py", line 70, in parse_sentence TypeError: parse_sentence() takes exactly 1 argument (2 given)
这是另一部分及其AssertionError报错:- def test_skip():
- skipA = skip([('noun','Jag'), ('verb', 'elsker')], 'verb')
- assert_equal(skipA, ('verb', 'elsker'))
- assert_equal(skipA, None)
复制代码Traceback (most recent call last): File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest self.test(*self.arg) File "/Users/admin/mystuff/test/ex49_tests.py", line 27, in test_skip assert_equal(skipA, ('verb', 'elsker')) AssertionError: None != ('verb', 'elsker')
|