CmuDictionaryReader { static Map<String, List<WordVariant>> readWords(SyllableParser syllableParser, InputStream is) throws IOException { Map<String, List<WordVariant>> words = new HashMap<>(); BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new InputStreamReader(is)); Pattern pattern = Pattern.compile("(^.*)\\(([0-9])\\).*$"); for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) { if (line.isEmpty()) continue; if (line.startsWith(";;;")) continue; int wordSeparator = line.indexOf(" "); String word = line.substring(0, wordSeparator).toLowerCase(Locale.US); int variantNumber = 0; Matcher matcher = pattern.matcher(word); if(matcher.matches()) { word = matcher.group(1); variantNumber = Integer.valueOf(matcher.group(2)); } String[] phones = line.substring(wordSeparator+2).split(" "); String[] syllables = syllableParser.extractRhymingSyllables(phones); String lastStressRhymingSyllables = syllableParser.extractStressRhymingSyllables(phones); String lastRhymingSyllable = concatenateLastSyllables(syllables, 1); String lastTwoRhymingSyllables = concatenateLastSyllables(syllables, 2); String lastThreeRhymingSyllables = concatenateLastSyllables(syllables, 3); WordVariant wordVariant = new WordVariant(variantNumber, lastStressRhymingSyllables, lastRhymingSyllable, lastTwoRhymingSyllables, lastThreeRhymingSyllables); List<WordVariant> wordVariants = words.get(word); if(wordVariants == null) { wordVariants = new ArrayList<>(); words.put(word, wordVariants); } wordVariants.add(wordVariant); } } finally { if (bufferedReader != null) bufferedReader.close(); } return words; } private  CmuDictionaryReader();   }
@Test public void testLoadWords() throws IOException { Map<String, List<WordVariant>> words = readWords(); Assert.assertNotNull(words); Assert.assertEquals(125074, words.size()); testWordSyllables("zynda", "AH", "IHNDAH", null, words); testWordSyllables("zyman", "AHN", "AYMAHN", null, words); testWordSyllables("cat", "AET", null, null, words); testWordSyllables("celebrate", "EYT", "AHBREYT", "EHLAHBREYT", words); }