edu.cornell.cs.nlp.spf.mr.lambda.Lambda Java Examples
The following examples show how to use
edu.cornell.cs.nlp.spf.mr.lambda.Lambda.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MakeApplicationSplits.java From spf with GNU General Public License v2.0 | 6 votes |
/** * Replace subExpression in originalExpression with replacementExpression * and wrap the result with a Lambda operator with the variable newVariable. * * @param originalExpression * @param subExpression * Assumed to be a sub-expression of originalExpression. * @param replacementExpression * @param newVariable * replacementExpression already contains this variable. * @param index * if 'null', will replace all occurrences of subExpression in * originalExpression. Otherwise will replace the n-th occurrence * only. * @return */ private static LogicalExpression createFunctor( LogicalExpression originalExpression, LogicalExpression subExpression, LogicalExpression replacementExpression, Variable newVariable, Integer index) { final LogicalExpression newBody; if (index == null) { // Case replace all newBody = ReplaceExpression.of(originalExpression, subExpression, replacementExpression); } else { // Case replace only a single occurrence newBody = ReplaceNthExpression.of(originalExpression, subExpression, replacementExpression, index); } return new Lambda(newVariable, newBody); }
Example #2
Source File: FactoringServices.java From spf with GNU General Public License v2.0 | 6 votes |
@Override public void visit(Lambda lambda) { // not visiting argument, since we are only abstracting constants. lambda.getBody().accept(this); final ListIterator<Pair<Placeholders, ? extends LogicalExpression>> iterator = tempReturn .listIterator(); while (iterator.hasNext()) { final Pair<Placeholders, ? extends LogicalExpression> pair = iterator .next(); if (pair.second() != null) { final LogicalExpression newBody = pair.second(); if (newBody == lambda.getBody()) { iterator.set(Pair.of(pair.first(), lambda)); } else { iterator.set(Pair.of(pair.first(), new Lambda(lambda.getArgument(), newBody))); } } } }
Example #3
Source File: GetMaxEvaluations.java From amr with GNU General Public License v2.0 | 6 votes |
@Override public void visit(LambdaNode node) { node.getArgument().accept(this); final List<Pair<LogicalExpression, Map<INode, LogicalExpression>>> variables = maxes; node.getBody().accept(this); if (maxes.isEmpty() || variables.isEmpty() || variables.size() * maxes.size() > limit) { // Case no logical expressions to generate or too many. LOG.debug("Too many evaluations for: %s", node); maxes = Collections.emptyList(); } else { final List<Pair<LogicalExpression, Map<INode, LogicalExpression>>> maxLambdas = new LinkedList<>(); for (final Pair<LogicalExpression, Map<INode, LogicalExpression>> variable : variables) { for (final Pair<LogicalExpression, Map<INode, LogicalExpression>> body : maxes) { body.second().putAll(variable.second()); maxLambdas.add(Pair.of( new Lambda((Variable) variable.first(), body .first()), body.second())); } } maxes = maxLambdas; } }
Example #4
Source File: InstanceCloneTest.java From amr with GNU General Public License v2.0 | 6 votes |
@Test public void test() { final LogicalExpression target = TestServices.getCategoryServices() .readSemantics("(lambda $0:e (boo:<e,t> $0))"); final LogicalExpression source = TestServices.getCategoryServices() .readSemantics("(lambda $0:e (boo:<e,t> $0))"); final LogicalExpression result = InstanceClone.of(target, source); assertEquals(source, result); assertNotEquals(((Lambda) source).getArgument(), ((Lambda) target).getArgument()); assertNotEquals(((Lambda) source).getArgument(), ((Lambda) result).getArgument()); assertEquals(((Lambda) result).getArgument(), ((Lambda) target).getArgument()); }
Example #5
Source File: InstanceCloneTest.java From amr with GNU General Public License v2.0 | 6 votes |
@Test public void test2() { final LogicalExpression target = TestServices .getCategoryServices() .readSemantics( "(lambda $0:e (and:<t*,t> (foo:<e,<e,t>> (ref:<id,e> na:id) $0) (boo:<e,t> $0)))"); final LogicalExpression source = TestServices .getCategoryServices() .readSemantics( "(lambda $0:e (and:<t*,t> (boo:<e,t> $0) (foo:<e,<e,t>> (ref:<id,e> !1) $0)))"); final LogicalExpression result = InstanceClone.of(target, source); assertEquals(source, result); assertNotEquals(((Lambda) source).getArgument(), ((Lambda) target).getArgument()); assertNotEquals(((Lambda) source).getArgument(), ((Lambda) result).getArgument()); assertEquals(((Lambda) result).getArgument(), ((Lambda) target).getArgument()); }
Example #6
Source File: ExtractTypedSubExpressionTest.java From amr with GNU General Public License v2.0 | 6 votes |
@Test public void test4() { final LogicalExpression exp = TestServices .getCategoryServices() .readSemantics( "(lambda $0:<e,t> (lambda $1:e (and:<t*,t> (do:<e,<e,<e,t>>> roo:e $1 (a:<<e,t>,e> (lambda $0:e (boo:<e,<e,t>> $0 too:e)))) ($0 $1) (boo:<e,<e,t>> $1 goo:e))))"); final Result result = ExtractTypedSubExpression.of(exp, LogicalConstant .read("p:t"), LogicLanguageServices.getTypeRepository() .getTypeCreateIfNeeded("t"), false); Assert.assertNotNull(result); final LogicalExpression expectedSubExp = ((Lambda) ((Lambda) exp) .getBody()).getBody(); final LogicalExpression expectedRemainder = TestServices .getCategoryServices().readSemantics( "(lambda $0:<e,t> (lambda $1:e p:t))"); Assert.assertEquals(expectedRemainder, result.getExpressionWithPlaceholder()); Assert.assertEquals(expectedSubExp, result.getExtractedSubExpression()); }
Example #7
Source File: ExtractTypedSubExpressionTest.java From amr with GNU General Public License v2.0 | 6 votes |
@Test public void test6() { final LogicalExpression exp = TestServices .getCategoryServices() .readSemantics( "(lambda $0:<e,t> (lambda $1:e (and:<t*,t> ($0 $1) (boo:<e,<e,t>> $1 goo:e))))"); final Result result = ExtractTypedSubExpression.of(exp, LogicalConstant .read("p:t"), LogicLanguageServices.getTypeRepository() .getTypeCreateIfNeeded("t"), true); Assert.assertNotNull(result); final LogicalExpression expectedSubExp = ((Literal) ((Lambda) ((Lambda) exp) .getBody()).getBody()).getArg(1); final LogicalExpression expectedRemainder = TestServices .getCategoryServices() .readSemantics( "(lambda $0:<e,t> (lambda $1:e (and:<t*,t> ($0 $1) p:t)))"); Assert.assertEquals(expectedRemainder, result.getExpressionWithPlaceholder()); Assert.assertEquals(expectedSubExp, result.getExtractedSubExpression()); }
Example #8
Source File: Evaluation.java From spf with GNU General Public License v2.0 | 6 votes |
/** * Decomposes a logical expression as a SELECT query. * * @param exp * @return Pair of queried variables and SELECT body. If not a SELECT query, * returns null. */ private static Pair<List<Variable>, LogicalExpression> decomposeLogicalExpressionAsSelect( LogicalExpression exp) { LogicalExpression currentBody = exp; final List<Variable> queryVariables = new LinkedList<Variable>(); while (currentBody instanceof Lambda) { final Lambda lambda = (Lambda) currentBody; if (lambda.getArgument().getType().isComplex()) { // Case argument is complex return null; } else { queryVariables.add(lambda.getArgument()); currentBody = lambda.getBody(); } } if (currentBody.getType().isComplex()) { return null; } else { return Pair.of(queryVariables, currentBody); } }
Example #9
Source File: LogicalExpressionCoordinationServices.java From spf with GNU General Public License v2.0 | 6 votes |
@Override public LogicalExpression createPartialCoordination( LogicalExpression coordinated, LogicalExpression coordinator) { // Create a binary predicate from coordinator if (!isBaseCoordinator(coordinator)) { return null; } // The type of the coordinated element is generalized to create the // coordination predicate final Type argType = LogicLanguageServices.getTypeRepository() .generalizeType(coordinated.getType()); final LogicalConstant coordinationPredicate = createPredicate( (LogicalConstant) coordinator, 2, argType); // Create a literal using the predicate, with a variable as the // first argument and 'coordinated' as the second, and wrap the literal // with a lambda expression binding the varaible. final LogicalExpression[] arguments = new LogicalExpression[2]; final Variable variable = new Variable(argType); arguments[0] = variable; arguments[1] = coordinated; return new Lambda(variable, new Literal(coordinationPredicate, arguments)); }
Example #10
Source File: GetAmrSubExpressions.java From amr with GNU General Public License v2.0 | 6 votes |
/** * For a given expression, create new expressions by potentially extracting * out a single skolem term and wrapping with a {@link Lambda} term. Only * extract entities from {@link Lambda} terms. For any other expression, * simply return a singleton set with that expression. */ private static Set<LogicalExpression> extractEntities( LogicalExpression expression) { final Set<LogicalExpression> entities = GetAllSkolemTerms.of( expression, true); final Set<LogicalExpression> expressions = new HashSet<>(); expressions.add(expression); if (expression instanceof Lambda && ((Lambda) expression) .getComplexType() .getRange() .equals(LogicLanguageServices.getTypeRepository() .getTruthValueType())) { for (final LogicalExpression entity : entities) { final Variable variable = new Variable(entity.getType()); expressions.add(new Lambda(variable, ReplaceExpression.of( expression, entity, variable))); } } return expressions; }
Example #11
Source File: SupervisedFilterFactory.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { lambda.getArgument().accept(this); if (isValid) { lambda.getBody().accept(this); } }
Example #12
Source File: AToExists.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { lambda.getBody().accept(this); if (result.first() != lambda.getBody()) { // Case body changed result = Pair.of(new Lambda(lambda.getArgument(), result.first()), result.second()); } else { result = Pair.of(lambda, result.second()); } }
Example #13
Source File: ApplyAndSimplify.java From spf with GNU General Public License v2.0 | 5 votes |
/** * Does apply-and-simplify without replacing any free variables. This method * should be used with extreme caution. */ static LogicalExpression ofUnsafe(LogicalExpression func, LogicalExpression arg) { // Verify type matching. The functor must be have a complex type, and // need to be in some kind of parent-child relationship with the // argument, as we allow flexible typing syntax-wise. if (!func.getType().isComplex() || !LogicLanguageServices.getTypeComparator().verifyArgType( func.getType().getDomain(), arg.getType())) { // Case typing mismatch return null; } else if (func instanceof Lambda) { // Case the functor is a Lambda expression final Lambda lambda = (Lambda) func; final Variable variable = lambda.getArgument(); final ApplyAndSimplify visitor = new ApplyAndSimplify(arg, variable); visitor.visit(lambda.getBody()); return visitor.result; } else if (func instanceof Literal) { // Case the functor is a literal, append the argument to // the end of the arguments list return Simplify.of(literalApplication((Literal) func, arg)); } else if (func instanceof Term) { // Case the functor is a variable or logical constant, // create the a literal with the functor as predicate and the // argument as the only argument in the argument list return Simplify.of(termApplication((Term) func, arg)); } else { // Should never happen throw new LogicalExpressionRuntimeException( "Impossible condition: un-handled logical expression object"); } }
Example #14
Source File: IsValidAmr.java From amr with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { lambda.getArgument().accept(this); if (isValid) { lambda.getBody().accept(this); } }
Example #15
Source File: ApplyAndSimplify.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { if (lambda.containsFreeVariable(rootVariable)) { super.visit(lambda); } else { result = lambda; } }
Example #16
Source File: AToExists.java From spf with GNU General Public License v2.0 | 5 votes |
private static Lambda makeEntityToTruthLambda(LogicalExpression exp) { if (exp instanceof Lambda && isEntityToTruthType(exp.getType())) { return (Lambda) exp; } else { return null; } }
Example #17
Source File: GetApplicationArgumentTest.java From spf with GNU General Public License v2.0 | 5 votes |
@Test public void testCreateArg3() { final LogicalExpression resultSubExp = TestServices .getCategoryServices().readSemantics("(pred:<e,t> boo:e)"); final LogicalExpression function = TestServices.getCategoryServices() .readSemantics("(lambda $0:<e,t> ($0 boo:e))"); final Variable applicationArg = ((Lambda) function).getArgument(); final LogicalExpression expected = TestServices.getCategoryServices() .readSemantics("pred:<e,t>"); Assert.assertEquals(expected, GetApplicationArgument.createArgument(resultSubExp, ((Lambda) function).getBody(), applicationArg, new ScopeMapping<Variable, Variable>())); }
Example #18
Source File: DummyEntityServices.java From amr with GNU General Public License v2.0 | 5 votes |
/** * Given a skolem term (i.e., (a:<<e,t>,<id,e>> na:id (lambda $0:e * (and:<t*,t> .... (pred:<e,<e,t>> $0 DUMMY:e) .... )))), removes the * literal that includes the DUMMY:e entity as second argument, and returns * its predicate. If the dummy entity appears more than once, fail. * * @return Pair of stripped skolem term (in first position) and the * predicate of the relation with the stripped dummy argument * (second position). */ public static Pair<Literal, LogicalConstant> stripDummy(Literal literal) { if (AMRServices.isSkolemTerm(literal) && literal.numArgs() == 2 && literal.getArg(1) instanceof Lambda) { final Pair<Lambda, LogicalConstant> pair = stripDummy((Lambda) literal .getArg(1)); if (pair != null) { return Pair.of(AMRServices.skolemize(pair.first()), pair.second()); } } return null; }
Example #19
Source File: LogicalExpressionToIndentedString.java From spf with GNU General Public License v2.0 | 5 votes |
@Override final public void visit(Lambda lambda) { outputString.append("(lambda "); lambda.getArgument().accept(this); outputString.append(' '); lambda.getBody().accept(this); outputString.append(')'); }
Example #20
Source File: AMRSupervisedFilter.java From amr with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { lambda.getArgument().accept(this); if (isValid) { lambda.getBody().accept(this); } }
Example #21
Source File: LogicalExpressionToString.java From spf with GNU General Public License v2.0 | 5 votes |
@Override final public void visit(Lambda lambda) { outputString.append("(lambda "); processVariable(lambda.getArgument(), true); outputString.append(' '); lambda.getBody().accept(this); outputString.append(')'); // Pop the variable name. variableIds.pop(lambda.getArgument()); }
Example #22
Source File: LogicalExpressionToLatexString.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { outputString.append("\\lambda "); lambda.getArgument().accept(this); outputString.append(". "); lambda.getBody().accept(this); }
Example #23
Source File: SententialAdverbialTypeShifting.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public Set<Category<LogicalExpression>> reverseApply( Category<LogicalExpression> result, SentenceSpan span) { final Unification syntaxUnification = result.getSyntax().unify( S_FS_AP_SYNTAX); if (result instanceof ComplexCategory && syntaxUnification != null) { // Create an argument category with semantics of (lambda $0:x // true:t). Consuming this as argument and simplifying should // reverse the shifting safely (and somewhat efficiently). final LogicalExpression semantics = result.getSemantics(); if (semantics instanceof Lambda) { final Variable argument = ((Lambda) semantics).getArgument(); if (argument.getType().isComplex() && LogicLanguageServices.getTypeRepository() .getTruthValueType() .equals(argument.getType().getRange())) { final LogicalExpression reversedSemantics = categoryServices .apply(semantics, new Lambda(new Variable(argument .getType().getDomain()), LogicLanguageServices.getTrue())); if (reversedSemantics != null) { return SetUtils.createSingleton(Category.create( ((ComplexSyntax) syntaxUnification .getUnifiedSyntax()).getLeft(), reversedSemantics)); } } } } return Collections.emptySet(); }
Example #24
Source File: CountVariables.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { final boolean removed; if (counted.contains(lambda.getArgument())) { counted.remove(lambda.getArgument()); removed = true; } else { removed = false; } lambda.getArgument().accept(this); lambda.getBody().accept(this); if (!removed) { counted.remove(lambda.getArgument()); } }
Example #25
Source File: AbstractTypeRaising.java From spf with GNU General Public License v2.0 | 5 votes |
private static LogicalExpression raiseSemantics(LogicalExpression sem, Type finalResultSemanticType) { final Variable variable = new Variable(LogicLanguageServices .getTypeRepository().getTypeCreateIfNeeded( LogicLanguageServices.getTypeRepository() .generalizeType(finalResultSemanticType), LogicLanguageServices.getTypeRepository() .generalizeType(sem.getType()))); return new Lambda(variable, new Literal(variable, ArrayUtils.create(sem))); }
Example #26
Source File: MakeCompositionSplits.java From spf with GNU General Public License v2.0 | 5 votes |
/** * Extract the entire subExpression * * @param subExpression * @param argumentOrder * The free variables in subExpression except the first variables * in originalLambda, which is assumed to be in subExpression as * well. * @return */ private SplittingPair doSplit(LogicalExpression subExpression, List<Variable> argumentOrder) { // The h expression final Variable rootArg = originalLambda.getArgument(); // Create variables for the objects we will pull out final List<Variable> newVars = new LinkedList<Variable>(); // The variable x, such that h = \lambda x f(g(x)) newVars.add(rootArg); // The rest of the arguments to the function g newVars.addAll(argumentOrder); // Create g, such that h = \lambda x f(g(x)) final LogicalExpression g = SplittingServices.makeExpression(newVars, subExpression); // Create f, such that h = \lambda x f(g(x)) newVars.remove(rootArg); final Variable compositionArgument = new Variable(LogicLanguageServices .getTypeRepository().generalizeType(g.getType().getRange())); final LogicalExpression embeddedApplication = SplittingServices .makeAssignment(newVars, compositionArgument); final LogicalExpression newBody = ReplaceExpression.of( originalLambda.getBody(), subExpression, embeddedApplication); final Lambda f = new Lambda(compositionArgument, newBody); // Verify that f has no free arguments. Can happen if rootArg appears in // another part of the expression. if (GetAllFreeVariables.of(f).size() == 0) { // Create the splitting pair and return it return createSplittingPair(f, g); } else { return null; } }
Example #27
Source File: IsTypeConsistent.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public void visit(Lambda lambda) { // Record this variable to test its references. variableTypes.put(lambda.getArgument(), lambda.getArgument().getType()); // Visit the body. lambda.getBody().accept(this); // Remove the variable from the mapping, since we are leaving its scope. variableTypes.remove(lambda.getArgument()); }
Example #28
Source File: MakeCompositionSplits.java From spf with GNU General Public License v2.0 | 5 votes |
/** * Usage only through static 'of' method. * * @param originalCategory * @param originalLambda * @param categoryServices */ private MakeCompositionSplits( ComplexCategory<LogicalExpression> originalCategory, Lambda originalLambda, ICategoryServices<LogicalExpression> categoryServices) { this.originalCategory = originalCategory; this.originalLambda = originalLambda; this.categoryServices = categoryServices; }
Example #29
Source File: MakeCompositionSplits.java From spf with GNU General Public License v2.0 | 5 votes |
/** * Create the empty split for the given category. * * @param originalCategory * Assumed to be a complex category with a complex typed logical * expression. * @param categoryServices * @return */ private static SplittingPair doEmptySplit( ComplexCategory<LogicalExpression> originalCategory, ICategoryServices<LogicalExpression> categoryServices) { // Create the unity function logical expression (lambda x x) final Variable unityVariable = new Variable(LogicLanguageServices .getTypeRepository().generalizeType( originalCategory.getSemantics().getType().getDomain())); final LogicalExpression unityFunction = new Lambda(unityVariable, unityVariable); // Create the X/X category with the unity function as its semantics final Syntax originalCategoryDomain = originalCategory.getSyntax() .getRight(); final Slash originalSlash = originalCategory.getSlash(); final ComplexCategory<LogicalExpression> newCategory = new ComplexCategory<LogicalExpression>( new ComplexSyntax(originalCategoryDomain, originalCategoryDomain, originalSlash), unityFunction); // Create the split. Don't allow crossing composition. final SplittingPair split; if (originalSlash == Slash.BACKWARD) { // Cator goes on right split = new SplittingPair(newCategory, originalCategory); } else { // Cator goes on left split = new SplittingPair(originalCategory, newCategory); } // Test the split final Category<LogicalExpression> newRoot = categoryServices.compose( originalCategory, newCategory, 1, false); if (!originalCategory.equals(newRoot)) { LOG.error("ERROR: error in Cat composition split"); LOG.error("%s --> %s != $s", split, newRoot, originalCategory); } return split; }
Example #30
Source File: MakeCompositionSplits.java From spf with GNU General Public License v2.0 | 5 votes |
static Set<SplittingPair> of(Category<LogicalExpression> originalCategory, ICategoryServices<LogicalExpression> categoryServices) { // Check if can split if (!(originalCategory instanceof ComplexCategory) || !(originalCategory.getSemantics() instanceof Lambda)) { // Case not a function, can't split return Collections.emptySet(); } final ComplexCategory<LogicalExpression> complexCategory = (ComplexCategory<LogicalExpression>) originalCategory; if (complexCategory.getSlash() == Slash.VERTICAL) { // Case the category has a vertical slash, so can't split it return Collections.emptySet(); } // Create the visitor and visit the logical expression final MakeCompositionSplits visitor = new MakeCompositionSplits( complexCategory, (Lambda) complexCategory.getSemantics(), categoryServices); visitor.visit(originalCategory.getSemantics()); // Empty split visitor.splits.add(doEmptySplit(complexCategory, categoryServices)); return visitor.splits; }