mirror of https://github.com/percyliang/sempre
final stress test, improvements to grammar induction, general bug fixes
This commit is contained in:
parent
f16fdf8b54
commit
32b4470e40
24
run
24
run
|
|
@ -1008,25 +1008,22 @@ addMode('interactive', 'interactive semantic parsing in a blocksworld', lambda {
|
|||
selo(0, 'BeamFloatingParser.floatStrategy', 'Never', 'NoParse', 'Always'),
|
||||
|
||||
o('ParserState.customExpectedCounts', 'None'),
|
||||
o('Params.l1Reg', 'lazy'),
|
||||
o('Params.l1RegCoeff', 0.001),
|
||||
o('Parser.beamSize', 10),
|
||||
o('Params.initStepSize', 0.2),
|
||||
o('Params.adaptiveStepSize', false),
|
||||
o('Params.stepSizeReduction', 0.1),
|
||||
o('Params.l1Reg', 'nonlazy'),
|
||||
o('Params.l1RegCoeff', 0.0001),
|
||||
o('Parser.beamSize', 25),
|
||||
o('Params.initStepSize', 0.1),
|
||||
o('Params.adaptiveStepSize', true),
|
||||
#o('Params.stepSizeReduction', 0.25),
|
||||
|
||||
o('FeatureExtractor.featureComputers', 'interactive.ActionFeatureComputer'),
|
||||
|
||||
o('FeatureExtractor.featureDomains', ':rule', ':span', ':stats', ':scope', ':social', ':window'),
|
||||
# o('FeatureExtractor.featureDomains', ':rule'),
|
||||
|
||||
# o('Master.newExamplesPath', './int-output/examples/'),
|
||||
# o('Master.logPath', './int-output/logs/'),
|
||||
o('Master.newGrammarPath', './int-output/grammar.json'),
|
||||
|
||||
# o('ILUtils.JSONLogPath', './int-output/json/'),
|
||||
o('Master.intOutputPath', './int-output/'),
|
||||
o('ILUtils.citationPath', './int-output/citation/'),
|
||||
o('ILUtils.useAligner', true),
|
||||
|
||||
o('ILUtils.useAligner', false),
|
||||
o('ILUtils.maxSequence', 20),
|
||||
|
||||
o('DefinitionAligner.strategies', 'ExactExclusion'),
|
||||
|
|
@ -1044,6 +1041,9 @@ o('JsonServer.queryLogPath', './int-output/query.log'),
|
|||
o('JsonServer.responseLogPath', './int-output/response.log'),
|
||||
o('JsonServer.port', 8410),
|
||||
|
||||
o('GrammarInducer.useBestPacking', true),
|
||||
o('GrammarInducer.useSimplePacking', true),
|
||||
|
||||
o('Derivation.showTypes', false),
|
||||
o('Derivation.showValues', false),
|
||||
o('Derivation.showRules', false),
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
30
shrdlurn/run
30
shrdlurn/run
|
|
@ -132,22 +132,40 @@ addMode('simulator', 'run the simulator', lambda { |e| l(
|
|||
'free1' => o('logFiles', './shrdlurn/queries/freebuild1-0121.json'),
|
||||
'free2' => o('logFiles', './shrdlurn/queries/freebuild2-0127.json'),
|
||||
'sidaw' => o('logFiles', './shrdlurn/queries/sidaw.json.log'),
|
||||
'all' => o('logFiles', './shrdlurn/queries/sidaw.json.log',
|
||||
'./shrdlurn/queries/freebuild1-0121.json', './shrdlurn/queries/freebuild2-0127.json'),
|
||||
'quals' => o('logFiles', './shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/qualifier2-0129.json', './shrdlurn/queries/qualifier3-0201.json'),
|
||||
'test' => o('logFiles',
|
||||
|
||||
'testqual' => o('logFiles',
|
||||
'./shrdlurn/queries/sidaw.json.log',
|
||||
#'./shrdlurn/queries/freebuild2-0127.json',
|
||||
'./shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/qualifier3-0201.json'),
|
||||
|
||||
'testfree' => o('logFiles',
|
||||
'./shrdlurn/queries/sidaw.json.log',
|
||||
#'./shrdlurn/queries/freebuild2-0127.json',
|
||||
#'./shrdlurn/queries/freebuild1-0121.json',
|
||||
'./shrdlurn/queries/freebuild2-0127.json'),
|
||||
|
||||
'stress' => o('logFiles',
|
||||
'./shrdlurn/queries/sidaw.json.log',
|
||||
'./shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/freebuild1-0121.json',
|
||||
'./shrdlurn/queries/freebuild2-0127.json',
|
||||
'./shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/qualifier3-0201.json',
|
||||
'./shrdlurn/queries/sidaw.json.log',
|
||||
'./shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/freebuild1-0121.json',
|
||||
'./shrdlurn/queries/freebuild2-0127.json',
|
||||
'./shrdlurn/queries/qualifier3-0201.json',
|
||||
'./shrdlurn/queries/sidaw.json.log',
|
||||
'./shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/freebuild1-0121.json',
|
||||
'./shrdlurn/queries/freebuild2-0127.json',
|
||||
'./shrdlurn/queries/qualifier3-0201.json',
|
||||
'./shrdlurn/queries/sidaw.json.log',
|
||||
'./shrdlurn/queries/qualifier1-0118.json',
|
||||
'./shrdlurn/queries/freebuild1-0121.json',
|
||||
'./shrdlurn/queries/freebuild2-0127.json',
|
||||
'./shrdlurn/queries/qualifier3-0201.json'
|
||||
)
|
||||
}),
|
||||
nil)})
|
||||
|
|
|
|||
|
|
@ -67,6 +67,9 @@ public final class ILUtils {
|
|||
|
||||
@Option(gloss = "mode for blocking")
|
||||
public ActionFormula.Mode blockMode = ActionFormula.Mode.sequential;
|
||||
|
||||
@Option(gloss = "verbose")
|
||||
public int verbose = 0;
|
||||
}
|
||||
|
||||
public static Options opts = new Options();
|
||||
|
|
@ -96,7 +99,7 @@ public final class ILUtils {
|
|||
}
|
||||
|
||||
public static Derivation stripBlock(Derivation deriv) {
|
||||
LogInfo.logs("StripBlock %s %s %s", deriv, deriv.rule, deriv.cat);
|
||||
if (opts.verbose > 0) LogInfo.logs("StripBlock %s %s %s", deriv, deriv.rule, deriv.cat);
|
||||
while ((deriv.rule.sem instanceof BlockFn || deriv.rule.sem instanceof IdentityFn) && deriv.children.size() == 1) {
|
||||
deriv = deriv.child(0);
|
||||
}
|
||||
|
|
@ -136,6 +139,7 @@ public final class ILUtils {
|
|||
if (d.formula.equals(targetFormula)) {
|
||||
found = true;
|
||||
allDerivs.add(stripDerivation(d));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found && !formula.equals("?"))
|
||||
|
|
@ -154,6 +158,7 @@ public final class ILUtils {
|
|||
allDerivs.add(res);
|
||||
}
|
||||
}
|
||||
// LogInfo.logs("returning deriv list %s, \n %s", allDerivs.toString(), jsonDef);
|
||||
return allDerivs;
|
||||
}
|
||||
|
||||
|
|
@ -201,6 +206,7 @@ public final class ILUtils {
|
|||
|
||||
public static List<Rule> induceRulesHelper(String command, String head, String jsonDef, Parser parser, Params params,
|
||||
String sessionId, Ref<Example> refEx) {
|
||||
|
||||
// ActionFormula.Mode blockmode = command.equals(":def") ? ActionFormula.Mode.block : ActionFormula.Mode.blockr;
|
||||
|
||||
Derivation bodyDeriv = ILUtils.combine(ILUtils.derivsfromJson(jsonDef, parser, params), opts.blockMode);
|
||||
|
|
@ -210,7 +216,6 @@ public final class ILUtils {
|
|||
Example exHead = b.createExample();
|
||||
exHead.preprocess();
|
||||
|
||||
LogInfo.begin_track("Definition");
|
||||
LogInfo.logs("mode: %s", opts.blockMode);
|
||||
LogInfo.logs("headraw: %s", head);
|
||||
LogInfo.logs("head: %s", exHead.getTokens());
|
||||
|
|
@ -238,7 +243,6 @@ public final class ILUtils {
|
|||
refEx.value = exHead;
|
||||
}
|
||||
|
||||
|
||||
List<Rule> inducedRules = new ArrayList<>();
|
||||
GrammarInducer grammarInducer = new GrammarInducer(exHead.getTokens(), bodyDeriv, state.chartList);
|
||||
inducedRules.addAll(grammarInducer.getRules());
|
||||
|
|
|
|||
|
|
@ -199,6 +199,7 @@ public class JsonServer {
|
|||
if (!Strings.isNullOrEmpty(message)) {
|
||||
response.lines.add(message);
|
||||
response.stats.put("error", message);
|
||||
LogInfo.resetInfos();
|
||||
}
|
||||
}
|
||||
return response;
|
||||
|
|
|
|||
|
|
@ -160,14 +160,12 @@ public class Learner {
|
|||
LogInfo.end_track();
|
||||
}
|
||||
|
||||
public void onlineLearnExampleByFormula(Example ex) {
|
||||
LogInfo.begin_track("onlineLearnExample: %s derivations", ex.predDerivations.size());
|
||||
public void onlineLearnExampleByFormula(Example ex, List<Formula> formulas) {
|
||||
HashMap<String, Double> counts = new HashMap<>();
|
||||
for (Derivation deriv : ex.predDerivations)
|
||||
deriv.compatibility = ex.targetFormula.equals(deriv.formula)? 1 : 0;
|
||||
deriv.compatibility = formulas.contains(deriv.formula)? 1 : 0;
|
||||
ParserState.computeExpectedCounts(ex.predDerivations, counts);
|
||||
params.update(counts);
|
||||
LogInfo.end_track();
|
||||
}
|
||||
|
||||
private Evaluation processExamples(int iter, String group,
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import java.lang.reflect.Field;
|
|||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A Master manages multiple sessions. Currently, they all share the same model,
|
||||
|
|
@ -45,7 +46,7 @@ public class Master {
|
|||
|
||||
// Interactive stuff
|
||||
@Option(gloss = "Write out new grammar rules")
|
||||
public String newGrammarPath;
|
||||
public String intOutputPath;
|
||||
@Option(gloss = "make sessions independent")
|
||||
public boolean independentSessions = false;
|
||||
@Option(gloss = "number of utterances to return for autocomplete")
|
||||
|
|
@ -218,8 +219,7 @@ public class Master {
|
|||
// Clean up
|
||||
// for (String outLine : stringOut.toString().split("\n"))
|
||||
// response.lines.add(outLine);
|
||||
LogInfo.setFileOut(null);
|
||||
|
||||
|
||||
// Log interaction to disk
|
||||
if (!Strings.isNullOrEmpty(opts.logPath)) {
|
||||
PrintWriter out;
|
||||
|
|
@ -320,20 +320,6 @@ public class Master {
|
|||
} else if (command.equals("grammar")) {
|
||||
for (Rule rule : builder.grammar.rules)
|
||||
LogInfo.logs("%s", rule.toLispTree());
|
||||
if (opts.newGrammarPath != null) {
|
||||
Set<Rule> deduper = new HashSet<>();
|
||||
String fileName = "grammar-" + LocalDateTime.now() + ".grammar";
|
||||
LogInfo.logs("Printing rules to %s", fileName);
|
||||
for (Rule rule : builder.grammar.rules) {
|
||||
if(!deduper.contains(rule)) {
|
||||
deduper.add(rule);
|
||||
LogInfo.logs("Printed rule %s", rule.toString());
|
||||
} else {
|
||||
LogInfo.logs("Skipped rule %s", rule.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else if (command.equals("params")) {
|
||||
if (tree.children.size() == 1) {
|
||||
builder.params.write(LogInfo.stdout);
|
||||
|
|
@ -464,6 +450,21 @@ public class Master {
|
|||
}
|
||||
response.ex = ex;
|
||||
|
||||
} else if (command.equals(":qdebug")) {
|
||||
// Create example
|
||||
String utt = tree.children.get(1).value;
|
||||
Example ex = exampleFromUtterance(utt, session);
|
||||
|
||||
builder.parser.parse(builder.params, ex, false);
|
||||
|
||||
Derivation.opts.showCat = true;
|
||||
Derivation.opts.showRules = true;
|
||||
for (Derivation d : ex.predDerivations) {
|
||||
response.lines.add(d.toLispTree().toString());
|
||||
}
|
||||
Derivation.opts.showCat = false;
|
||||
Derivation.opts.showRules = false;
|
||||
response.ex = ex;
|
||||
} else if (command.equals(":reject")) {
|
||||
if (session.isStatsing()) {
|
||||
response.stats.put("type", "reject");
|
||||
|
|
@ -471,16 +472,16 @@ public class Master {
|
|||
}
|
||||
} else if (command.equals(":accept")) {
|
||||
String utt = tree.children.get(1).value;
|
||||
String formula = tree.children.get(2).value;
|
||||
Formula targetFormula;
|
||||
|
||||
List<Formula> targetFormulas = new ArrayList<>();
|
||||
try {
|
||||
targetFormula = Formulas.fromLispTree(LispTree.proto.parseFromString(formula));
|
||||
targetFormulas = tree.children.subList(2, tree.children.size()).stream()
|
||||
.map(t -> Formulas.fromLispTree(LispTree.proto.parseFromString(t.value)))
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
response.lines.add("cannot accept formula: " + formula);
|
||||
formula = "(:? ERROR)";
|
||||
targetFormula = Formulas.fromLispTree(LispTree.proto.parseFromString(formula));
|
||||
e.printStackTrace();
|
||||
response.lines.add("cannot accept formula: ");
|
||||
}
|
||||
final Formula targetFormulaFinal = targetFormula;
|
||||
|
||||
Example ex = exampleFromUtterance(utt, session);
|
||||
response.ex = ex;
|
||||
|
|
@ -498,8 +499,8 @@ public class Master {
|
|||
Derivation match = null;
|
||||
for (int i = 0; i < ex.predDerivations.size(); i++) {
|
||||
Derivation derivi = ex.predDerivations.get(i);
|
||||
if (derivi.formula.equals(targetFormulaFinal)) {
|
||||
rank = i; match = derivi;
|
||||
if (targetFormulas.contains(derivi.formula)) {
|
||||
rank = i; match = derivi; break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -508,24 +509,25 @@ public class Master {
|
|||
response.stats.put("rank", rank);
|
||||
response.stats.put("status", GrammarInducer.getParseStatus(ex));
|
||||
response.stats.put("size", ex.predDerivations.size());
|
||||
response.stats.put("formulas.size", targetFormulas.size());
|
||||
response.stats.put("rank", rank);
|
||||
|
||||
response.stats.put("len_formula", targetFormula.toString().length());
|
||||
response.stats.put("len_formula", targetFormulas.get(0).toString().length());
|
||||
response.stats.put("len_utterance", ex.utterance.length());
|
||||
}
|
||||
|
||||
ex.setTargetFormula(targetFormula);
|
||||
if (match != null) {
|
||||
LogInfo.logs("Matched: %s", match);
|
||||
if (match!=null) {
|
||||
LogInfo.logs(":accept successful: %s", response.stats);
|
||||
|
||||
if (session.isWritingCitation()) {
|
||||
CitationTracker tracker = new CitationTracker(session.id, ex);
|
||||
tracker.citeAll(match);
|
||||
}
|
||||
|
||||
ex.setTargetValue(match.value); // this is just for logging, not actually used for learning
|
||||
// ex.setTargetValue(match.value); // this is just for logging, not actually used for learning
|
||||
if (session.isLearning()) {
|
||||
LogInfo.begin_track("Updating parameters");
|
||||
learner.onlineLearnExampleByFormula(ex);
|
||||
learner.onlineLearnExampleByFormula(ex, targetFormulas);
|
||||
LogInfo.end_track();
|
||||
}
|
||||
}
|
||||
|
|
@ -550,12 +552,14 @@ public class Master {
|
|||
|
||||
// write out the grammar
|
||||
if (session.isWritingGrammar()) {
|
||||
PrintWriter out = IOUtils.openOutAppendHard(Paths.get(Master.opts.newGrammarPath).toString());
|
||||
PrintWriter out = IOUtils.openOutAppendHard(Paths.get(Master.opts.intOutputPath, "grammar.log.json").toString());
|
||||
for (Rule rule : inducedRules) {
|
||||
out.println(rule.toJson());
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
} else {
|
||||
LogInfo.logs("No rule induced for head %s", head);
|
||||
}
|
||||
|
||||
if (session.isStatsing()) {
|
||||
|
|
@ -565,6 +569,16 @@ public class Master {
|
|||
} else {
|
||||
LogInfo.logs("Invalid format for def");
|
||||
}
|
||||
} else if (command.equals(":admin")) {
|
||||
if (!tree.child(1).value.equals("withcrappysecurity")) return;
|
||||
LogInfo.logs("Printing and overriding grammar and parameters...");
|
||||
builder.params.write(Paths.get(Master.opts.intOutputPath, "params.params").toString());
|
||||
PrintWriter out = IOUtils.openOutAppendHard(Paths.get(Master.opts.intOutputPath + "grammar.final.json").toString());
|
||||
for (Rule rule : builder.grammar.rules) {
|
||||
out.println(rule.toJson());
|
||||
}
|
||||
out.close();
|
||||
LogInfo.logs("Done printing and overriding grammar and parameters...");
|
||||
} else if (command.equals(":autocomplete")) {
|
||||
if (tree.children.size() == 2) {
|
||||
String prefix = tree.children.get(1).value;
|
||||
|
|
|
|||
|
|
@ -180,8 +180,8 @@ public abstract class Parser {
|
|||
Derivation.sortByScore(ex.predDerivations);
|
||||
|
||||
// Evaluate
|
||||
ex.evaluation = new Evaluation();
|
||||
addToEvaluation(state, ex.evaluation);
|
||||
// ex.evaluation = new Evaluation();
|
||||
// addToEvaluation(state, ex.evaluation);
|
||||
|
||||
// Clean up temporary state used during parsing
|
||||
ex.clearTempState();
|
||||
|
|
|
|||
|
|
@ -283,7 +283,6 @@ public abstract class ParserState {
|
|||
if (goodAndBad == null) return;
|
||||
}
|
||||
|
||||
boolean hasPostive = opts.customExpectedCounts != CustomExpectedCount.NONE;
|
||||
for (int i = 0; i < n; i++) {
|
||||
Derivation deriv = derivations.get(i);
|
||||
double logReward = Math.log(compatibilityToReward(deriv.compatibility));
|
||||
|
|
@ -292,7 +291,6 @@ public abstract class ParserState {
|
|||
case NONE:
|
||||
trueScores[i] = deriv.score + logReward;
|
||||
predScores[i] = deriv.score;
|
||||
if (logReward > Double.NEGATIVE_INFINITY) hasPostive = true;
|
||||
break;
|
||||
case UNIFORM:
|
||||
trueScores[i] = logReward;
|
||||
|
|
@ -308,13 +306,8 @@ public abstract class ParserState {
|
|||
}
|
||||
|
||||
// Usually this happens when there are no derivations.
|
||||
// sidaw: allow rejection updates of everything
|
||||
if (hasPostive) {
|
||||
if(!NumUtils.expNormalize(trueScores)) return;
|
||||
} else {
|
||||
for (int i = 0; i < n; i++) trueScores[i] = 0;
|
||||
}
|
||||
|
||||
if(!NumUtils.expNormalize(trueScores)) return;
|
||||
|
||||
if (!NumUtils.expNormalize(predScores)) return;
|
||||
|
||||
// Update parameters
|
||||
|
|
|
|||
|
|
@ -101,6 +101,8 @@ public class Rule {
|
|||
for (Pair<String, Double> p : info)
|
||||
tree.addChild(LispTree.proto.newList(p.getFirst(), "" + p.getSecond()));
|
||||
}
|
||||
if (source!=null)
|
||||
tree.addChild(source.toJson());
|
||||
return tree;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,15 +102,19 @@ public class ActionFeatureComputer implements FeatureComputer {
|
|||
|
||||
if (deriv.rule.source!=null) {
|
||||
deriv.addFeature(":stats", "cite", deriv.rule.source.cite);
|
||||
if (deriv.rule.source.cite > 0)
|
||||
deriv.addFeature(":stats", "has_cite");
|
||||
else
|
||||
deriv.addFeature(":stats", "no_cite");
|
||||
if (deriv.rule.source.cite > 0) deriv.addFeature(":stats", "has_cite");
|
||||
else deriv.addFeature(":stats", "no_cite");
|
||||
|
||||
|
||||
if (deriv.rule.source.self > 0) deriv.addFeature(":stats", "has_selfcite");
|
||||
else deriv.addFeature(":stats", "no_selfcite");
|
||||
|
||||
if (deriv.rule.source.align) deriv.addFeature(":stats", "align");
|
||||
else deriv.addFeature(":stats", "no_align");
|
||||
|
||||
if (deriv.rule.getInfoTag("simple_packing")!=-1.0) deriv.addFeature(":stats", "simple_packing");
|
||||
else deriv.addFeature(":stats", "no_simple_packing");
|
||||
|
||||
if (deriv.rule.source.self > 0)
|
||||
deriv.addFeature(":stats", "has_selfcite");
|
||||
else
|
||||
deriv.addFeature(":stats", "no_selfcite");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,9 +61,12 @@ public class CitationTracker {
|
|||
boolean selfcite = author.equals(uid);
|
||||
if (!selfcite) {
|
||||
summary.put("cite", (Integer)summary.get("cite") + 1);
|
||||
rule.source.cite++;
|
||||
} else {
|
||||
summary.put("self", (Integer)summary.get("self") + 1);
|
||||
rule.source.self++;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
summary = defaultMap(rule);
|
||||
e.printStackTrace();
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ public class DefinitionAligner {
|
|||
public int verbose = 0;
|
||||
|
||||
}
|
||||
public enum Strategies {SetExclusion, ExactExclusion};
|
||||
public enum Strategies {SetExclusion, ExactExclusion, cmdSet};
|
||||
public static Options opts = new Options();
|
||||
public class Match {
|
||||
@Override
|
||||
|
|
@ -75,7 +75,6 @@ public class DefinitionAligner {
|
|||
if (opts.verbose > 0) LogInfo.logs("DefinitionAligner.allMatches.size(): %d", aligner.allMatches.size());
|
||||
|
||||
for (int i = 0; i < aligner.allMatches.size() && i <= opts.maxMatches; i++) {
|
||||
// just take the top match
|
||||
Match match = aligner.allMatches.get(i);
|
||||
|
||||
List<Derivation> filteredList = chartList.stream()
|
||||
|
|
@ -87,7 +86,7 @@ public class DefinitionAligner {
|
|||
if (opts.verbose > 1) LogInfo.logs("DefinitionAligner.chartList.d: %s", d);
|
||||
return (d.start == match.start && d.end == match.end);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (opts.verbose > 1) LogInfo.logs("DefinitionAligner.Match: %s", match);
|
||||
if (opts.verbose > 1) LogInfo.logs("DefinitionAligner.currentParses: %s", currentParses);
|
||||
|
|
@ -144,6 +143,8 @@ public class DefinitionAligner {
|
|||
return true;
|
||||
if (opts.strategies.contains(Strategies.SetExclusion) && setExclusion(def, start, end))
|
||||
return true;
|
||||
if (opts.strategies.contains(Strategies.cmdSet) && cmdSet(def, start, end))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -168,7 +169,7 @@ public class DefinitionAligner {
|
|||
List<String> ret = new ArrayList<>();
|
||||
for (int i = lower; i < upper; i++) {
|
||||
if (i < 0 || i>=list.size())
|
||||
ret.add("(*)"); // a bit of a hack...
|
||||
ret.add("(*)");
|
||||
else ret.add(list.get(i));
|
||||
}
|
||||
return ret;
|
||||
|
|
@ -191,6 +192,16 @@ public class DefinitionAligner {
|
|||
return true;
|
||||
}
|
||||
|
||||
// exact match plus big
|
||||
private boolean cmdSet(Derivation def, int start, int end) {
|
||||
if (opts.verbose > 0) LogInfo.log("In exactPlusBig");
|
||||
// match only beginning and end
|
||||
boolean cmdSet = (end == headTokens.size()) && (start > 0) && def.end == defTokens.size() && def.start > 0;
|
||||
if (cmdSet && headTokens.subList(0, start).equals(defTokens.subList(0, start))) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,11 +63,12 @@ public class GrammarInducer {
|
|||
// induce rule is possible,
|
||||
// otherwise set the correct status
|
||||
public GrammarInducer(List<String> headTokens, Derivation def, List<Derivation> chartList) {
|
||||
|
||||
// grammarInfo start and end is used to indicate partial, when using aligner
|
||||
boolean allHead = false;
|
||||
if (def.grammarInfo.start == -1) {
|
||||
def.grammarInfo.start = 0;
|
||||
def.grammarInfo.end = headTokens.size();
|
||||
allHead = true;
|
||||
}
|
||||
|
||||
// dont want weird cat unary rules with strange semantics
|
||||
|
|
@ -86,25 +87,7 @@ public class GrammarInducer {
|
|||
|
||||
|
||||
inducedRules = new ArrayList<>();
|
||||
|
||||
if (opts.useBestPacking) {
|
||||
List<Derivation> bestPacking = bestPackingDP(this.matches, numTokens);
|
||||
|
||||
HashMap<String, String> formulaToCat = new HashMap<>();
|
||||
bestPacking.forEach(d -> formulaToCat.put(catFormulaKey(d), varName(d)));
|
||||
buildFormula(def, formulaToCat);
|
||||
inducedRules.addAll(induceRules(bestPacking, def));
|
||||
|
||||
if (opts.verbose > 1) {
|
||||
LogInfo.logs("chartList.size = %d", chartList.size());
|
||||
LogInfo.log("Potential packings: " );
|
||||
this.matches.forEach(d -> LogInfo.logs("%f: %s\t", d.getScore(), d.formula));
|
||||
LogInfo.logs("BestPacking: %s", bestPacking);
|
||||
LogInfo.logs("formulaToCat: %s", formulaToCat);
|
||||
}
|
||||
|
||||
}
|
||||
if (opts.useSimplePacking) {
|
||||
if (allHead && opts.useSimplePacking) {
|
||||
List<Derivation> filteredMatches = this.matches.stream().filter( d -> {
|
||||
return opts.simpleCats.contains(d.cat) && d.allAnchored() && d.end - d.start == 1;
|
||||
}).collect(Collectors.toList());
|
||||
|
|
@ -123,9 +106,10 @@ public class GrammarInducer {
|
|||
packing.forEach(d -> formulaToCat.put(catFormulaKey(d), varName(d)));
|
||||
buildFormula(def, formulaToCat);
|
||||
List<Rule> simpleInduced = induceRules(packing, def);
|
||||
for (Rule rule : simpleInduced)
|
||||
for (Rule rule : simpleInduced) {
|
||||
rule.addInfo("simple_packing", 1.0);
|
||||
addRuleDedupByRHS(simpleInduced);
|
||||
addRuleDedupByRHS(rule);
|
||||
}
|
||||
|
||||
if (opts.verbose > 1) {
|
||||
LogInfo.logs("Simple Packing", chartList.size());
|
||||
|
|
@ -136,16 +120,33 @@ public class GrammarInducer {
|
|||
LogInfo.logs("formulaToCat: %s", formulaToCat);
|
||||
}
|
||||
}
|
||||
if (opts.useBestPacking) {
|
||||
List<Derivation> bestPacking = bestPackingDP(this.matches, numTokens);
|
||||
|
||||
HashMap<String, String> formulaToCat = new HashMap<>();
|
||||
bestPacking.forEach(d -> formulaToCat.put(catFormulaKey(d), varName(d)));
|
||||
buildFormula(def, formulaToCat);
|
||||
for (Rule rule : induceRules(bestPacking, def)) {
|
||||
if (rule.rhs.stream().allMatch(s -> Rule.isCat(s))) continue;
|
||||
addRuleDedupByRHS(rule);
|
||||
}
|
||||
|
||||
if (opts.verbose > 1) {
|
||||
LogInfo.logs("chartList.size = %d", chartList.size());
|
||||
LogInfo.log("Potential packings: " );
|
||||
this.matches.forEach(d -> LogInfo.logs("%f: %s\t", d.getScore(), d.formula));
|
||||
LogInfo.logs("BestPacking: %s", bestPacking);
|
||||
LogInfo.logs("formulaToCat: %s", formulaToCat);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void addRuleDedupByRHS(List<Rule> rules) {
|
||||
Set<String> RHSs = new HashSet<>();
|
||||
for (Rule rule : inducedRules) {
|
||||
RHSs.add(rule.rhs.toString());
|
||||
}
|
||||
for (Rule rule : rules) {
|
||||
if (!RHSs.contains(rule.rhs.toString()))
|
||||
inducedRules.add(rule);
|
||||
Set<String> RHSs = new HashSet<>();
|
||||
private void addRuleDedupByRHS(Rule rule) {
|
||||
if (!RHSs.contains(rule.rhs.toString())) {
|
||||
inducedRules.add(rule);
|
||||
RHSs.add(rule.rhs.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -325,7 +326,6 @@ public class GrammarInducer {
|
|||
} else {
|
||||
deriv.grammarInfo.formula = deriv.formula;
|
||||
}
|
||||
|
||||
// LogInfo.logs("BUILT %s for %s", deriv.grammarInfo.formula, deriv.formula);
|
||||
// LogInfo.log("built " + deriv.grammarInfo.formula);
|
||||
}
|
||||
|
|
@ -354,6 +354,7 @@ public class GrammarInducer {
|
|||
|
||||
private SemanticFn getSemantics(final Derivation def, List<Derivation> packings) {
|
||||
Formula baseFormula = def.grammarInfo.formula;
|
||||
if (opts.verbose > 0) LogInfo.logs("getSemantics %s", baseFormula);
|
||||
if (packings.size() == 0) {
|
||||
SemanticFn constantFn = new ConstantFn();
|
||||
LispTree newTree = LispTree.proto.newList();
|
||||
|
|
|
|||
|
|
@ -301,7 +301,7 @@ public class GrammarInducerTest {
|
|||
@Test public void setsTest() {
|
||||
LogInfo.begin_track("setsTest");
|
||||
ParseTester T = new ParseTester();
|
||||
Assertion A = soft;
|
||||
Assertion A = hard;
|
||||
|
||||
T.def("remove those red blocks", d("remove has color red"));
|
||||
A.assertTrue(T.match("select those red blocks", d("select has color red")));
|
||||
|
|
|
|||
|
|
@ -94,10 +94,16 @@ public class Simulator implements Runnable {
|
|||
}
|
||||
|
||||
long endTime = System.nanoTime();
|
||||
LogInfo.logs("Took %d ns or %.4f s", (endTime - startTime), (endTime - startTime)/1.0e9);
|
||||
LogInfo.logs("Took %d ns or %.4f s", (endTime - startTime), (endTime - startTime)/1.0e9);
|
||||
}
|
||||
SimulationAnalyzer.flush();
|
||||
LogInfo.end_track();
|
||||
try {
|
||||
sempreQuery("(:admin withcrappysecurity)", "simulator");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String sempreQuery(String query, String sessionId) throws UnsupportedEncodingException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue