将复合词变为复数

时间:2016-10-25 19:09:01

标签: java

我有这个程序,可以使一些单数名词复数(我知道我失踪了很多,但这是不重要的)。当我输入诸如“战争之人”这样的词时,它将返回“战争之战”,而不是“战争之人”。我该如何解决?我的代码已经可以让男人对男人,只是不是我提到的情况。同样对于这个程序,每个复合词都有破折号,它只是用于练习。

public class LanguageUtils {

static boolean checkException(String noun){ 
    String[] exceptions = {"fish", "fox", "deer", "moose", "sheep", "cattle","pants","scissors"};
    for(int i=0;i<exceptions.length;i++) {
        if(exceptions[i].equals(noun))
            return true;
    }  
    return false;       
}

static boolean EnglishConsonant(char ch) {
    switch (Character.toLowerCase(ch)) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
            return false;
        default:
            return true;
    }
}

static String makePlural (String noun){
    String pluralWord = "";
    int length = noun.length();
    String strippedWord = noun.substring(0, noun.length()-1);
    char lastLetter = noun.charAt(noun.length()-1);

    if(noun.contains("-")){
        String nounsaver = noun.substring(noun.indexOf('-'), noun.length());
        pluralWord = noun.substring(0,noun.indexOf('-')) + "s" + nounsaver;
    }
    else{
    switch (lastLetter){
        case 's':
        case 'x':
        case 'z':
            if(noun.equals("ox")){
                 pluralWord = noun + "en";
                 break;
            }
            else{
                pluralWord = noun + "es";
                break;
            }
        case 'o':
            if(EnglishConsonant(noun.charAt(noun.length()-2))){
                pluralWord = strippedWord + "oes";
                break;
            }
        case 'e':
            char f = noun.charAt(noun.length()-2);
            String prec = noun.substring(0, noun.length()-2);
            if(f == 'f'){
                pluralWord = prec + "ves";
                break;
            }
            if(noun.equals("goose")){
                pluralWord = "geese";
                break;
            }
            else{
                pluralWord = noun + "s";
                break;
            }
        case 'h': 
            if ((noun.charAt(noun.length()-2)== 'c') || (noun.charAt(noun.length()-2)== 's')) {
                pluralWord = noun + "es";
                break;
            }
        case 'f':
            if (EnglishConsonant(noun.charAt(noun.length()-2))) {
                pluralWord = strippedWord + "ves";
                break;
            }
        case 'y':
            if (EnglishConsonant(noun.charAt(noun.length()-2))) {
                pluralWord = strippedWord + "ies";
                break;
            }  
        default:    
            if(noun.equals("foot")){
                pluralWord = "feet";
                break;
            }
            if(noun.endsWith("man")){
                pluralWord = noun.substring(0, noun.length()-3)+"men";
                break;
            }
            else{
                pluralWord = noun + "s";
                break;
            }
    }
    }
    if (length == 1){
        pluralWord = noun + "'s";
    }
    if(checkException(noun)){
        pluralWord = noun;
    }
    return pluralWord;
}

}

3 个答案:

答案 0 :(得分:0)

在处理复合词时,如果需要复数一个部分,请调用一个旨在实现这一目的的函数:makePlural

答案 1 :(得分:0)

如果你有 - 那么打破单词并再次为这些单词调用makePlural。

答案 2 :(得分:0)

您可以使用String#replace

Tue Oct 25 22:43:36 
real    0m13.275s
user    0m0.004s
sys     0m0.008s