BeamSearch { public Sequence bestSequence(T[] sequence, Object[] additionalContext) { Sequence sequences[] = bestSequences(1, sequence, additionalContext,zeroLog); if (sequences.length > 0) return sequences[0]; else return null; } BeamSearch(int size, BeamSearchContextGenerator<T> cg, MaxentModel model);  BeamSearch(int size, BeamSearchContextGenerator<T> cg, MaxentModel model,
      int cacheSize);  BeamSearch(int size, BeamSearchContextGenerator<T> cg, MaxentModel model,
      SequenceValidator<T> validator, int cacheSize); Sequence[] bestSequences(int numSequences, T[] sequence, Object[] additionalContext); Sequence[] bestSequences(int numSequences, T[] sequence, Object[] additionalContext, double minSequenceScore); Sequence bestSequence(T[] sequence, Object[] additionalContext);  }
@Test public void testBestSequenceZeroLengthInput() { String sequence[] = new String[0]; BeamSearchContextGenerator<String> cg = new IdentityFeatureGenerator(sequence); String outcomes[] = new String[] {"1", "2", "3"}; MaxentModel model = new IdentityModel(outcomes); BeamSearch<String> bs = new BeamSearch<String>(3, cg, model); Sequence seq = bs.bestSequence(sequence, null); assertNotNull(seq); assertEquals(sequence.length, seq.getOutcomes().size()); }
@Test public void testBestSequenceOneElementInput() { String sequence[] = {"1"}; BeamSearchContextGenerator<String> cg = new IdentityFeatureGenerator(sequence); String outcomes[] = new String[] {"1", "2", "3"}; MaxentModel model = new IdentityModel(outcomes); BeamSearch<String> bs = new BeamSearch<String>(3, cg, model); Sequence seq = bs.bestSequence(sequence, null); assertNotNull(seq); assertEquals(sequence.length, seq.getOutcomes().size()); assertEquals("1", seq.getOutcomes().get(0)); }
@Test public void testBestSequence() { String sequence[] = {"1", "2", "3", "2", "1"}; BeamSearchContextGenerator<String> cg = new IdentityFeatureGenerator(sequence); String outcomes[] = new String[] {"1", "2", "3"}; MaxentModel model = new IdentityModel(outcomes); BeamSearch<String> bs = new BeamSearch<String>(2, cg, model); Sequence seq = bs.bestSequence(sequence, null); assertNotNull(seq); assertEquals(sequence.length, seq.getOutcomes().size()); assertEquals("1", seq.getOutcomes().get(0)); assertEquals("2", seq.getOutcomes().get(1)); assertEquals("3", seq.getOutcomes().get(2)); assertEquals("2", seq.getOutcomes().get(3)); assertEquals("1", seq.getOutcomes().get(4)); }
@Test public void testBestSequenceWithValidator() { String sequence[] = {"1", "2", "3", "2", "1"}; BeamSearchContextGenerator<String> cg = new IdentityFeatureGenerator(sequence); String outcomes[] = new String[] {"1", "2", "3"}; MaxentModel model = new IdentityModel(outcomes); BeamSearch<String> bs = new BeamSearch<String>(2, cg, model, new SequenceValidator<String>(){ public boolean validSequence(int i, String[] inputSequence, String[] outcomesSequence, String outcome) { return !"2".equals(outcome); }}, 0); Sequence seq = bs.bestSequence(sequence, null); assertNotNull(seq); assertEquals(sequence.length, seq.getOutcomes().size()); assertEquals("1", seq.getOutcomes().get(0)); assertNotSame("2", seq.getOutcomes().get(1)); assertEquals("3", seq.getOutcomes().get(2)); assertNotSame("2", seq.getOutcomes().get(3)); assertEquals("1", seq.getOutcomes().get(4)); }