@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)); }
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); }
Parser { 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); } }
Parser { 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); } Parser(Grammar grammar, Tokenizer tokenizer, List<Annotator> annotators); }
Parser { 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); } Parser(Grammar grammar, Tokenizer tokenizer, List<Annotator> annotators); Map<String, Float> getWeights(); void setWeights(Map<String, Float> weights); List<Derivation> parseSyntactic(String input); List<LogicalForm> parse(String input); LogicalForm computeLogicalForm(Derivation d); }
Parser { 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); } Parser(Grammar grammar, Tokenizer tokenizer, List<Annotator> annotators); Map<String, Float> getWeights(); void setWeights(Map<String, Float> weights); List<Derivation> parseSyntactic(String input); List<LogicalForm> parse(String input); LogicalForm computeLogicalForm(Derivation d);  }