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
- class noun, with subclasses:
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";
}
}