NGramExtractor { public Expression<String> extract(Automaton automaton) { if (automaton.isAccept(0)) { return True.instance(); } return new NGramAutomaton(automaton, gramSize, maxExpand, maxStatesTraced, maxNgrams, ngramAnalyzer).expression().simplify(); } NGramExtractor(int gramSize, int maxExpand, int maxStatesTraced, int maxNgrams, Analyzer ngramAnalyzer); Expression<String> extract(Automaton automaton);  }
@Test public void simple() { NGramExtractor gram = new NGramExtractor(3, 4, 10000, 100, new KeywordAnalyzer()); Automaton automaton = new RegExp("hero of legend").toAutomaton(); assertEquals( new And<>(leaves("her", "ero", "ro ", "o o", " of", "of ", "f l", " le", "leg", "ege", "gen", "end")), gram.extract(automaton)); automaton = new RegExp("").toAutomaton(); assertEquals(True.<String> instance(), gram.extract(automaton)); automaton = new RegExp(".*").toAutomaton(); assertEquals(True.<String> instance(), gram.extract(automaton)); automaton = new RegExp("he").toAutomaton(); assertEquals(True.<String> instance(), gram.extract(automaton)); automaton = new RegExp("her").toAutomaton(); assertEquals(new Leaf<>("her"), gram.extract(automaton)); }
@Test public void maxNgrams() { NGramExtractor gram = new NGramExtractor(3, 4, 10000, 3, new KeywordAnalyzer()); Automaton automaton = new RegExp("hero of legend").toAutomaton(); assertEquals( new And<>(leaves("her", "ero", "ro ")), gram.extract(automaton)); }