I’m on EX48 (Advanced User Input) and have made the lexicon.py and placed it in the appropriate position in the directory, but when I run the nosetests it shows this Error from lexicon_tests.py file:
(I’m on Windows 7, using Powershell,
And my Editor is VSC)
FAIL: lexicon_tests.test_stops
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\Users\pp\desktop\python\core\n\ex481\ex48\lexicon_tests.py", line 30, in test_stops
assert_equal(lexicon.scan("the"), [('stop', 'the')])
AssertionError: Lists differ: [('error', 'the')] != [('stop', 'the')]
First differing element 0:
('error', 'the')
('stop', 'the')
- [('error', 'the')]
? ^^^ ^
+ [('stop', 'the')]
? ^^ ^
----------------------------------------------------------------------
Ran 3 tests in 0.047s
FAILED (failures=1)
This is my lexicon.py file :-
lexicon = {
'north': 'direction',
'south': 'direction',
'east' : 'direction',
'west' : 'direction',
'go' : 'verb',
'kill': 'verb',
'eat' : 'verb',
'stop': 'verb',
'The': 'stop',
'of': 'stop',
'from':'stop',
'at':'stop',
'it':'stop',
'Door':'Noun',
'Bear':'Noun',
'princess':'Noun',
'cabinet':'Noun',
'123456789': 'number',
}
def convert_numbers(s):
try:
return int(s)
except ValueError:
return s
def scan(sentence):
words = sentence.split()
result = []
for word in words:
check_string = convert_numbers(word)
if word in lexicon:
check_number = convert_numbers(word)
pair = (lexicon[word], check_number)
result.append(pair)
elif type(check_string) ==type(1):
number = convert_numbers(word)
if number:
pair = ('number', number)
result.append(pair)
else:
error_word = word
pair = ('error', error_word)
result.append(pair)
return result
And this is the lexicon_tests.py file:
from nose.tools import *
from ex48 import lexicon
def test_directions():
assert_equal(lexicon.scan("north"), [('direction','north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
test_directions()
def test_verbs():
assert_equal(lexicon.scan("go"), [('verb', 'go')])
result = lexicon.scan("go kill eat")
assert_equal(result, [('verb', 'go'),
('verb', 'kill'),
('verb', 'eat')])
def test_stops():
assert_equal(lexicon.scan("the"), [('stop', 'the')])
result = lexicon.scan("the in of")
assert_equal(result, [('stop', 'the'),
('stop', 'in'),
('stop', 'of')])
def test_nouns():
assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
result = lexicon.scan("bear princess")
assert_equal(result, [('noun', 'bear'),
('noun', 'princess')])
def test_numbers():
assert_equal(lexicon.scan("1234"), [('number', 1234)])
result = lexicon.scan("3 91234")
assert_equal(result, [('number', 3),
('number', 91234)])
def test_errors():
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
result = lexicon.scan("bear IAS princess")
assert_equal(result, [('noun', 'bear'),
('error', 'IAS'),
('noun', 'princess')])
What Am I doing wrong?