Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d554a5f

Browse files
committed
ex49 创建句子(里面有我写的测试)
1 parent 7a467dc commit d554a5f

File tree

11 files changed

+133
-0
lines changed

11 files changed

+133
-0
lines changed

ex49/skeleton/bin/这里放需要的脚本

Whitespace-only changes.

ex49/skeleton/docs/这里放文档

Whitespace-only changes.

ex49/skeleton/ex49/__init__.py

Whitespace-only changes.

ex49/skeleton/ex49/__init__.pyc

137 Bytes
Binary file not shown.

ex49/skeleton/ex49/ex49.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class ParserError(Exception):
2+
pass
3+
4+
class Sentence(object):
5+
6+
def __init__(self, subject, verb, object):
7+
self.subject = subject[1]
8+
self.verb = verb[1]
9+
self.object = object[1]
10+
11+
def peek(word_list):
12+
if word_list:
13+
word= word_list[0]
14+
return word[0]
15+
else:
16+
return None
17+
18+
def match(word_list, expecting):
19+
if word_list:
20+
word = word_list.pop(0)
21+
22+
if word[0] == expecting:
23+
return word
24+
else:
25+
return None
26+
else:
27+
return None
28+
29+
30+
def skip(word_list, word_type):
31+
while peek(word_list) == word_type:
32+
match(word_list, word_type)
33+
34+
35+
def parse_verb(word_list):
36+
skip(word_list, 'stop')
37+
38+
if peek(word_list) == 'verb':
39+
return match(word_list, 'verb')
40+
else:
41+
raise ParserError("Expected a verb next.")
42+
43+
def parse_object(word_list):
44+
skip(word_list, 'stop')
45+
next = peek(word_list)
46+
47+
if next == 'noun':
48+
return match(word_list, 'noun')
49+
if next == 'direction':
50+
return match(word_list, 'direction')
51+
else:
52+
raise ParserError("Expected a noun or direction next.")
53+
54+
def parse_subject(word_list, subj):
55+
verb = parse_verb(word_list)
56+
obj = parse_object(word_list)
57+
58+
return Sentence(subj, verb, obj)
59+
60+
def parse_sentence(word_list):
61+
skip(word_list, 'stop')
62+
63+
start = peek(word_list)
64+
65+
if start == 'noun':
66+
subj = match(word_list, 'noun')
67+
return parse_subject(word_list, subj)
68+
elif start == 'verb':
69+
return parse_subject(word_list, ('noun', 'player'))
70+
else:
71+
raise ParserError("Must start with subject, object, or verb not:%s" % start)

ex49/skeleton/ex49/ex49.pyc

2.62 KB
Binary file not shown.

ex49/skeleton/setup.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
try:
2+
from setuptools import setup
3+
except ImportError:
4+
from distutils.core import setup
5+
6+
7+
cnfig = {
8+
'description': 'My Project',
9+
'author': 'XueWeiHan',
10+
'url': 'URL to get it at.',
11+
'download_url': 'Where to download it.',
12+
'author_email': 'My email.',
13+
'version': '0.1',
14+
'install_requirse': ['nose'],
15+
'packages': ['NAME'],
16+
'scripts': [],
17+
'name': 'projectname'
18+
}
19+
20+
setup(**config)
21+

ex49/skeleton/tests/__init__.py

Whitespace-only changes.

ex49/skeleton/tests/__init__.pyc

138 Bytes
Binary file not shown.

ex49/skeleton/tests/ex49_tests.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#coding:utf-8
2+
from nose.tools import *
3+
from ex49.ex49 import *
4+
# 输入nosetests,开始测试一个
5+
# 输出结果如下:
6+
# qq@qq-virtual-machine:~/git/projects/skeleton$ nosetests
7+
# ....
8+
# ----------------------------------------------------------------------
9+
# Ran 4 tests in 0.004s
10+
#
11+
# OK
12+
13+
def test_Sentence():
14+
sentence = Sentence(('noun', 'apple'), ('verb', 'eat'), ('direction', 'good'))
15+
assert_equal(sentence.subject, 'apple')
16+
17+
def test_peek():
18+
#sentence = Sentence(('noun', 'apple'), ('verb', 'eat'), ('direction', 'good'))
19+
peek1 = peek([('noun', 'apple'), ('verb', 'eat'), ('direction', 'good')])
20+
peek2 = peek([('direction', 'good')])
21+
peek3 = peek([])
22+
assert_equal(peek1, 'noun')
23+
assert_equal(peek2, 'direction')
24+
assert_equal(peek3, None) # None != 'None'
25+
26+
def test_match():
27+
match1 = match([('verb', 'eat'), ('noun', 'apple')], 'verb') #match接收的是word_list
28+
assert_equal(match1, ('verb', 'eat'))
29+
30+
# 我觉得的parse_verb()方法不是需要测试——因为它还调用了别的方法(不知道我想的对不对)
31+
#def test_parse_verb():
32+
# parse_verb1 = parse_verb([('noun', 'apple'), ('verb', 'eat'), ('direction', 'good')])
33+
# assert_equal(parse_verb1,)
34+
35+
36+
def test_parse_subject():
37+
last_sentence = Sentence(('', ''), ('', ''), ('', ''))
38+
#下面的方法返回 Sentence(subj, verb, obj)
39+
last_sentence = parse_subject([('verb', 'eat'), ('direction', 'nice')], ('noun', 'apple'))
40+
41+
assert_equal(last_sentence.verb, 'eat')

0 commit comments

Comments
 (0)