/** Test a configuration that behaves a lot like KeepWordFilter */ public void testKeep() throws Exception { CharacterRunAutomaton keepWords = new CharacterRunAutomaton( BasicOperations.complement( Automaton.union( Arrays.asList( BasicAutomata.makeString("foo"), BasicAutomata.makeString("bar"))))); Analyzer a = new MockAnalyzer(MockTokenizer.SIMPLE, true, keepWords, true); assertAnalyzesTo( a, "quick foo brown bar bar fox foo", new String[] {"foo", "bar", "bar", "foo"}, new int[] {2, 2, 1, 2}); }
/** * Create a automaton for a given context query this automaton will be used to find the matching * paths with the fst * * @param preserveSep set an additional char (<code>XAnalyzingSuggester.SEP_LABEL</code>) * between each context query * @param queries list of {@link ContextQuery} defining the lookup context * @return Automaton matching the given Query */ public static Automaton toAutomaton(boolean preserveSep, Iterable<ContextQuery> queries) { Automaton a = BasicAutomata.makeEmptyString(); Automaton gap = BasicAutomata.makeChar(ContextMapping.SEPARATOR); if (preserveSep) { // if separators are preserved the fst contains a SEP_LABEL // behind each gap. To have a matching automaton, we need to // include the SEP_LABEL in the query as well gap = BasicOperations.concatenate(gap, BasicAutomata.makeChar(XAnalyzingSuggester.SEP_LABEL)); } for (ContextQuery query : queries) { a = Automaton.concatenate(Arrays.asList(query.toAutomaton(), gap, a)); } BasicOperations.determinize(a); return a; }
@Override public String toString(String field) { return field.toString() + automaton.toString(); }