Java Code Examples for edu.cornell.cs.nlp.spf.mr.lambda.LogicalExpression#getType()

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.LogicalExpression#getType() . 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: GetAmrSubExpressions.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 2
Source File: LogicalExpressionCoordinationServices.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
private boolean isCoordinator(LogicalExpression predicate, int numArgs) {
	if (predicate instanceof LogicalConstant
			&& (isConjunctionCoordinator((LogicalConstant) predicate) || isDisjunctionCoordinator((LogicalConstant) predicate))
			&& predicate.getType() instanceof ComplexType) {
		final ComplexType predicateType = (ComplexType) predicate.getType();
		Type current = predicateType;
		int count = 0;
		while (current instanceof ComplexType) {
			++count;
			current = ((ComplexType) current).getRange();
		}
		return count == numArgs;
	} else {
		return false;
	}
}
 
Example 3
Source File: TreeTransformer.java    From UDepLambda with Apache License 2.0 5 votes vote down vote up
private static LogicalExpression bindOperation(
    LogicalExpression leftTreeParse, LogicalExpression rightTreeParse) {
  Preconditions.checkNotNull(leftTreeParse);
  Preconditions.checkNotNull(rightTreeParse);
  Preconditions.checkArgument(rightTreeParse instanceof LogicalConstant);

  // Dirty implementation. A better way is to work with objects and not the
  // strings.
  Type leftType = leftTreeParse.getType();
  String existsExpression =
      String.format("(lambda $f:%s (exists:<%s,<%s,%s>> $x:%s ($f $x))",
          leftType, leftType.getDomain(), leftType.getRange(),
          leftType.getRange(), leftType.getDomain());
  leftTreeParse =
      ApplyAndSimplify
          .of(SimpleLogicalExpressionReader.read(existsExpression),
              leftTreeParse);

  String variable = rightTreeParse.toString();
  String leftParse = leftTreeParse.toString();
  String variableType = rightTreeParse.getType().getDomain().toString();
  String returnType = rightTreeParse.getType().getRange().toString();
  Pattern variablePattern =
      Pattern.compile(String.format("([\\(\\)\\s])(%s)([\\(\\)\\s])",
          variable));
  Matcher matcher = variablePattern.matcher(leftParse);

  String finalString =
      matcher
          .replaceAll(String.format("$1%s:<%s,<%s,%s>> \\$x$3",
              PredicateKeys.EQUAL_PREFIX, variableType, variableType,
              returnType));
  finalString = String.format("(lambda $x:%s %s)", variableType, finalString);
  return SimpleLogicalExpressionReader.read(finalString);
}
 
Example 4
Source File: CoordinationServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an AMR coordination, e.g., (lambda $0:e (and:<t*,t> (C:<e,t> $0)
 * (c_op1:<e,<e,t>> $0 a1) ... (c_opn:<e,<e,t>> $0 an))).
 *
 * @param coordinatedElements
 *            The entities being coordinated a1,...,an, where the type of
 *            each ai is baseType.
 * @param baseType
 *            The type of all coordinated entities.
 * @param coordinationWrapper
 *            The wrapping predicate that packed this coordination, as
 *            initialized by {@link CoordinationC1Rule}. C is inferred from
 *            this wrapper.
 * @return <e,t>-typed logical expression.
 */
public static LogicalExpression createCoordination(
		LogicalExpression[] coordinatedElements, Type baseType,
		LogicalConstant coordinationWrapper) {
	// Create coordination variable.
	final Variable coordinationVariable = new Variable(
			LogicLanguageServices.getTypeRepository().getEntityType());

	final LogicalExpression[] conjunctionElements = new LogicalExpression[coordinatedElements.length + 1];

	// Wrap each coordinated element with a c_op predicate and get the
	// common type.
	Type commonType = null;
	for (int i = 0; i < coordinatedElements.length; ++i) {
		final LogicalExpression element = coordinatedElements[i];
		if (commonType == null || element.getType().isExtending(commonType)) {
			commonType = element.getType();
		} else if (!commonType.isExtending(element.getType())) {
			return null;
		}
		conjunctionElements[i + 1] = new Literal(createpCOpPredicate(i + 1,
				baseType), ArrayUtils.create(coordinationVariable, element));
	}

	/*
	 * Create the coordination instance, the conjunction of all the c_op
	 * literals and the instance unary literal.
	 */
	final LogicalConstant predicate = getCoordinationPredicate(
			coordinationWrapper.getBaseName(), commonType);
	if (predicate == null) {
		return null;
	}
	conjunctionElements[0] = new Literal(predicate,
			ArrayUtils.create(coordinationVariable));

	return new Lambda(coordinationVariable, new Literal(
			LogicLanguageServices.getConjunctionPredicate(),
			conjunctionElements));
}
 
Example 5
Source File: GetApplicationFunction.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
private GetApplicationFunction(LogicalExpression applicationArgument,
		int maxSubsetSize, int maxDepth) {
	// Use via 'of' function.
	this.applicationArgument = applicationArgument;
	this.maxSubsetSize = maxSubsetSize;
	this.maxDepth = maxDepth;
	this.applicationVariable = new Variable(applicationArgument.getType());
}
 
