public void interactive(){ model.loadWeights(weightsPath); Parser parser = model.getParser(); Scanner scanner = new Scanner(System.in); while(true){ System.out.print(">> "); String input = scanner.nextLine(); List<LogicalForm> results = parser.parse(input); if(results.size() > 0) System.out.println(results.get(0)); else System.err.println("Failed to parse input\n"); } } }
boolean firstCorrect = false; List<LogicalForm> lfs = parser.parse(e.input); for(LogicalForm lf: lfs){ if(first)
@Override public float optimize(Example e) { List<LogicalForm> candidates = parser.parse(e.input); if(candidates.isEmpty()) return 1; LogicalForm lf = randomCandidate(e, candidates); Derivation d = lf.getDerivation(); Map<String, Integer> features = d.getRuleFeatures(); features.putAll(lf.fields().stream().collect(Collectors.toMap(Function.identity(), k -> 1))); float target = computeTarget(lf, e); float loss = hingeLoss(features, target); if(loss > 0) updateWeights(features, target, learnRate); updateL2Penalty(learnRate, l2Penalty); return loss; }
@Test void parse() { List<Rule> rules = Arrays.asList( new Rule("$A", "a"), new Rule("$B", "b"), new Rule("$C", "$A $B", "{e:@first, f:@last}") ); Grammar grammar = new Grammar(rules, "$C"); Parser p = new Parser(grammar, s -> Arrays.asList(s.split(" ")), Collections.emptyList()); Map<String, Object> expected = new HashMap<String, Object>(){{ put("e", "a"); put("f", "b"); }}; List<LogicalForm> actual = p.parse("a b"); assertEquals(1, actual.size()); assertEquals(expected, actual.get(0).getMap()); }