EntryFstMaker { public MutableFst inputToFst(Word inputWord, SymbolTable inputSymbols) { MutableFst efst = new MutableFst(RING); efst.setInputSymbolsAsCopy(inputSymbols); efst.setOutputSymbolsAsCopy(inputSymbols); MutableState prevState = efst.newStartState(); MutableState sentStartState = efst.newState(); MutableState nextState = sentStartState; efst.addArc(prevState, START, START, nextState, RING.one()); for (String letter : inputWord) { prevState = nextState; nextState = efst.newState(); efst.addArc(prevState, letter, letter, nextState, RING.one()); } MutableState endSentState = efst.newState(); efst.addArc(nextState, END, END, endSentState, RING.one()); endSentState.setFinalWeight(RING.one()); for (int i = 0; i < inputWord.unigramCount(); i++) { prevState = efst.getState(sentStartState.getId() + i); List<String> suffix = inputWord.getValue().subList(i, inputWord.unigramCount()); List<Map.Entry<? extends List<String>, String>> matches = clusterAcceptor.accept(suffix); for (Map.Entry<? extends List<String>, String> match : matches) { nextState = efst.getState(sentStartState.getId() + i + match.getKey().size()); efst.addArc(prevState, match.getValue(), match.getValue(), nextState, RING.one()); } } return efst; } EntryFstMaker(Iterable<String> symbols); MutableFst inputToFst(Word inputWord, SymbolTable inputSymbols);  }
@Test public void shouldCovertNoClusters() throws Exception { EntryFstMaker maker = new EntryFstMaker(ImmutableSet.of("A", "B", "C", "D", "E")); MutableFst fst1 = maker.inputToFst(Word.fromNormalString("STEVE"), new MutableSymbolTable()); for (int i = 0; i < fst1.getStateCount(); i++) { if (i == fst1.getStateCount() - 1) { assertEquals(0, fst1.getState(i).getArcCount()); } else { assertEquals(1, fst1.getState(i).getArcCount()); } } }
@Test public void shouldConvertWithClusters() throws Exception { EntryFstMaker maker = new EntryFstMaker(ImmutableSet.of("S", "T", "E", "V", "E", "S|T", "T|E", "S|T|E", "V|E")); MutableFst fst1 = maker.inputToFst(Word.fromNormalString("STEVE"), new MutableSymbolTable()); assertEquals(8, fst1.getStateCount()); }