Example 6
Source File: AbstractCoordinationCXRaisedApply.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
protected Category<LogicalExpression> doApply(
		ComplexCategory<LogicalExpression> function,
		Category<LogicalExpression> coordiantedArguments) {
	if (coordiantedArguments.getSyntax() instanceof CoordinationSyntax
			&& function
					.getSyntax()
					.getRight()
					.equals(((CoordinationSyntax) coordiantedArguments
							.getSyntax()).getCoordinatedSyntax())
			&& coordiantedArguments.getSemantics() instanceof Literal
			&& ((Literal) coordiantedArguments.getSemantics())
					.getPredicate() instanceof LogicalConstant
			&& ((Literal) coordiantedArguments.getSemantics()).numArgs() >= 2) {
		// Get all the arguments, apply the function to each one of them,
		// and try to coordinate them. The output syntax is the functor
		// syntax following the application.
		final Literal coordinationLiteral = (Literal) coordiantedArguments
				.getSemantics();
		LogicalExpression arg0 = null;
		final int coordLiteralNumArgs = coordinationLiteral.numArgs();
		final LogicalExpression[] appliedArgs = new LogicalExpression[coordLiteralNumArgs - 1];
		// Accumulate all variables to make sure we don't duplicate any.
		Type argType = null;
		final LogicalExpression func = ReplaceFreeVariablesIfPresent.of(
				function.getSemantics(),
				coordinationLiteral.getFreeVariables());
		for (int i = 0; i < coordLiteralNumArgs; ++i) {
			final LogicalExpression arg = coordinationLiteral.getArg(i);
			final LogicalExpression appliedArg = categoryServices.apply(
					func, arg);
			if (appliedArg == null) {
				return null;
			} else {
				if (i == 0) {
					arg0 = appliedArg;
				} else {
					appliedArgs[i - 1] = appliedArg;
				}
				if (argType == null
						|| appliedArg.getType().isExtending(argType)) {
					argType = appliedArg.getType();
				}
			}
		}
		return cxRule.doApply(arg0, CoordinationServices.createLiteral(
				((LogicalConstant) coordinationLiteral.getPredicate())
						.getBaseName(), appliedArgs, argType), function
				.getSyntax().getLeft(),
				(LogicalConstant) ((Literal) coordiantedArguments
						.getSemantics()).getPredicate());
	}
	return null;
}
 
Example 7
Source File: SententialAdverbialTypeShifting.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
/**
 * (lambda $0:x (g $0)) ==> (lambda $0:<x,t> (lambda $1:x (and:<t*,t> ($0
 * $1) (g $1))))
 */
protected LogicalExpression typeShiftSemantics(LogicalExpression sem) {
	final Type semType = sem.getType();
	final Type range = semType.getRange();

	if (semType.isComplex()
			&& range.equals(LogicLanguageServices.getTypeRepository()
					.getTruthValueType())) {

		// Make sure the expression is wrapped with lambda operators, since
		// the variables are required
		final Lambda lambda = (Lambda) sem;

		// Variable for the new outer lambda
		final Variable outerVariable = new Variable(LogicLanguageServices
				.getTypeRepository().getTypeCreateIfNeeded(
						LogicLanguageServices.getTypeRepository()
								.getTruthValueType(),
						lambda.getArgument().getType()));

		// Create the literal applying the function to the original
		// argument
		final LogicalExpression[] args = new LogicalExpression[1];
		args[0] = lambda.getArgument();
		final Literal newLiteral = new Literal(outerVariable, args);

		// Create the conjunction of newLitral and the original body
		final Literal conjunction = new Literal(
				LogicLanguageServices.getConjunctionPredicate(),
				ArrayUtils.create(newLiteral, lambda.getBody()));

		// The new inner lambda
		final Lambda innerLambda = new Lambda(lambda.getArgument(),
				conjunction);

		// The new outer lambda
		final Lambda outerLambda = new Lambda(outerVariable, innerLambda);

		// Simplify the output and return it
		final LogicalExpression ret = Simplify.of(outerLambda);

		return ret;
	}

	return null;
}
 
Example 8
Source File: AbstractShiftingRule.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
/**
 * (lambda $0:x (g $0)) ==> (lambda $0:<x,t> (lambda $1:x (and:<t*,t> ($0
 * $1) (g $1))))
 */
protected LogicalExpression typeShiftSemantics(LogicalExpression sem) {
	final Type semType = sem.getType();
	final Type range = semType.getRange();

	if (semType.isComplex() && range.equals(LogicLanguageServices
			.getTypeRepository().getTruthValueType())) {

		// Make sure the expression is wrapped with lambda operators, since
		// the variables are required
		final Lambda lambda = (Lambda) sem;

		// Variable for the new outer lambda
		final Variable outerVariable = new Variable(LogicLanguageServices
				.getTypeRepository().getTypeCreateIfNeeded(
						LogicLanguageServices.getTypeRepository()
								.getTruthValueType(),
						lambda.getArgument().getType()));

		// Create the literal applying the function to the original
		// argument
		final LogicalExpression[] args = new LogicalExpression[1];
		args[0] = lambda.getArgument();
		final Literal newLiteral = new Literal(outerVariable, args);

		// Create the conjunction of newLitral and the original body
		final Literal conjunction = new Literal(
				LogicLanguageServices.getConjunctionPredicate(),
				ArrayUtils.create(newLiteral, lambda.getBody()));

		// The new inner lambda
		final Lambda innerLambda = new Lambda(lambda.getArgument(),
				conjunction);

		// The new outer lambda
		final Lambda outerLambda = new Lambda(outerVariable, innerLambda);

		// Simplify the output and return it
		final LogicalExpression ret = Simplify.of(outerLambda);

		return ret;
	}

	return null;
}