@Override public List<Rule> annotate(List<String> tokens) { if(tokens.size() == 1) { try { String n = tokens.get(0); Object f = Double.valueOf(n); return Collections.singletonList(new Rule(SYMBOL, n, SemanticUtils.value(f))); }catch(NumberFormatException e) { } } return Collections.emptyList(); }
private Rule parseMonth(String s) { Rule r = null; if(mapMonths.containsKey(s.toLowerCase())) r = new Rule("$DATE_MONTH", s, SemanticUtils.named("month", mapMonths.get(s))); return r; }
private Rule parseTime(String s) { Rule rule = null; if(SHIFT_MAP.containsKey(s)){ rule = new Rule("$DATE_SHIFT", s, SemanticUtils.named("shift", SHIFT_MAP.get(s))); } return rule; }
private Rule parseDay(String s) { Rule r = null; if(s.equals("today")) r = new Rule("$DATE_OFFSET", s, SemanticUtils.named("offset", SemanticUtils.named("day", 0.0))); else if(s.equals("tomorrow")) r = new Rule("$DATE_OFFSET", s, SemanticUtils.named("offset", SemanticUtils.named("day", 1.0))); else if(mapDow.containsKey(s)) r = new Rule("$DATE_DOW", s, SemanticUtils.named("dow", mapDow.get(s))); return r; }
@Override public List<Rule> annotate(List<String> tokens) { if(tokens.size() == 1) return Collections.singletonList( new Rule(SYMBOL, StringTuple.fromList(tokens), SemanticUtils.valueFn(tokens.get(0)))); return Collections.emptyList(); }
@Override public List<Rule> annotate(List<String> tokens) { String phrase = StringTuple.joinList(" ", tokens); return Collections.singletonList( new Rule(SYMBOL, StringTuple.fromList(tokens), SemanticUtils.valueFn(phrase))); }
@Test void getLexicalRules() { List<Rule> rules = Collections.singletonList(new Rule("$A", "B C")); Grammar grammar = new Grammar(rules, "$ROOT"); assertEquals(rules.get(0), grammar.getLexicalRules(Arrays.asList("B", "C")).get(0)); assertEquals(0, grammar.getBinaryRules("$B", "$C").size()); assertEquals(0, grammar.getUnaryRules("$B").size()); }
@Test void getBinaryRules() { List<Rule> rules = Collections.singletonList(new Rule("$A", "$B $C")); Grammar grammar = new Grammar(rules, "$ROOT"); assertEquals(rules.get(0), grammar.getBinaryRules("$B", "$C").get(0)); assertEquals(0, grammar.getLexicalRules(Arrays.asList("$B", "$C")).size()); assertEquals(0, grammar.getUnaryRules("$B").size()); } }
@Test void getUnaryRules() { List<Rule> rules = Collections.singletonList(new Rule("$A", "$B")); Grammar grammar = new Grammar(rules, "$ROOT"); assertEquals(rules.get(0), grammar.getUnaryRules("$B").get(0)); assertEquals(0, grammar.getBinaryRules("$B", "$C").size()); assertEquals(0, grammar.getLexicalRules(Arrays.asList("$B", "$C")).size()); }
@Test void applyLexicalRules() { List<Rule> rules = Collections.singletonList(new Rule("$A", "B C")); Grammar grammar = new Grammar(rules, "$ROOT"); Parser p = new Parser(grammar, null, null); Parser.Chart chart = p.new Chart(10); List<String> tokens = Arrays.asList("A", "B", "C"); p.applyLexicalRules(chart, tokens, 1, 3); assertEquals(rules.get(0), chart.getDerivations(1, 3).get(0).rule); }
@Test void applySemantics() { Rule r1 = new Rule("$A", "a"); Rule r2 = new Rule("$B", "$A $A", "{b:@1}"); Derivation dc1 = new Derivation(r1, null); Derivation dc2 = new Derivation(r1, null); Derivation d = new Derivation(r2, Arrays.asList(dc1, dc2)); Map<String, Object> expected = new HashMap<String, Object>(){{ put("b", "a"); }}; Parser p = new Parser(null, null, null); assertEquals(expected, p.applySemantics(d).get(0)); }
@Test void shouldSplitNaryRule() { Rule[] rules = { new Rule("$A", "$B $C $D") }; Grammar g = new Grammar(Arrays.asList(rules), null); assertTrue(g.binaryRules.containsKey(new StringTuple("$C $D"))); assertTrue(g.binaryRules.containsKey(new StringTuple("$B $A_$B"))); assertEquals(g.binaryRules.get(new StringTuple("$C $D")).get(0).getLHS(), "$A_$B"); assertEquals(g.binaryRules.get(new StringTuple("$B $A_$B")).get(0).getLHS(), "$A"); }
@Test void getRuleFeatures() { Rule r1 = new Rule("$A", "a"); Rule r2 = new Rule("$B", "$A $A"); Derivation dc1 = new Derivation(r1, Collections.emptyList()); Derivation dc2 = new Derivation(r1, Collections.emptyList()); Derivation d = new Derivation(r2, Arrays.asList(dc1, dc2)); Map<String, Integer> expected = new HashMap<String, Integer>(){{ put(r1.toString(), 2); put(r2.toString(), 1); }}; assertEquals(expected, d.getRuleFeatures()); } }
@Test void shouldSplitOptionals() { Rule[] rules = { new Rule("$A", "?$B $C") }; Grammar g = new Grammar(Arrays.asList(rules), null); assertTrue(g.binaryRules.containsKey(new StringTuple("$B $C"))); assertTrue(g.unaryRules.containsKey(new StringTuple("$C"))); assertEquals(g.binaryRules.get(new StringTuple("$B $C")).get(0).getLHS(), "$A"); assertEquals(g.unaryRules.get(new StringTuple("$C")).get(0).getLHS(), "$A"); }
@Test void getRHS() { Rule r = new Rule("$A", "B C D"); assertEquals(r.getRHS(), StringTuple.fromParts("B", "C", "D")); }