Please refer to LearningPythonTheHardWay exercise48 for more details on the purpose of these code. I am still new, I know there are a lot to be improved or better ways to do them.
The main issue I have encounter here is that the result from the nosetest, the codes is attached below after lexicon.py.
In short, I had 1 error and 5 failures.
Error:
NameError: global name 'ASDFADFASDF' is not defined
Failure(s):
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]
Where the results was None instead of the answer at the right hand side. For the error I had a similar result from my codes except compares to the test_error, where I had
[('error', 'ASDFADFASDF')]
the test result is
[('error', ASDFADFASDF)]
As for the failure the typical failure message I got is:
File "C:\xxx\xxxxxx\learningpython\projectex48\skeleton\tests\lexicon_tests.py", line 13, in test_verb
assert_equal(lexicon.scan("go"),[('verb', 'go')])
AssertionError: None != [('verb', 'go')]
-------------------- >> begin captured stdout << ---------------------
[('verb', 'go')]
--------------------- >> end captured stdout << ----------------------
I don't know why it only run for 1 element, example "go kill eat" python only runs "go". I am still trying to figure out the logic and how to fix it. Can any one please help me to identify problem of why I failed the 5 test(s) and had 1 error.
I have spend weeks to code my own version of scan function for this exercise, this is the coding of lexicon.py:
direction = [('direction', 'north'),
('direction', 'south'),
('direction', 'east'),
('direction', 'west'),
('direction', 'up'),
('direction', 'down'),
('direction', 'left'),
('direction', 'right'),
('direction', 'back')
]
verbs = [('verb', 'go'),
('verb', 'stop'),
('verb', 'kill'),
('verb', 'eat')
]
stop_words = [('stop', 'the'),
('stop', 'in'),
('stop', 'of'),
('stop', 'from'),
('stop', 'at'),
('stop', 'it')
]
nouns = [('noun', 'door'),
('noun', 'bear'),
('noun', 'princess'),
('noun', 'cabinet')
]
library = tuple(nouns + stop_words + verbs + direction)
def convert_number(x):
try:
return int(x)
except ValueError:
return None
def scan(input):
element = input.split()
data = library
i = 0
z = 0
output = []
temp = True
while (i == 0) or (not (element[(i-1)] == element[-1])):
try:
j = 0
while (j == 0) or (not data[(j-1)][1] == data [-1][1]):
matching = data[j][1]
if (matching == element[i]):
output.append(data[j])
j += 1
z += 1
temp = False
else:
while (data[j][1] == data [-1][1]) and (temp == True):
convert = convert_number(element[i])
a = tuple(['number', convert])
b = tuple(['error', element[i]])
if convert == a[1] and not(convert == None):
output.append(a)
temp = False
else:
output.append(b)
temp = False
j += 1
i += 1
temp = True
except ValueError:
return None
else:
pass
print output
The above was a simplified version that I used to put into test, the concept is to search and gather/add the data corresponding to the input and save and append into the output as tuple, then finally print them out all together.
Below is the lexicon_tests.py codes which I directly copy from the book:
from nose.tools import *
from ex48 import lexicon
def test_direction():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
def test_verb():
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)]) #refer to Update#1
result = lexicon.scan("bear IAS princess")
assert_equal(result, [('noun', 'bear'),
('error', 'IAS'),
('noun', 'princess')])
This is where I saved the files in:
$ mkdir projectsex48
$ cd projectsex48/
$ mkdir skeleton
$ cd skeleton
$ mkdir bin
$ mkdir ex48 #(lexicon.py)
$ mkdir tests #(lexicon_tests.py)
$ mkdir doc
**Update #1:**For the Error
NameError: global name 'ASDFADFASDF' is not defined
It was because bracket '' was missing from lexicon_tests.py for ASDFADFASDF as below:
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
Now I have 6 failure where the function returns None.
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]
AssertionError: None != [('error', 'ASDFADFASDF')]
Yes!! I have finally solved it.
I made a mistake by ending the scan function with
print output
I corrected it with
return output
So the final corrected code is:
direction = [('direction', 'north'),
('direction', 'south'),
('direction', 'east'),
('direction', 'west'),
('direction', 'down'),
('direction', 'left'),
('direction', 'right'),
('direction', 'back')
]
verbs = [('verb', 'go'),
('verb', 'stop'),
('verb', 'kill'),
('verb', 'eat')
]
stop_words = [('stop', 'the'),
('stop', 'in'),
('stop', 'of'),
('stop', 'from'),
('stop', 'at'),
('stop', 'it')
]
nouns = [('noun', 'door'),
('noun', 'bear'),
('noun', 'princess'),
('noun', 'cabinet')
]
library = tuple(nouns + stop_words + verbs + direction)
def convert_number(x):
try:
return int(x)
except ValueError:
return None
def scan(input):
#include uppercase input for searching. (Study Drills no.3)
lowercase = input.lower()
#element is what i want to search.
element = lowercase.split()
#orielement is the original input which have uppercase, for 'error' type
orielement = input.split()
#library is tuple of the word types from above. You can replace with your data source.
data = library
#i is used to evaluate the position of element
i = 0
#z is used to indicate the position of output, which is the data that match what i search, equals to "i".
z = 0
#create a place to store my output.
output = []
#temp is just a on/off switch. Turn off the switch when i get any match for that particular input.
temp = True
#creating a condition which evaluates the total search needed to be done and follows the sequence by +1.
while not(i == len(element)):
try:
#j is used to position the word in the library, eg 'door', 'bear', 'go', etc which exclude the word type.
j = 0
while not (j == len(data)):
#data[j][1] all the single word in library
matching = data[j][1]
#when the word match, it will save the match into the output.
if (matching == element[i]):
output.append(data[j])
#print output[z]
j += 1
z += 1
#to switch off the search for else: below and go to next input search. Otherwise they would be considerd 'error'
temp = False
#else is everything that is not in the library.
else:
while (data[j][1] == data [-1][1]) and (temp == True):
#refer to convert_number, to test if the input is a number, here i use orielement which includes uppercase
convert = convert_number(orielement[i])
#a is used to save number only.
a = tuple(['number', convert])
#b is to save everything
b = tuple(['error', orielement[i]])
#conver is number a[1] is the access the number inside, if it returns None from number then it wont append.
if convert == a[1] and not(convert == None):
output.append(a)
temp = False
else:
output.append(b)
#keep the switch off to escape the while loop!
temp = False
#searching in next data
j += 1
#next word of input
i += 1
temp = True
except ValueError:
return output
else:
pass
return output
output is a tuple that I used to store the searching results.