HW J6: Text Translation
Assigned 22 November 2004
Due 9:00 a.m. on 10 December 2004
(PDF Version)

 

Introduction

The purpose of this assignment is to gain familiarity with the Java array construct and exposure to its GUI capabilities. You will do so by completing the design and implementation of classes that support a simple translation algorithm from one language to another. For your information there are many free useful language translators on the web. One of the most popular is babelfish by AltaVista

You will need to submit one file for this assignment: TranslationDictionary.java.  You will also need to use the Entry.class and the TranslatorGUI.class. The source to TranslatorGUI.java is available for examination. The source to Entry.java is unavailable -- knowing how it was implemented might be helpful for the future. Sample query text is available and a sample (on-authoritative Spanish to English dictionary is also available.

Background

The translator program that you are to develop is a relatively simple one that has no intelligence regarding any particular pair of languages. It will translate the text word by word. The text will contain no punctuation and capitalization will matter. How to map a word from the from language to the to language will be specified by a translation dictionary file.

The first line in the translation dictionary file specifies the number of from-to mappings in the file. For example suppose the contents of the file are
        4
       ist is
       unser our
       Dana Dana
       Freund friend
then the following line
        Dana ist unser Freund
translates to
        Dana is our friend
As another example, suppose the contents translation dictionary file are
        5
        son are
        felices happy
        y and
        Dana Dana
        Alex Alex

then the following line
        Alex y Dana son felices
translates to
        Alex and Dana are happy

As a third example, suppose the contents translation dictionary file are
        4
       
欢迎    Welcome
       
感恩    Thanksgiving
       
愉快    Happy
       
大家    Everybody
then the following line
        欢迎  大家 愉快 感恩   
translates to
        Welcome Everybody Happy Thanksgiving

A translation dictionary representation

The principle attribute of a dictionary representation is its list of word mappings. We will represent an individual word mapping with an Entry. The word list will be an array of Entry.

Class Entry has the following methods.

The class TranslationDictionary whose implementation you are to complete is to have the following methods.

Examples

A sample use of class TranslationDictionary is shown in the following method main()

    public static void main(String[] args) {
        TranslationDictionary dictionary = new TranslationDictionary("sTOe.txt");

         String spanish = "querido estudiantes\n" + "nosotros esperamos que "
            + "usted puede traducir una oración\n" + "o dos con un Java "
            + "computadora programa\n" + "el zorro marrón rápido saltó "
            + "sobre el perro perezoso\n" + "cordialmente\n"
            + "sus profesores\n";

        System.out.println( dictionary.translateText(spanish) );
    }

In producing its output the method makes use of translation dictionary file sTOe.txt.
    dear students
    we hope that/what you can translate a sentence
    or two with a Java computer program
    the fox brown quick jumped over the dog lazy
    cordially
    your professors

Class TranslationDictionary is also used by GUI-based program TranslatorGUI.java, which also uses file sTOe.txt as the basis for its dictionary. A snapshot of the GUI for the program follows.

 

Your task

Hints

// translateText(): translates lines of words using the dictionary
public String translateText(String text) {
    Scanner instream = new Scanner(text);

    String translation = "";

    while ( instream.hasNext() ) {
        String currentLine = instream.nextLine();

        currentLine = currentLine.trim();

        if (currentLine.length() != 0) {
            String result = this.translateLine(currentLine);
            translation = translation + result + "\n";
        }
        else {
            translation = translation + "\n";
        }
    }

    return translation;
}