public List<Derivation> parseSyntactic(String input){ List<String> tokens = tokenizer.tokenize(input); List<String> tokensLower = new ArrayList<>(tokens.size()); for(String token: tokens) tokensLower.add(token.toLowerCase()); int N = tokens.size(); Chart chart = new Chart(N+1); for(int e = 1; e <= N; e++) { for(int s = e-1; s >= 0; s--) { applyAnnotators(chart, tokens, s, e); applyLexicalRules(chart, tokensLower, s, e); applyBinaryRules(chart, s, e); applyUnaryRules(chart, s, e); } } List<Derivation> derivations = new LinkedList<>(); for(Derivation d: chart.getDerivations(0, N)) if(grammar.isRoot(d.rule)) derivations.add(d); return derivations; }
@Test void applyBinaryRules() { List<Rule> rules = Collections.singletonList( new Rule("$C", "$A $B")); Grammar grammar = new Grammar(rules, "$ROOT"); Parser p = new Parser(grammar, null, null); Parser.Chart chart = p.new Chart(10); chart.addDerivation(0, 1, new Derivation(new Rule("$A", "A"), null)); chart.addDerivation(1, 2, new Derivation(new Rule("$B", "B"), null)); p.applyBinaryRules(chart, 0, 2); assertEquals(rules.get(0), chart.getDerivations(0, 2).get(0).rule); } }