将单词分解为音节

时间:2012-04-03 09:05:16

标签: java

您好我正在尝试编写一个程序,使用罗马尼亚规则将单词分成音节。想要这样做比我想的要困难得多,因为我不知道如何创建这个规则。这就是我所拥有的我知道代码在这种情况下是不正确的,但是我需要检查汽车数组中的字母是否存在于vocale数组中,然后检查汽车数组中的下一个字母是否存在于consoane数组中,所以这里的一个是代码我知道if条件输入错误我只需要一个解决方案来检查这个条件:

String[] vocale = {"a", "e", "i", "o", "u"};
String[] consoane = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
public String DesparteCuvant(String cuv){        
    String[] car = new String[cuv.length()];
    String c = "";

    for(int i = 0; i < cuv.length(); i++){
        car[i] = cuv.substring(i, i+1);

    }


    for (int j = 0; j < car.length - 2; j++){
       if(car[j] == vocale[] && car[j+1] == consoane[] && car[j+2] == vocale[]){

       }

    }            

    return c;
}

2 个答案:

答案 0 :(得分:1)

好的,如果您只想让if语句正常工作,那就是我会这样做的:

private final Set<String> vocale = new HashSet<String>();
private final Set<String> consoane  = new HashSet<String>();

private init(){
    // fill the sets with your strings
}

private boolean isVocale(String s){
    return vocale.contains( s );
}

private boolean isConsoane(String s){
    return consoane.contains( s );
}

你的if语句看起来像这样:

 if(isVocale(car[j]) &&  isConsoane( car[j+1] ) && isVocale( car[j+2] ) ){
      // do your stuff
 }

答案 1 :(得分:1)

我认为您可以使用regular expressions执行此任务。它们可以让您更轻松地写下这些模式。

例如

// following is the enough to specify a "complex" pattern
Pattern rule1 = Pattern.compile("([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])"); 

Matcher matcher= rule1.matcher(strLine);
while (matcher.find()){
  String aMatch= matcher.group(1);
  // do what you need to do
}

将取代您的

for (int j = 0; j < car.length - 2; j++){
   if(car[j] == vocale[] && car[j+1] == consoane[] && car[j+2] == vocale[]){
   }

}