AllophoneSet { public String syllabify(String phoneString) throws IllegalArgumentException { if (phoneString.trim().isEmpty()) { throw new IllegalArgumentException("Cannot syllabify empty phone string"); } List<String> allophoneStrings = splitIntoAllophoneList(phoneString, true); List<Object> phonesAndSyllables = new ArrayList<Object>(allophoneStrings); ListIterator<Object> iterator = phonesAndSyllables.listIterator(); Syllable currentSyllable = null; while (iterator.hasNext()) { String phone = (String) iterator.next(); try { Allophone allophone = getAllophone(phone); if (allophone.isSyllabic()) { boolean appendR = false; if (allophone.getFeature("ctype").equals("r")) { if (iterator.previousIndex() > 1) { Object previousPhoneOrSyllable = phonesAndSyllables.get(iterator.previousIndex() - 1); if (previousPhoneOrSyllable == currentSyllable) { if (!currentSyllable.getLastAllophone().isDiphthong()) { appendR = true; } } } } if (appendR) { iterator.remove(); currentSyllable.appendAllophone(allophone); } else { currentSyllable = new Syllable(allophone); iterator.set(currentSyllable); } } } catch (IllegalArgumentException e) { if (!getIgnoreChars().contains(phone)) { throw e; } } } currentSyllable = null; boolean foundPrimaryStress = false; iterator = phonesAndSyllables.listIterator(phonesAndSyllables.size()); while (iterator.hasPrevious()) { Object phoneOrSyllable = iterator.previous(); if (phoneOrSyllable instanceof Syllable) { currentSyllable = (Syllable) phoneOrSyllable; } else if (currentSyllable == null) { continue; } else { String phone = (String) phoneOrSyllable; try { Allophone allophone = getAllophone(phone); if (allophone.sonority() < currentSyllable.getFirstAllophone().sonority()) { iterator.remove(); currentSyllable.prependAllophone(allophone); } } catch (IllegalArgumentException e) { switch (phone) { case Stress.PRIMARY: iterator.remove(); currentSyllable.setStress(Stress.PRIMARY); foundPrimaryStress = true; break; case Stress.SECONDARY: iterator.remove(); currentSyllable.setStress(Stress.SECONDARY); break; case "-": iterator.remove(); break; default: throw e; } } } } Syllable initialSyllable = currentSyllable; currentSyllable = null; iterator = phonesAndSyllables.listIterator(); while (iterator.hasNext()) { Object phoneOrSyllable = iterator.next(); if (phoneOrSyllable instanceof Syllable) { currentSyllable = (Syllable) phoneOrSyllable; } else { String phone = (String) phoneOrSyllable; try { Allophone allophone; try { allophone = getAllophone(phone); } catch (IllegalArgumentException e) { if (getIgnoreChars().contains(phone)) { iterator.remove(); continue; } else { throw e; } } if (currentSyllable == null) { iterator.remove(); if (initialSyllable == null) { initialSyllable = new Syllable(allophone); iterator.add(initialSyllable); } else { initialSyllable.prependAllophone(allophone); } } else { iterator.remove(); currentSyllable.appendAllophone(allophone); } } catch (IllegalArgumentException e) { throw e; } } } if (!foundPrimaryStress) { initialSyllable.setStress(Stress.PRIMARY); } return StringUtils.join(phonesAndSyllables, " - "); } private  AllophoneSet(InputStream inputStream); static AllophoneSet getAllophoneSet(String filename); static boolean hasAllophoneSet(String identifier); static AllophoneSet getAllophoneSetById(String identifier); static AllophoneSet getAllophoneSet(InputStream inStream, String identifier); Locale getLocale(); Allophone getAllophone(String ph); Allophone getSilence(); String getIgnoreChars(); String getPhoneFeature(String ph, String featureName); Set<String> getPhoneFeatures(); String[] getPossibleFeatureValues(String featureName); Set<String> getAllophoneNames(); Allophone[] splitIntoAllophones(String allophoneString); String splitAllophoneString(String allophoneString); List<String> splitIntoAllophoneList(String allophonesString); boolean checkAllophoneSyntax(String allophoneString); String syllabify(String phoneString);  }
@Test(dataProvider = "legacySyllabifierData") public void testLegacySyllabify(String phones, String expected) { String actual = allophoneSet.syllabify(phones); Assert.assertEquals(actual, expected); }
@Test public void testLegacySyllabifyWithoutNucleus() { String actual = allophoneSet.syllabify("s1"); String expected = "' s"; Assert.assertEquals(actual, expected); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSyllabifyWithEmptyInput() { allophoneSet.syllabify(""); }
@Test(dataProvider = "syllabifierData") public void testSyllabify(String phones, String expected) { String actual = allophoneSet.syllabify(phones); Assert.assertEquals(actual, expected); }
@Test public void testSyllabifyWithoutNucleus() { String actual = allophoneSet.syllabify("s"); String expected = "' s"; Assert.assertEquals(actual, expected); }