RuleSyllabifier { public static int syllable(String word) { word = word.toLowerCase(); if (word.equals("w")) { return 2; } if (word.length() == 1) { return 1; } word = word.replaceAll("'", " "); if (word.endsWith("e")) { word = word.substring(0, word.length() - 1); } String[] phonems = VOWELS.split(word); int syl = 0; for (int i = 0; i < staticSubMatches.length; i++) { if (word.contains(staticSubMatches[i])) { syl -= 1; } } for (int i = 0; i < regexSubMatches.length; i++) { if (regexSubMatches[i].matcher(word).matches()) { syl -= 1; } } for (int i = 0; i < staticAddMatches.length; i++) { if (word.contains(staticAddMatches[i])) { syl += 1; } } for (int i = 0; i < regexAddMatches.length; i++) { if (regexAddMatches[i].matcher(word).matches()) { syl += 1; } } for (int i = 0; i < phonems.length; i++) { if (phonems[i].length() > 0) { syl++; } } if (syl == 0) { syl = 1; } return syl; }  static int syllable(String word);  }
@Test public void shouldCheckSomeWords() throws Exception { assertEquals(2, RuleSyllabifier.syllable("special")); }
@Test public void shouldMatchPerlTestCases() throws Exception { ImmutableMap<String, Integer> tests = ImmutableMap.<String, Integer>builder() .put("hoopty ", 2) .put("bah ", 1) .put("I ", 1) .put("A ", 1) .put("organism ", 4) .put("organisms ", 4) .put("antagonisms ", 5) .put("schisms ", 2) .put("monisms ", 3) .put("puritanisms ", 5) .put("criticisms ", 4) .put("microorganisms", 6) .put("surrealisms ", 4) .put("isms ", 2) .put("aphorisms ", 4) .put("prisms ", 2) .put("anachronisms ", 5) .put("dualisms ", 4) .put("euphemisms ", 4) .put("mechanisms ", 4) .put("mannerisms ", 4) .put("yogiisms ", 4) .put("metabolisms ", 5) .put("baptisms ", 3) .put("embolisms ", 4) .put("methodisms ", 4) .put("executed ", 4) .put("accused ", 2) .put("dosed ", 1) .build(); for (Map.Entry<String, Integer> entry : tests.entrySet()) { String word = entry.getKey().trim(); assertEquals(word + " bad", (int)entry.getValue(), RuleSyllabifier.syllable(word)); } }