Organizing Java Programs: How Can I Get Rid of This Mass Case Report?

I am creating a program that fills this grammar. Right now I am modeling the β€œkinds of words” as follows:

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 almost the ugliest code I can imagine. I know I can do it a little better using the case argument, but it is still terribly ugly. Here is my question:

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

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

But I'm not sure if this also sounds like a great idea. How can I do it better?


Edit: if I did the second approach, I'm not even sure what the classes 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";
    }
}
+3
3

- .

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; }
}

.

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

, :

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

. , " ", " ", " ", "", "" .., - , , , Decorator Pattern, .

- :

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

, :

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

:

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

, , , FIRST_PERSON NOUN PAST.

+6

. getKind , other, .

+12

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

+2
source

Source: https://habr.com/ru/post/1754044/


All Articles