Lab 6 – Classes – Sentence Game

Codeboard Project

Goal

For this lab we will be using classes to build a game based on the presidents’ inaugural speeches. The game will present a sentence pulled from a speech, and the names of three presidents. The user is to try to guess which president spoke that sentence. We will be using classes to represent both a single speech, as well as a set of speeches.

Design

For this project, I will provide a design that everyone is expected to follow. The program will include a Speech class that stores the text of a speech (represented as a list of sentences) and the name of the president. Speech files are located in the “presidents” folder within the project. The Speech class will include methods to load a speech (__init__) and to select a random sentence from the speech (randomSent).

Additionally, the program will have a Speeches container class to store all the speeches as a list of Speech objects. The Speeches class constructor will accept a list of speech file names and iterate over each file, creating a Speech object for each one (by calling the Speech constructor with each filename). It will also contain a method, randomSpeech, which selects a random Speech.
Below are the classes design:

The disgram below shows how the Speeches class consists of a list of Speech objects.

Game Play

Below is an example of the game played:

60 speeches loaded
60 Loaded
This is a game to guess which inaugural address a sentence comes from.
The old imperialism-exploitation for foreign profit-has no place in our plans.
Is this from:
1. Jimmy Carter
2. Richard M Nixon 2
3. Harry S Truman
Who do you think said it?1
Sorry, that was said by: Harry S Truman
Go again? (y or n)y
In reference to the Army and Navy, lately employed with so much distinction on active service, care shall be taken to insure the highest condition of efficiency, and in furtherance of that object the military and naval schools, sustained by the liberality of Congress, shall receive the special attention of the Executive.
Is this from:
1. Grover Cleveland 2
2. Zachary Taylor
3. Woodrow Wilson 1
Who do you think said it?2
You got it!
Go again? (y or n)y
His name may be still a rampart, and the knowledge that he lives a bulwark, against all open or secret enemies of his country's peace.
Is this from:
1. Thomas Jefferson 1
2. Warren G Harding
3. John Adams
Who do you think said it?3
You got it!
Go again? (y or n)n

Project code

Below is the starting code for the project. You are to finish this program using this design. Finish and test the objects, then finish the functions in the main program. Then get everything running.

Project

Steps

Step 1 – Finish the Speech class

The Speech class constructor __init__(self,text) is passed a list of lines from a speech where each line is a full paragraph. The constructor with get the first line and stor that in the president variable, and then parse the remaining lines into sentences (looking for periods to separate sentinces out from each line), and storing the sentences in the sentences variable.

You should write a function readSpeech(filename) which reads a speech and splits it into lines.

line=readSpeech(“https://cs.jimskon.com/text/president/07.JamesMadison2.txt”)
aSpeech=Speech(lines) # Create a speech.

The last line above will call the __init__ constructor method in the Speech class. This method needs to read the lines of text (each of which is a paragraph) and break it up into sentences and store the sentences in a list of strings in the class. This is a little tricky, but can be done with the find function if we iterate through the string. This code looks for the periods, and breaks of the string. You could also use split().

s="jim skon. kelly decastro. kenyon college."

p=s.find(".")
while p>=0:
  print(s[0:p+1])
  s=s[p+1:]
  i=input()
  p=s.find(".")

Try it! Then finish the Speech class constructor. It should first read the first line of the speech, which contains the president’s name, and put it in the object’s president variable. It should then go through all the lines in the speech, break each line into sentences, and store the sentences in the sentences list.

Now we need to write the class method randomSent(self). This should pick a random sentence from the speech. This can be done by finding the length N of the senteces list, and then using random.randint() to pick a random number in the range of the number of sentences. It should then return that sentence.

You should fully test this class before moving on.

Step 2 – Finish the Speeches Class

This class will store a list of Speech objects from above. The class constructor __init__(self,speechList,webSiteURL) is passed a list of the speech files (which you can read from speeches.txt), and the url of the website containing the speeches. You will need to iterate through the list of speech names, and use readSpeech(filename) to read in the speech, and then pass the speech text to the speech constructor to create a Speech object for that speech. This Speech object will then be appended to the list speeches.

This constructor may take some time to execute – you should put some print statements in the loop to see if it is working.

Next you need to write the randomSpeech() method. This picks a random Speech.

Step 3 – Write the mainline

Here we wrap it up and write the game. The game loop is already written. For each time through, the game will to call randomSent() on the Speeches object to get 3 sentences, and the three corresponding presidents names. It should put these three into two small lists, one for the sentences and one for the names, and then pick one randomly to show the sentence for. Then show the three names, and ask which one the player things said the sentence.

Turn in

Turn in the following on Moodle:

  1. A link to your final codeboard.io
  2. A text file with the output of playing the game 5 times before quitting. I suggest copy and pasting the output into a word or text doc and turning in the document.

Grading

RequirementsGrading CommentsPointsScore
Fully functioning code that works exactly like as described40
Completion of all functions and methods in the start code30
Correct use of classes20
Fully commented code, with your name and date at the top.10
Total100
Scroll to Top