HmmUtils { public static int[] encodeStateSequence(HmmModel model, Collection<String> sequence, boolean observed, int defaultValue) { int[] encoded = new int[sequence.size()]; Iterator<String> seqIter = sequence.iterator(); for (int i = 0; i < sequence.size(); ++i) { String nextState = seqIter.next(); int nextID; if (observed) { nextID = model.getOutputStateID(nextState); } else { nextID = model.getHiddenStateID(nextState); } encoded[i] = nextID < 0 ? defaultValue : nextID; } return encoded; } private  HmmUtils(); static Matrix getCumulativeTransitionMatrix(HmmModel model); static Matrix getCumulativeOutputMatrix(HmmModel model); static Vector getCumulativeInitialProbabilities(HmmModel model); static void validate(HmmModel model); static int[] encodeStateSequence(HmmModel model,
                                          Collection<String> sequence, boolean observed, int defaultValue); static List<String> decodeStateSequence(HmmModel model,
                                                 int[] sequence,
                                                 boolean observed,
                                                 String defaultValue); static void normalizeModel(HmmModel model); static HmmModel truncateModel(HmmModel model, double threshold);  }
@Test public void testEncodeStateSequence() { String[] hiddenSequence = {"H1", "H2", "H0", "H3", "H4"}; String[] outputSequence = {"O1", "O2", "O4", "O0"}; int[] hiddenSequenceEnc = HmmUtils.encodeStateSequence(getModel(), Arrays .asList(hiddenSequence), false, -1); int[] outputSequenceEnc = HmmUtils.encodeStateSequence(getModel(), Arrays .asList(outputSequence), true, -1); int[] hiddenSequenceExp = {1, 2, 0, 3, -1}; int[] outputSequenceExp = {1, 2, -1, 0}; for (int i = 0; i < hiddenSequenceEnc.length; ++i) { assertEquals(hiddenSequenceExp[i], hiddenSequenceEnc[i]); } for (int i = 0; i < outputSequenceEnc.length; ++i) { assertEquals(outputSequenceExp[i], outputSequenceEnc[i]); } }