views:

154

answers:

3

I am creating a program that will fill in a given grammar. Right now I am modeling the "kinds of words" like this:

public class WordDescriptor {

    public static final String noun = "N";
    public static final String plural = "p";
    public static final String nounPhrase = "h";
    public static final String usuParticipleVerb = "V";
    public static final String transitiveVerb = "t";
    public static final String intransitiveVerb = "i";
    public static final String adjective = "A";
    public static final String adverb = "v";
    public static final String conjunction = "C";
    public static final String preposition = "P";
    public static final String interjection = "!";
    public static final String pronoun = "r";
    public static final String definiteArticle = "D";
    public static final String indefiniteArticle = "I";
    public static final String nominative = "o";
    public static final String defaultTag = "?";

    private String word; // where word is one of the constants defined above.  

    public String getWord(){
        return word;
    }

    public String setWord(){
        return word;
    }

    /** For debugging only.  
     * 
     * @param s
     * @return
     */
    protected static String getKind(String s){
        if(s.equals(noun)){
            return "noun";
        }else if(s.equals(plural)){
            return "plural";
        }else if(s.equals(nounPhrase)){
            return "nounPhrase";
        }else if(s.equals(usuParticipleVerb)){
            return "usuParticipleVerb";
        }else if(s.equals(transitiveVerb)){
            return "transitiveVerb";
        }else if(s.equals(intransitiveVerb)){
            return "intransitiveVerb";
        }else if(s.equals(adjective)){
            return "adjective";
        }else if(s.equals(adverb)){
            return "adverb";
        }else if(s.equals(conjunction)){
            return "conjunction";
        }else if(s.equals(preposition)){
            return "preposition";
        }else if(s.equals(interjection)){
            return "interjection";
        }else if(s.equals(pronoun)){
            return "pronoun";
        }else if(s.equals(definiteArticle)){
            return "definiteArticle";
        }else if(s.equals(indefiniteArticle)){
            return "indefiniteArticle";
        }else if(s.equals(nominative)){
            return "nominative";
        } else if(s.equals(defaultTag)){
            return "defaultTag";
        }else{
            return "other: "+s;
        }
    }

}

This is pretty much the most ugly code that I can imagine. I know I can make it a bit better by using a case statement however that is still friggin ugly. Here is my question:

How can I make it beautiful? I was thinking about making:

  • class WordDescriptor, with subclasses:
    • class noun, with subclasses:
      • singular
      • plural
    • class verb
    • class adverb

But I am not sure if this seems like a great idea either. How can I this out better?


Edit: If I did take the second approach I am not even sure what the classes would look like. Here is an example:

public abstract class WordDescriptor {

   public String toString();

}

public class Noun extends WordDescriptor {

    public String toString(){
        return "Noun";
    }
}

public class Plural extends Noun{
    public String toString(){
        return "Plural";
    }
}
+12  A: 

I would just create a dictionary or map from abbreviation to full description. Then getKind would look up its input in the mapping and return the result, or other if the input doesn't map to anything.

Gabe
I think that this is a good idea HOWEVER, it does not address that there are some words that have subcategories. Ie: a noun can be singular or plural.
sixtyfootersdude
@sixtyfootersdude Nor does your original solution. `getKind(String s)` function, s is simply compared to each of the constants. Did you intend to post the code to retrieve `s` before it is passed to `getKind()`?
Chadwick
In order to support multiple subcategories, you could just use a list of descriptors (or a string: "Npo" would describe a plural nominative noun). Or you could create a bunch of subcategories (like Case, Gender, Tense, Person, Declension, etc.) and assign them to a `List<Subcategory>` that's part of each word.
Gabe
+2  A: 

Could you use a dictionary with the code (s in your case) as the key and the returned string as the value?

Chris Diver
simplest solution, is nearly always the best
Gareth Davis
+6  A: 

You could do something with an enumerated type.

public enum SpeechPart
{
    NOUN ("noun"),
    PLURAL ("plural"),
    NOUNPHRASE ("noun phrase"),
    ADVERB ("adverb"),
    ADJECTIVE ("adjective"),
    CONJUNCTION ("conjunction"),
    VERB ("verb");

    private String english;

    SpeechPart(String inEnglish)
    {
        this.english = inEnglish;
    }

    public String toString() { return english; }
}

You can now assign these to a variable.

SpeechPart dog = SpeechPart.NOUN;
SpeechPart ran = SpeechPart.VERB;
SpeechPart quickly = SpeechPart.ADVERB;

And then you can see what their parts of speech are:

System.out.println(dog.toString());
System.out.println(quickly);        // Implicit call to toString()

This solution assumes only a single part of speech per word. To allow for modifiers, such as "first person," "third person," "plural," "present," "progressive," etc., you could simply enumerate all of them -- a tedious job but need be done only once. Alternatively, you could adapt the Decorator Pattern, which addresses specifically the need to add attributes to an object dynamically.

Another suggestion is enumerate the modifiers:

public enum SpeechModifier
{
    SINGULAR,
    PLURAL,
    FIRST_PERSON,
    SECOND_PERSON,
    THIRD_PERSON,
    PRESENT,
    PAST,
    PERFECT,
    PROGRESSIVE;
}

Then build a class that combines them together:

public class Word
{
    String word;
    SpeechPart part;
    EnumSet<SpeechModifier> modifiers;
}

Now you can model a whole word:

Word w1 = new Word();
w1.word = "bouncing";
w1.part = SpeechPart.VERB;
w1.modifiers = EnumSet<SpeechModifier>.of(SpeechModifier.PRESENT, SpeechModifier.PROGRESSIVE);

This solution, however, doesn't prevent non-sensical combinations, such as FIRST_PERSON NOUN PAST.

Barry Brown
I think that this is a good idea HOWEVER, it does not address that there are some words that have subcategories. Ie: a noun can be singular or plural.
sixtyfootersdude
Your original solution doesn't address that, either. I'll add some notes to point you in some appropriate directions.
Barry Brown