private void addUnaryRule(Rule rule) { nonTerminals.add(rule); computeIfAbsent(unaryRules, rule.getRHS(), new LinkedList<>()).add(rule); }
private void addBinaryRule(Rule rule) { nonTerminals.add(rule); computeIfAbsent(binaryRules, rule.getRHS(), new LinkedList<>()).add(rule); }
private void addLexicalRule(Rule rule) { computeIfAbsent(lexicalRules, rule.getRHS(), new LinkedList<>()).add(rule); }
List<Map<String, Object>> applySemantics(Derivation d){ SemanticFunction fn = d.rule.getSemantics(); if(d.children == null) { if(fn == null) return Collections.singletonList(SemanticUtils.value(d.rule.getRHS().toString())); else return fn.apply(Collections.emptyList()); } String rule = d.rule.toString(); d.score = weights.containsKey(rule) ? weights.get(rule) : 0f; List<Map<String, Object>> params = new LinkedList<>(); for(Derivation child: d.children){ params.addAll(applySemantics(child)); d.score += child.score; } return fn.apply(params); }
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())); }
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)); }
@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));; } }
@Test void getRHS() { Rule r = new Rule("$A", "B C D"); assertEquals(r.getRHS(), StringTuple.fromParts("B", "C", "D")); }