public StringTuple remove(int index) throws IndexOutOfBoundsException{ if( index < 0 || index >= items.size()) throw new IndexOutOfBoundsException(); List<String> copy = stringToItems(repr); copy.remove(index); String modifiedRepr = joinList(" ", copy); return new StringTuple(modifiedRepr, copy); }
public List<Rule> getUnaryRules(String rhs){ StringTuple tuple = StringTuple.fromParts(rhs); return unaryRules.containsKey(tuple) ? unaryRules.get(tuple) : Collections.emptyList(); }
public static StringTuple fromList(List<String> list) { List<String> items = new LinkedList<String>(list); String repr = joinList(" ", items); return new StringTuple(repr, items); }
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 testModify() { StringTuple s = new StringTuple("1 2 3"); StringTuple changed = s.modify(1, (item) -> "4"); assertEquals(changed, new StringTuple("1 4 3")); assertNotEquals(changed, s); }
@Test void testRemove() { StringTuple s = new StringTuple("1 2 3"); StringTuple changed = s.remove(0); assertEquals(changed, new StringTuple("2 3")); assertNotEquals(changed, s); }
public Rule(String lhs, String rhs) { this(lhs, new StringTuple(rhs)); }
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 testInsert() { StringTuple s = new StringTuple("2 3"); StringTuple changed = s.insert(0, "X"); assertEquals(changed, new StringTuple("X 2 3")); assertNotEquals(changed, s); }
@Test void joinList(){ List<String> items = Arrays.asList("a", "bb", "ccc", "12"); assertEquals("a bb ccc 12", StringTuple.joinList(" ", items)); } }
public static StringTuple fromParts(String ...parts) { return fromList(Arrays.asList(parts)); }
@Test void testHashing() { HashMap<StringTuple, String> map = new HashMap<>(); map.put(new StringTuple("A B C"), "Test"); assertTrue(map.containsKey(new StringTuple("A B C"))); }
@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)); } }
public List<Rule> getLexicalRules(List<String> rhs){ StringTuple tuple = StringTuple.fromList(rhs); return lexicalRules.containsKey(tuple) ? lexicalRules.get(tuple) : Collections.emptyList(); }
public StringTuple insert(int index, String value) throws IndexOutOfBoundsException{ if( index < 0 || index >= items.size()) throw new IndexOutOfBoundsException(); List<String> copy = stringToItems(repr); copy.add(index, value); String modifiedRepr = joinList(" ", copy); return new StringTuple(modifiedRepr, copy); }
public List<Rule> getBinaryRules(String left, String right){ StringTuple tuple = StringTuple.fromParts(left, right); return binaryRules.containsKey(tuple) ? binaryRules.get(tuple) : Collections.emptyList(); }
@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 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));; } }