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); }
@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); } }
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 parseSemanticsValue() { List<Map<String, Object>> expected = Collections.singletonList( value("test") ); assertEquals(expected, SemanticUtils.parseSemantics("test").apply(null)); }
@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 parseSemantics() { List<Map<String, Object>> params = Arrays.asList( named("a", 1), named("b",2), named("c", 3) ); List<Map<String, Object>> first = Collections.singletonList( named("a", 1) ); List<Map<String, Object>> mid = Collections.singletonList( named("b", 2) ); assertEquals(first, SemanticUtils.parseSemantics("@first").apply(params)); assertEquals(mid, SemanticUtils.parseSemantics("@1").apply(params)); }
@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 parseSemanticsJson() { List<Map<String, Object>> params = Arrays.asList( value(1), named("x", 10), value(3) ); List<Map<String, Object>> params2 = Arrays.asList( value("hello"), named("z", -1), value(30) ); Map<String, Object> expected = new HashMap<String, Object>(){{ put("a", 1); put("b", new HashMap<String, Object>() {{put("x", 10);}}); put("c", new HashMap<String, Object>() {{put("d", 3);}}); put("e", "hello"); }}; Map<String, Object> expected2 = new HashMap<String, Object>(){{ put("a", "hello"); put("b", new HashMap<String, Object>() {{put("z", -1);}}); put("c", new HashMap<String, Object>() {{put("d", 30);}}); put("e", "hello"); }}; String json = "{a:@first, b:@1, c:{d:@last}, e:hello}"; SemanticFunction fn = SemanticUtils.parseSemantics(json); assertEquals(Collections.singletonList(expected), fn.apply(params)); assertEquals(Collections.singletonList(expected2), fn.apply(params2)); }
@Test void parseTemplate() { Map<String, Object> template = named("a", _FIRST).named("b", _N(1)) .named("c", named("d", _LAST)); Map<String, Object> expected = new HashMap<String, Object>(){{ put("a", 1); put("b", new HashMap<String, Object>() {{put("x", 10);}}); put("c", new HashMap<String, Object>() {{put("d", 3);}}); }}; List<Map<String, Object>> params = Arrays.asList( value(1), named("x", 10), value(3) ); SemanticFunction fn = SemanticUtils.parseTemplate(template); assertEquals(Collections.singletonList(expected), fn.apply(params)); }
@Test void parseSemanticsMerge() { Map<String, Object> expected = new HashMap<String, Object>(){{ put("a", 1); put("x", 10); put("y", 20); }}; List<Map<String, Object>> params = Arrays.asList( value(1), named("x", 10), named("y", 20) ); String json = "{a:@first, @merge:[1,2]}"; SemanticFunction fn = SemanticUtils.parseSemantics(json); assertEquals(Collections.singletonList(expected), fn.apply(params)); }
@Test void parseTemplateMerge() { Map<String, Object> template = named("a", _FIRST) .named("@merge", Arrays.asList(1, 2)); Map<String, Object> expected = new HashMap<String, Object>(){{ put("a", 1); put("x", 10); put("y", 20); }}; List<Map<String, Object>> params = Arrays.asList( value(1), named("x", 10), named("y", 20) ); SemanticFunction fn = SemanticUtils.parseTemplate(template); assertEquals(Collections.singletonList(expected), fn.apply(params)); }
@Test void parseTemplateAppend(){ Map<String, Object> template = named("x", _APPEND); Map<String, Object> expected = named("x", Arrays.asList( named("a", 10), named("b",20), named("c",3) )); List<Map<String, Object>> params = Arrays.asList( value(1), named("x", Arrays.asList(named("a", 10), named("b", 20))), named("c",3) ); SemanticFunction fn = SemanticUtils.parseTemplate(template); assertEquals(Collections.singletonList(expected), fn.apply(params)); }