public boolean isRoot(Rule rule) { return rule.getLHS().equals(rootCategory); }
void applyUnaryRules(Chart chart, int start, int end) { LinkedList<Derivation> queue = new LinkedList<>(chart.getDerivations(start, end)); while(!queue.isEmpty()) { Derivation d = queue.removeFirst(); for(Rule rule: grammar.getUnaryRules(d.rule.getLHS())) { if(chart.isSpanFull(start, end)) return; //System.out.println(rule); Derivation parent = new Derivation(rule, Collections.singletonList(d)); queue.addLast(parent); chart.addDerivation(start, end, parent); } } }
void applyBinaryRules(Chart chart, int start, int end) { if(end > start + 1) { for(int mid = start + 1; mid < end; mid++) { List<Derivation> left = chart.getDerivations(start, mid); List<Derivation> right = chart.getDerivations(mid, end); for(Derivation l:left) { for(Derivation r: right) { for(Rule rule: grammar.getBinaryRules(l.rule.getLHS(), r.rule.getLHS())) { if(chart.isSpanFull(start, end)) return; //System.out.println(rule); chart.addDerivation(start, end, new Derivation(rule, Arrays.asList(l, r))); } } } } } }
private void processOptionals(Rule rule) { StringTuple rhs = rule.getRHS(); int index = rhs.find(rule::isOptional); assert index != -1; StringTuple withOptional = rhs.modify(index, s->s.substring(1)); StringTuple withoutOptional = rhs.remove(index); assert withoutOptional.size() > 0: String.format("Cannot have all optionals in rule: %s", rule); addRule(new Rule(rule.getLHS(), withOptional, rule.getSemantics())); // Insert blank argument for the removed item SemanticFunction fn = params -> { params.add(index, new HashMap<>()); return rule.getSemantics().apply(params); }; addRule(new Rule(rule.getLHS(), withoutOptional, fn)); }
private void processNAryRule(Rule rule) { StringTuple rhs = rule.getRHS(); String lhsUpper = rule.getLHS(); String lhsLower = String.format("%s_%s", lhsUpper, rhs.get(0)); // Check for existing names while(combinedCategories.contains(lhsLower)) { lhsLower = lhsLower.concat("_"); } combinedCategories.add(lhsLower); StringTuple rhsLower = rhs.remove(0); StringTuple rhsUpper = StringTuple.fromParts(rhs.get(0), lhsLower); addRule(new Rule(lhsLower, rhsLower, SemanticUtils::identity)); addRule(new Rule(lhsUpper, rhsUpper, rule.getSemantics())); }
@Test void annotateMonth() { List<String> tokens = Collections.singletonList("May"); List<Rule> rules = DateTimeAnnotator.INSTANCE.annotate(tokens); assertEquals(rules.get(0).getLHS(), "$DATE_MONTH"); List<Map<String, Object>> result = rules.get(0).getSemantics().apply(Collections.emptyList()); assertTrue(result.get(0).containsKey("month")); assertEquals(result.get(0).get("month"), 5.0); }
@Test void annotateDow() { List<String> tokens = Collections.singletonList("Sunday"); List<Rule> rules = DateTimeAnnotator.INSTANCE.annotate(tokens); assertEquals(rules.get(0).getLHS(), "$DATE_DOW"); List<Map<String, Object>> result = rules.get(0).getSemantics().apply(Collections.emptyList()); assertTrue(result.get(0).containsKey("dow")); assertEquals(result.get(0).get("dow"), 7.0); } }
@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 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 annotate() { Annotator annotator = new NumberAnnotator(); List<String> tokens = Collections.singletonList("123"); List<Rule> actual = annotator.annotate(tokens); assertEquals(1, actual.size()); assertEquals("$NUMBER", actual.get(0).getLHS()); assertEquals("123", actual.get(0).getRHS().toString()); assertEquals(123.0, actual.get(0) .getSemantics().apply(null) .get(0).get(SemanticUtils.KEY_UNNAMED)); } }
@Test void annotate() { Annotator annotator = new TokenAnnotator(); List<String> tokens = Collections.singletonList("Single"); String expected = "Single"; List<Rule> actual = annotator.annotate(tokens); assertEquals(1, actual.size()); assertEquals("$TOKEN", actual.get(0).getLHS()); assertEquals(expected, actual.get(0).getRHS().toString()); assertEquals(expected, actual.get(0) .getSemantics().apply(null) .get(0).get(SemanticUtils.KEY_UNNAMED));; } }
@Test void annotate() { Annotator annotator = new PhraseAnnotator(); List<String> tokens = Arrays.asList( "The", "boy", "who", "cried" ); String expected = "The boy who cried"; List<Rule> actual = annotator.annotate(tokens); assertEquals(1, actual.size()); assertEquals("$PHRASE", actual.get(0).getLHS()); assertEquals(expected, actual.get(0).getRHS().toString()); assertEquals(expected, actual.get(0) .getSemantics().apply(null) .get(0).get(SemanticUtils.KEY_UNNAMED));; } }