/*
 * PigLatin class for Translator applet
 * $Id: PigLatin.java,v 1.2 1996/06/19 23:09:21 las Exp $
 *
 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101">the
 * CS101 homepage</a> or email <las@ai.mit.edu>.
 *
 * Copyright (C) 1996 Massachusetts Institute of Technology.
 * Please do not redistribute without obtaining permission.
 */

import cs101.util.StringUtils;

/**
 * PigLatin is an auxilliary class for the Translator applet which
 * actually handles the String transformation into Pig Latin.  It is a
 * while (true) {echo();} loop which runs in its own thread.<p>
 *
 * Note that the modularization of this applet is not what one might
 * expect from standard Java.  For more details, see the
 *      <a href="http://www.ai.mit.edu/projects/cs101/">
 *                    Rethinking CS101
 * </a> project of Lynn Andrea Stein's AP Group at the MIT
 * Artificial Intelligence Laboratory.<p>
 *
 * @see      Translator
 * @see      cs101.util.StringUtils
 *
 * @author   Emil Sit, sit@mit.edu
 * @author   Lynn Andrea Stein, las@ai.mit.edu
 * @author   Adrian Banard, inki@mit.edu
 * @version  $Id: PigLatin.java,v 1.2 1996/06/19 23:09:21 las Exp $
 *
 * Copyright 1996 Massachusetts Institute of Technology
 *
 */
public class PigLatin extends Thread {
    private Translator GUI;

   /**
    * PigLatin( Translator )
    *
    * This constructor takes a Translator GUI applet and builds a
    * PigLatin object which uses that GUI for IO.  The constructor
    * (a) stores the GUI for future reference and 
    * (b) starts the PigLatin thread running.
    *
    * @param    gui       Translator applet for IO.
    */  
    public PigLatin ( Translator gui ) {
        this.GUI = gui;
        this.start();
    }

   /**
    * run()
    *
    * An infinite loop which <br>
    * (a) gets a new word from the GUI, <br>
    * (b) possibly constructs a new version of it, and <br>
    * (c) asks the GUI to print it out.<p>
    *
    * For (b), the logic proceeds as follows: <br>
    * If it's a word, find the first vowel position. <br>
    * If it begins with a vowel, add "way". (E.g., appleway) <br>
    * If it's a word that doesn't begin with a vowel, put the first
    *    consonant cluster last and add "ay". (E.g., isThay) <br>
    * If the word was originally capitalized, produce capitalized
    *    output. <br>
    * If the word was originally uppercased, produce uppercased
    *    output. <br>
    * If it isn't a word, it's punctuation or whitespace -- just
    *    output it. <p>
    * 
    * Known "bugs": <br>
    * Doesn't handle apostrophes correctly. <br>
    * Thinks "I" is an all-caps word, so produces IWAY. <br>
    * What <i>should</i> it do with JavaCapitalizedWords? <p>
    * 
    * Uses Strings rather than StringBuffers to minimize conceptual
    * intricacies, pretending that garbage collection really <i>is</i>
    * magic. <p>
    *
    * Invoked by the constructor method of the PigLatin object.<p> 
    */  
    public void run () {

        while (true) {

            // Get new word.
            String nextWord = this.GUI.getNextWord();

            // If it's a word,
            if ( StringUtils.wordP( nextWord ) ) {
                // Get first vowel position. 
                int pos = StringUtils.firstVowelPos( nextWord );

                String ordWay;
                if (pos == 0) {             // begins with vowel
                    ordWay = ( nextWord + "way" );
                } else {                    // begins with consonant
                    if (StringUtils.capitalizedP(nextWord)) {
                        ordWay 
                          = ( StringUtils.capitalize(nextWord.substring(pos))
                              + StringUtils.unCapitalize(nextWord.substring(0,pos))
                              + "ay" );
                    } else {    // ! capitalizedP (nextWord) 
                        ordWay = ( nextWord.substring(pos)
                                            + nextWord.substring(0,pos)
                                            + "ay" );
                    }           // end if capitalizedP(nextWord) else
                }           // end if pos == 0 else, i.e., consonant
                if (StringUtils.allUpperCaseP (nextWord)) {
                    ordWay = StringUtils.capitalizeAll (ordWay);
                }
                this.GUI.printWord( ordWay );
            } else {   // not word at all -- just print it.
                this.GUI.printWord( nextWord );
            }          // end if wordP else
        }          // end while
    }

}


/*
 * Comments:
 *     Some code ideas borrowed from Java language tutorial and other
 *      java references.
 *
 * History:
 *     $Log: PigLatin.java,v $
 *     Revision 1.2  1996/06/19 23:09:21  las
 *     Cleaned up documentation.
 *
 *     Revision 1.1  1996/06/19 21:46:19  las
 *     Initial revision
 *     6-19-96  Documentation, modularity improved by las@ai.mit.edu
 *     6-18-96  "Final" version created by las@ai.mit.edu 
 *                  from PigLatin.java by sit@mit.edu
 *                       Translator.java,v 1.1 1996/06/08 19:30:11 sit Exp 
 *                       PigLatin.java,v 1.2 1996/06/08 19:26:16 sit Exp 
 *                  and IgpayAtinlay.java by inki@mit.edu
 *
 */

