From 7eff08dd0fef0cb5d92cbcf76e5edd5bb84da04d Mon Sep 17 00:00:00 2001 From: Panupong Pasupat Date: Fri, 22 Apr 2016 17:02:11 -0700 Subject: [PATCH] Minor edits --- run | 2 +- .../nlp/sempre/ChildDerivationsGroup.java | 36 +++++++++++++++++++ .../DefaultDerivationPruningComputer.java | 11 +++--- .../stanford/nlp/sempre/DerivationGroup.java | 32 ----------------- .../stanford/nlp/sempre/DerivationPruner.java | 1 + .../stanford/nlp/sempre/FloatingParser.java | 18 +++++----- src/edu/stanford/nlp/sempre/SemanticFn.java | 2 +- 7 files changed, 54 insertions(+), 48 deletions(-) create mode 100644 src/edu/stanford/nlp/sempre/ChildDerivationsGroup.java delete mode 100644 src/edu/stanford/nlp/sempre/DerivationGroup.java diff --git a/run b/run index 8224db6..3769e89 100755 --- a/run +++ b/run @@ -814,7 +814,7 @@ end def tablesPruningStrategies [ # Formula - "singleton", + "atomic", "multipleSuperlatives", "sameMerge", "forwardBackward", diff --git a/src/edu/stanford/nlp/sempre/ChildDerivationsGroup.java b/src/edu/stanford/nlp/sempre/ChildDerivationsGroup.java new file mode 100644 index 0000000..c6ca9e0 --- /dev/null +++ b/src/edu/stanford/nlp/sempre/ChildDerivationsGroup.java @@ -0,0 +1,36 @@ +package edu.stanford.nlp.sempre; + +import java.util.List; + +/** + * A group containing one or two lists of potential child derivations. + * + * The motivation is to group potential child derivations based on type compatibility. + * For example, when building (and __ __), considering all pairs of derivations + * is time-wasting since a lot of pairs don't type-check. We instead group + * derivations by type, and only apply the rule to the pairs that type-check. + * + * This idea also extends to one-argument rules. For example, for (sum ___), + * we should only look at child derivations with number type. + * + * During parsing, for each DerivationGroup: + * - For a one-argument rule (derivations2 == null): + * Apply the rule on all derivations in derivations1 + * - For a two-argument rule (derivations2 != null): + * Apply the rule to all pairs (d1, d2) where d1 is in derivations1 and d2 is in derivations2 + * + * @author ppasupat + */ +public class ChildDerivationsGroup { + public final List derivations1, derivations2; + + public ChildDerivationsGroup(List derivations1) { + this.derivations1 = derivations1; + this.derivations2 = null; + } + + public ChildDerivationsGroup(List derivations1, List derivations2) { + this.derivations1 = derivations1; + this.derivations2 = derivations2; + } +} diff --git a/src/edu/stanford/nlp/sempre/DefaultDerivationPruningComputer.java b/src/edu/stanford/nlp/sempre/DefaultDerivationPruningComputer.java index 3399f67..1e0a73a 100644 --- a/src/edu/stanford/nlp/sempre/DefaultDerivationPruningComputer.java +++ b/src/edu/stanford/nlp/sempre/DefaultDerivationPruningComputer.java @@ -17,7 +17,7 @@ public class DefaultDerivationPruningComputer extends DerivationPruningComputer super(pruner); } - public static final String singleton = "singleton"; + public static final String atomic = "atomic"; public static final String emptyDenotation = "emptyDenotation"; public static final String nonLambdaError = "nonLambdaError"; public static final String tooManyValues = "tooManyValues"; @@ -30,7 +30,7 @@ public class DefaultDerivationPruningComputer extends DerivationPruningComputer @Override public Collection getAllStrategyNames() { return Arrays.asList( - singleton, + atomic, emptyDenotation, nonLambdaError, tooManyValues, doubleSummarizers, sameMerge, mistypedMerge, unsortedMerge, badSummarizerHead); } @@ -41,10 +41,11 @@ public class DefaultDerivationPruningComputer extends DerivationPruningComputer @Override public String isPrunedWithoutExecution(Derivation deriv) { - // singleton: Prune singleton formula at root. - if (containsStrategy(singleton)) { + // atomic: Prune atomic formula at root. + // e.g., Prevent "Who was taller, Lincoln or Obama" --> fb:en.lincoln generated from lexicon without any computation + if (containsStrategy(atomic)) { if (deriv.isRoot(pruner.ex.numTokens()) && deriv.formula instanceof ValueFormula) - return singleton; + return atomic; } return null; } diff --git a/src/edu/stanford/nlp/sempre/DerivationGroup.java b/src/edu/stanford/nlp/sempre/DerivationGroup.java deleted file mode 100644 index 0373b1b..0000000 --- a/src/edu/stanford/nlp/sempre/DerivationGroup.java +++ /dev/null @@ -1,32 +0,0 @@ -package edu.stanford.nlp.sempre; - -import java.util.List; - -/** - * Represents one or two lists of derivations. - * - * The motivation is to group derivations based on type compatibility. - * For example, when building (and __ __), considering all pairs of derivations - * is time-wasting since a lot of pairs don't type-check. We instead group - * derivations by type, and only apply the rule to the pairs that type-check. - * - * During parsing, for each DerivationGroup: - * - (if derivations2 == null) apply the rule on all derivations in derivations1 - * - (otherwise) apply the rule to all pairs (d1, d2) where d1 is in derivations1 - * and d2 is in derivations2 - * - * @author ppasupat - */ -public class DerivationGroup { - public final List derivations1, derivations2; - - public DerivationGroup(List derivations1) { - this.derivations1 = derivations1; - this.derivations2 = null; - } - - public DerivationGroup(List derivations1, List derivations2) { - this.derivations1 = derivations1; - this.derivations2 = derivations2; - } -} diff --git a/src/edu/stanford/nlp/sempre/DerivationPruner.java b/src/edu/stanford/nlp/sempre/DerivationPruner.java index 5266d3f..0ee296c 100644 --- a/src/edu/stanford/nlp/sempre/DerivationPruner.java +++ b/src/edu/stanford/nlp/sempre/DerivationPruner.java @@ -125,6 +125,7 @@ public class DerivationPruner { // Prune based on subformula boolean isPrunedRecursive(Derivation deriv) { if (!opts.recursivePruning) { + // If recursivePruning flag is turned off, only look at the outermost layer. if (opts.ensureExecuted) deriv.ensureExecuted(parser.executor, ex.context); String matchedStrategy; diff --git a/src/edu/stanford/nlp/sempre/FloatingParser.java b/src/edu/stanford/nlp/sempre/FloatingParser.java index 205a8e7..4f433f2 100644 --- a/src/edu/stanford/nlp/sempre/FloatingParser.java +++ b/src/edu/stanford/nlp/sempre/FloatingParser.java @@ -81,7 +81,7 @@ public class FloatingParser extends Parser { public int trainBeamSize = -1; @Option(gloss = "Whether to beta reduce the formula") public boolean betaReduce = false; - @Option(gloss = "DEBUG: Print amounts of time spent on each rule") + @Option(gloss = "DEBUG: Print amount of time spent on each rule") public boolean summarizeRuleTime = false; @Option(gloss = "Stop the parser if it has used more than this amount of time (in seconds)") public int maxFloatingParsingTime = 600; @@ -276,18 +276,18 @@ class FloatingParserState extends ParserState { * * The rule should be applied on all derivations (or all pairs of derivations) in each DerivationGroup. */ - private Collection getFilteredDerivations(Rule rule, Object cell1, Object cell2) { + private Collection getFilteredDerivations(Rule rule, Object cell1, Object cell2) { List derivations1 = getDerivations(cell1), derivations2 = (cell2 == null) ? null : getDerivations(cell2); if (!FloatingParser.opts.filterChildDerivations) - return Collections.singleton(new DerivationGroup(derivations1, derivations2)); + return Collections.singleton(new ChildDerivationsGroup(derivations1, derivations2)); // Try to filter down the number of partial logical forms if (rule.getSem().supportFilteringOnTypeData()) return rule.getSem().getFilteredDerivations(derivations1, derivations2); - return Collections.singleton(new DerivationGroup(derivations1, derivations2)); + return Collections.singleton(new ChildDerivationsGroup(derivations1, derivations2)); } - private Collection getFilteredDerivations(Rule rule, Object cell) { + private Collection getFilteredDerivations(Rule rule, Object cell) { return getFilteredDerivations(rule, cell, null); } @@ -386,7 +386,7 @@ class FloatingParserState extends ParserState { derivLoop: for (int depth1 = 0; depth1 < depth; depth1++) { // sizes must add up to depth-1 (actually size-1) int depth2 = depth - 1 - depth1; - for (DerivationGroup group : getFilteredDerivations(rule, floatingCell(rhs1, depth1), floatingCell(rhs2, depth2))) + for (ChildDerivationsGroup group : getFilteredDerivations(rule, floatingCell(rhs1, depth1), floatingCell(rhs2, depth2))) for (Derivation deriv1 : group.derivations1) for (Derivation deriv2 : group.derivations2) if (!applyFloatingRule(rule, depth, deriv1, deriv2, deriv1.canonicalUtterance + " " + deriv2.canonicalUtterance)) @@ -396,7 +396,7 @@ class FloatingParserState extends ParserState { { derivLoop: for (int subDepth = 0; subDepth < depth; subDepth++) { // depth-1 <=depth-1 - for (DerivationGroup group : getFilteredDerivations(rule, floatingCell(rhs1, depth - 1), floatingCell(rhs2, subDepth))) + for (ChildDerivationsGroup group : getFilteredDerivations(rule, floatingCell(rhs1, depth - 1), floatingCell(rhs2, subDepth))) for (Derivation deriv1 : group.derivations1) for (Derivation deriv2 : group.derivations2) if (!applyFloatingRule(rule, depth, deriv1, deriv2, deriv1.canonicalUtterance + " " + deriv2.canonicalUtterance)) @@ -406,7 +406,7 @@ class FloatingParserState extends ParserState { { derivLoop: for (int subDepth = 0; subDepth < depth - 1; subDepth++) { // getFilteredDerivations( + public Collection getFilteredDerivations( List derivations1, List derivations2) { throw new UnsupportedOperationException(); }