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

Skip to content

Commit bb6b558

Browse files
committed
ex48 更复杂的用户输入
1 parent cfb1657 commit bb6b558

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

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

Whitespace-only changes.

ex48/skeleton/docs/这里放文档

Whitespace-only changes.

ex48/skeleton/ex48/__init__.py

Whitespace-only changes.

ex48/skeleton/ex48/lexicon.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#coding:utf-8
2+
#class lexicon(object):
3+
# 我这里写错了,其实不应该写成类。应该是一个方法,根据模块调用的方法
4+
5+
#@staticmethod # 修饰为静态方法,调用时不惜要实例化
6+
def scan(stuff):
7+
result = []
8+
direction = ['north', 'east', 'south']
9+
verb = ['go', 'kill' , 'eat']
10+
stop = ['the', 'in', 'of']
11+
noun = ['bear', 'princess']
12+
stuff = stuff.split()
13+
for i in range(len(stuff)): # 如果w变量遍历整个列表,那么列表中元素是什么对象
14+
if stuff[i] in direction: # w也就对应的是该对象
15+
d = ('direction', stuff[i])
16+
result.append(d)
17+
#return result
18+
19+
elif stuff[i] in verb:
20+
v = ('verb', stuff[i])
21+
result.append(v)
22+
#return result
23+
24+
elif stuff[i] in stop:
25+
s = ('stop', stuff[i])
26+
result.append(s)
27+
#return result
28+
29+
elif stuff[i] in noun:
30+
n = ('noun', stuff[i])
31+
result.append(n)
32+
#return result
33+
34+
elif convert_number(stuff[i]): # 检测输入是否为数字
35+
num = ('number', int(stuff[i]))
36+
result.append(num)
37+
#return result
38+
39+
else:
40+
error = ('error', stuff[i])
41+
result.append(error)
42+
#return result
43+
return result # 最后返回result列表——因为有可能分割出多个word
44+
45+
def convert_number(s): # 如果s能转化为数字则为T,不能则为F
46+
try:
47+
return int(s)
48+
except ValueError:
49+
return None

ex48/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+

ex48/skeleton/tests/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from nose.tools import *
2+
from ex48 import lexicon
3+
4+
5+
def test_directions():
6+
assert_equal(lexicon.scan("north"), [('direction', 'north')])
7+
result = lexicon.scan("north east south")
8+
assert_equal(result, [('direction', 'north'),
9+
('direction', 'east'),
10+
('direction', 'south')])
11+
12+
def test_verbs():
13+
assert_equal(lexicon.scan("go"), [('verb', 'go')])
14+
result = lexicon.scan("go kill eat")
15+
assert_equal(result, [('verb', 'go'),
16+
('verb', 'kill'),
17+
('verb', 'eat')])
18+
19+
def test_stops():
20+
assert_equal(lexicon.scan("the"),[('stop', 'the')])
21+
result = lexicon.scan("the in of")
22+
assert_equal(result, [('stop', 'the'),
23+
('stop', 'in'),
24+
('stop', 'of')])
25+
26+
def test_nouns():
27+
assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
28+
result = lexicon.scan("bear princess")
29+
assert_equal(result, [('noun', 'bear'),
30+
('noun', 'princess')])
31+
32+
def test_numbers():
33+
assert_equal(lexicon.scan("1234"), [('number', 1234)])
34+
result = lexicon.scan("3 91234")
35+
assert_equal(result, [('number', 3),
36+
('number', 91234)])
37+
38+
def test_errors():
39+
assert_equal(lexicon.scan("ASFSDFASDF"),[('error','ASFSDFASDF')])
40+
result = lexicon.scan("bear IAS princess")
41+
assert_equal(result, [('noun', 'bear'),
42+
('error', 'IAS'),
43+
('noun', 'princess')])

0 commit comments

Comments
 (0)