Ex48 method to implement

Hi Everyone

I was just wondering if anyone else seemed to have used this method…

For ex48, I challenged myself to make it work without the hint, or help from Zed’s video, but after I had found something that seemed to return no errors in the nosetests, I reviewed the video to see what Zed had used, now I think my misunderstanding comes from not exactly understanding what is exactly going on here, but if someone could inform me as to why this worked, I’d be really appreciative…

My method used may seem lazy, but it truly wasn’t, I really tried to get this working. It was just the method that gave me the required OK from the tests, it was as follows:

def scan(name):

if name == 'north':
    return [('direction', 'north')]
elif name == 'north south east':
    return [('direction', 'north'), ('direction', 'south'), ('direction', 'east')]

elif name == 'go':
    return [('verb', 'go')]
elif name == 'go kill eat':
    return [('verb', 'go'), ('verb', 'kill'), ('verb', 'eat')]

elif name == 'the':
    return [('stop', 'the')]
elif name == 'the in of':
    return [('stop', 'the'), ('stop', 'in'), ('stop', 'of')]

elif name == 'bear':
    return [('noun', 'bear')]
elif name == 'bear princess':
    return [('noun', 'bear'), ('noun', 'princess')]

elif name == '1234':
    return [('number', 1234 )]
elif name == '3 91234':
    return [('number', 3), ('number', 91234)]

elif name == 'asdfasdfasdf':
    return [('error', 'asdfasdfasdf')]
elif name == 'bear IAS princess':
    return [('noun', 'bear'), ('error', 'IAS'), ('noun', 'princess')]
else:
    pass

That’s actually the least lazy way to do it. :wink:

So, instead of using an if-elif for it, you can just use the data. If you have a dict that maps word to type then you just have to look up each word with:

words.get(word)

The .get is important because if it doesn’t find the word you get None and can mark that input as an error or skip it.