Tuesday, October 4, 2011

LPTHW - Exercise 49

In the previous exercise, we had created a scanner, which would take a line of words as input and return to us a list of tuples which gave us the type of each word.

In this exercise, we try and add some intelligence to our game, and identify sentence parts in the user input.

This is a bit of an over-simplification, but we will consider a sentence to consist of the following structure: subject verb object

If our sentence contains words from our lexicon and is of the above form, then we will be able to parse the sentence and take an action in the game based on it. We ignore stop words, and also allow for arbitrary upper/lower case. This gives the player a decent amount of freedom in creating their commands. So for example they could enter "eat bear", or they could also enter "Eat the bear" and both the inputs will be accepted as valid by the sentence parser.

For this exercise I copied the code from ex48 because we still need the lexicon and lexicon_tests files. In addition I added the files sentence.py and sentence_tests.py

The entire code is embedded below.

No comments:

Post a Comment