Computer Science 2311
Fall 2007
Programming Assignment 5

Date assigned: October 1, 2007


For this assignment, modify your solution to part 2 of assignment 4 as follows.

Your current program has the translations built into it. For example, it says to translate "is" to "be" One general principal that software designers use is that programs should have as little information built into them as possible. Instead, they should be flexible enough that they can read information from files. To modify the behavior of the software, you only need to modify the files, not the software.

That is how computer game manufacturers are able to create new games quickly. They write general software that reads information about characters, such as what the characters look like and how they move, from files. They read about backgrounds and how different rooms or scenes are connected together. To make a new game, you just create files that describe new characters and backgrounds.

What to do

Modify your program that so that the translations are written in a file called pirate.txt. The file should consist of some number of lines, where each line has a regular word followed by a corresponding pirate word. For example, the pirate.txt file might look as follows, to request the same translations as before.

    is  be
    Is  Be
    are be
    Are Be

Your program cannot assume that it knows how many lines are in the pirate.txt file. You must be able to add new lines without modifying your program at all.

Hints

  1. To keep things simple here, you can use two library functions, wsExplode and breakIntoLines.

    1. Function wsExplode takes a string and breaks it into a list of words. For example, wsExplode("is be") = ["is", "be"].

    2. Function breakIntoLines takes a string that contains line breaks and cuts it into a list of lines. The line break character is '\n'. For example, breakIntoLines("is be\nIs Be\nare be") = ["is be", "Is Be", "are be"].

  2. As a warmup for the next part, write a function that takes a list of numbers and produces a list of the squares of those numbers. For example, if the function is called allSquares, then allSquares([2, 5, 3, 7]) = [4, 25, 9, 49]. You will not include allSquares in your program, but writing it will help you see how to do the same thing to each thing in a list.

  3. Write a function that takes a list of lines, such as ["is be", "Is Be", "are be"], as an argument, and yields the result of converting each string into a list of words. For example, if this function is called splitLines, then splitLines(["is be", "Is Be", "are be"]) = [["is", "be"], ["Is", "Be"], ["are", "be"]]. Notice that you need to use wsExplode on each string in the list. Include a contract and at least two examples of your function. Test the examples before moving on.

  4. Write a function that takes a file name, such as "pirate.txt", and produces the list of lists that indicates the translations found in that file. Use infile to get the contents of the file, breakIntoLines to cut the file into separate lines, and your function splitLines from the preceding part to do the job. Include a contract and at least two examples of your function. Test the examples before moving on.

  5. Write a function that takes two arguments, a word and a list of translations. It should yield the appropriate translation of the word (or just the word itself if that word should not be changed). For example, if your function is called translation, then translation("is", [["is", "be"], ["Is", "Be"], ["are", "be"]]) = "be" and translation("fox", [["is", "be"], ["Is", "Be"], ["are", "be"]]) = "fox". Just look through the list of replacements, to see whether any of them hold this word as their first members. Include a contract and at least two examples of your function. Test the examples before moving on.

  6. Write your program so that it repeatedly looks at characters in the text that it is supposed to translate, and does the following with each character that it sees.

    1. If the character is not a letter, then just display it and move to the next character.

    2. If the character is a letter, then get an entire word. Display the translation of that word, and continue with what comes after the word.

    You can make this simple using the following library functions. (Import "lang/char".)
    1. Function letter? yields true if its argument is a letter, and false if not. For example, letter?('a') = true, but letter?('@') = false.

    2. Function getString yields an ordered pair consisting of the first word and everything after the first word. For example, getString("Man the ramparts") = ("Man", " the ramparts"). If your string is called text, then

        Let p: (String, String) = getString(text).
        Let word: String = left(p).
        Let rest: String = right(p).
      
      will make word be the first word in text, and rest be all of text that comes after the first word.