lexicon = {
'north': 'direction',
'south': 'direction',
'west': 'direction',
'east' : 'direction',
'go' : 'verb',
'kill': 'verb',
'eat': 'verb',
'take': 'verb',
'the': 'stop',
'in': 'stop',
'of': 'stop',
'bear': 'noun',
'princess': 'noun',
}
def number_check(number):
try:
int(number)
return True
except ValueError:
return False
def scan(sentence):
result = []
sentence_lowercase = sentence.lower()
words = sentence_lowercase.split()
for word in words:
word_type = lexicon.get(word)
print(word_type)
if word_type == None and number_check(word):
result.append(('number',int(word)))
elif word_type is not None:
result.append((word_type,word))
else:
result.append(('error',word))
return result
So this is my solution for ex48. I had to do a work around for the final test . In the book :
Itâs written as :
def test_errors():
assert_equal(lexicon.scan(âASDFADFASDFâ),
[(âerrorâ, âASDFADFASDFâ)])
But I had to change the test to :
def test_errors():
assert_equal(lexicon.scan(âASDFADFASDFâ),
[(âerrorâ, âasdfadfasdfâ)])
Did I do this right ? My thinking is since all the input will be changed to lowercase anyway, thereâs no way for the error tuple to be the original .