University of Arizona, Department of Computer Science

CSc 120: Rhyming Words (Object-oriented)

This program is behaviorally identical to the rhyming words program from Assignment 3, except for two differences: first, the program has to detect and deal with some errors (see Errors below); and second, the program has to be implemented in an object-oriented style, i.e., using objects and classes.

Definitions

As in assignment 3, we will compute perfect rhymes.

Expected Behavior

As in assignment 3. Your program should be in a file rhymes-oo.py.

Errors

Note: The specification was changed to add more detail to error messages. See Piazza post @348, which describes the following errors:

  1. The pronunciation dictionary cannot be read. (Use a try statement to detect this.)

    Program behavior: Give an error message and quit.

    Error message: "ERROR: Could not open file " + filename

  2. Some word in the pronunciation dictionary has a pronunciation with more than one primary stress.

    Program behavior: Give an error message, discard the offending pronunciation, and continue processing.

  3. The input word (for which the program is to find rhymes) is not in the pronunciation dictionary. (Use an assert statement to detect this.)

    Program behavior: Give an error message and quit.

    Error message: "ERROR: the word input by the user is not in the pronuciation dictionary " + word

Input format

As in assignment 3.

Output format

As in assignment 3.

Programming Requirements

Your program should implement the following classes:

class Word
An instance of this class represents a word.

Attributes: These should include:

  • the string that makes up the word;
  • a collection (e.g., list, set, or dictionary) of pronunciations for the word;

Methods: These should include:

  • methods to initialize, access, and (where appropriate) update the attributes specified above;
  • methods defined such that, given two Word objects w1 and w2, the expression
    w1 == w2
    is True if and only if w1 and w2 form a perfect rhyme.
  • __init__() and __str__() methods; and, optionally, a __repr__() method.
class WordMap
An instance of this class represents data structures and methods to associate words (strings) with the corresponding Word objects.

Attributes: These should include:

  • A dictionary that takes strings to the corresponding Word objects.

Methods: These should include:

  • a method to read in a pronunciation dictionary and use this to set the mapping from strings to Word objects;
  • a method to read in a word (string) and print out the words that rhyme with it.
  • __init__() and __str__() methods; and, optionally, a __repr__() method.

Examples

As in assignment 3.