Java Code Examples for edu.cornell.cs.nlp.spf.mr.lambda.Literal#numArgs()

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.Literal#numArgs() . 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: CoordinationC2Rule.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
protected ParseRuleResult<LogicalExpression> doApply(
		LogicalExpression left, Literal right, CoordinationSyntax syntax) {
	final Type baseType = right.getType();

	// Create the argument list and the new semantic form.
	final int numArgs = right.numArgs();
	final LogicalExpression[] arguments = new LogicalExpression[numArgs + 1];
	// Make sure there's no free variable overlap.
	arguments[0] = ReplaceFreeVariablesIfPresent.of(left,
			right.getFreeVariables());
	right.copyArgsIntoArray(arguments, 0, 1, numArgs);

	// Create the return category, including the syntactic
	// component.
	final Category<LogicalExpression> resultCategory = Category
			.<LogicalExpression> create(syntax, CoordinationServices
					.createLiteral(((LogicalConstant) right.getPredicate())
							.getBaseName(), arguments, baseType));

	return new ParseRuleResult<>(ruleName, resultCategory);
}
 
Example 2
Source File: LogicalExpressionToString.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	outputString.append("(");
	literal.getPredicate().accept(this);
	// Visit the arguments to print them. Print a space before each
	// argument.
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		outputString.append(' ');
		literal.getArg(i).accept(this);
	}
	outputString.append(')');
}
 
Example 3
Source File: GetAllVacuousVariables.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		literal.getArg(i).accept(this);
	}
}
 
Example 4
Source File: GetConstantsSet.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		literal.getArg(i).accept(this);
	}
}
 
Example 5
Source File: DummyEntityServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a skolem term (i.e., (a:<<e,t>,<id,e>> na:id (lambda $0:e ....))),
 * return 'true' iff it has a relation to a dummy entity.
 */
public static boolean hasDummyEntity(Literal literal) {
	if (AMRServices.isSkolemTerm(literal) && literal.numArgs() == 2
			&& literal.getArg(1) instanceof Lambda) {
		return hasDummyEntity((Lambda) literal.getArg(1));
	} else {
		return false;
	}
}
 
Example 6
Source File: NotSimplifier.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public LogicalExpression simplify(LogicalExpression exp) {
	if (exp instanceof Literal) {
		// If the argument is a literal with 'not:t' predicate, return the
		// argument for the inner literal
		final Literal literal = (Literal) exp;

		// If we have more than one argument, don't do anything, this
		// expression is one bad apple
		if (literal.numArgs() == 1) {
			if (literal.getArg(0) instanceof Literal
					&& ((Literal) literal.getArg(0)).getPredicate().equals(
							literal.getPredicate())) {
				// Case the only argument is a 'not:t' literal, so return
				// its
				// single argument
				final Literal subNot = (Literal) literal.getArg(0);
				if (subNot.numArgs() == 1) {
					return subNot.getArg(0);
				}
			} else if (literal.getArg(0) == LogicLanguageServices.getTrue()) {
				// If the single argument is 'true:t' constant, return
				// 'false:t'
				return LogicLanguageServices.getFalse();
			} else if (literal.getArg(0) == LogicLanguageServices
					.getFalse()) {
				// If the single argument is 'false:t' constant, return
				// 'true:t'
				return LogicLanguageServices.getTrue();
			}

		}
		// Case didn't change anything
		return exp;
	} else {
		return exp;
	}
}
 
Example 7
Source File: MakeCompositionSplits.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	if (literal.getPredicateType() instanceof RecursiveComplexType) {
		// Case recursive predicate, we to extract subsets of its arguments
		splits.addAll(doSplitsForRecursivePredicate(literal));
	}

	// Collect all the free variables. We need one of them to match the
	// outermost variables of the original Lambda expression. The total
	// number has to be less than the threshold. Also, skip literals where
	// the predicate is a variable.
	final Set<Variable> freeVars = GetAllFreeVariables.of(literal);
	if (freeVars.size() <= SplittingServices.MAX_NUM_VARS
			&& freeVars.remove(originalLambda.getArgument())
			&& !(literal.getPredicate() instanceof Variable)) {
		for (final List<Variable> order : SplittingServices
				.allOrders(freeVars)) {
			final SplittingPair split = doSplit(literal, order);
			if (split != null) {
				splits.add(split);
			}
		}
	}

	// NOTE: we do not call literal.getPredicate().accept(this) because we
	// don't want to pull out predicate names
	final int numArgs = literal.numArgs();
	for (int i = 0; i < numArgs; ++i) {
		literal.getArg(i).accept(this);
	}
}
 
Example 8
Source File: CreateFactorGraph.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	final AbstractDummyNode predicateNode = currentRoot;
	final int numArgs = literal.numArgs();
	final List<AbstractDummyNode> argNodes = new ArrayList<>(numArgs);
	for (int i = 0; i < numArgs; ++i) {
		literal.getArg(i).accept(this);
		argNodes.add(currentRoot);
	}
	currentRoot = new LiteralNode(literal, predicateNode, argNodes,
			idCounter++);
}
 
Example 9
Source File: GetSkolemIds.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		literal.getArg(i).accept(this);
	}
}
 
Example 10
Source File: SingleSentencePartialCreditTestingStatistics.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	// Visit the predicate
	literal.getPredicate().accept(this);

	final LogicalExpression pred = literal.getPredicate();
	final int numArgs = literal.numArgs();
	if (!LogicLanguageServices.isCoordinationPredicate(pred)
			&& !LogicLanguageServices.isArrayIndexPredicate(pred)
			&& !LogicLanguageServices.isArraySubPredicate(pred)
			&& literal.getPredicate() instanceof LogicalConstant) {
		if (numArgs == 1
				&& !(literal.getArg(0) instanceof LogicalConstant)) {
			// Unary predicates
			predConstPairs.add(Pair.of(literal.getPredicate(),
					(LogicalExpression) null));
			return;
		} else if (numArgs == 2
				&& !(literal.getArg(0) instanceof LogicalConstant)
				&& IsExtendedConstant.of(literal.getArg(1))) {
			// Binary predicate
			predConstPairs.add(Pair.of(literal.getPredicate(),
					literal.getArg(1)));
			return;
		}
	}

	// Just visit the arguments and predicate
	for (int i = 0; i < numArgs; ++i) {
		literal.getArg(i).accept(this);
	}
}
 
Example 11
Source File: IsExtendedConstant.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		literal.getArg(i).accept(this);
	}
}
 
Example 12
Source File: AMRServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a skolem term, return the unary predicate that defines its type, if
 * such exist.
 */
public static LogicalConstant getTypingPredicate(Literal skolemTerm) {
	if (skolemTerm.numArgs() == 2
			&& skolemTerm.getArg(1) instanceof Lambda) {
		return getTypingPredicate((Lambda) skolemTerm.getArg(1));
	}
	return null;
}
 
Example 13
Source File: AMRServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the unary typing predicate (if one exists) from a <e,t>-typed lambda
 * term (the body of a skolem term).
 */
public static LogicalConstant getTypingPredicate(Lambda set) {
	if (set.getBody() instanceof Literal) {
		final Literal bodyLiteral = (Literal) set.getBody();
		final int len = bodyLiteral.numArgs();
		if (bodyLiteral.getPredicate()
				.equals(LogicLanguageServices.getConjunctionPredicate())) {
			return getTypingPredicateFromConjunction(bodyLiteral);
		} else if (len == 1
				&& bodyLiteral.getPredicate() instanceof LogicalConstant) {
			return (LogicalConstant) bodyLiteral.getPredicate();
		}
	}
	return null;
}
 
Example 14
Source File: IsEvaluable.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		literal.getArg(i).accept(this);
		if (!result) {
			return;
		}
	}
}
 
Example 15
Source File: IsUnderspecifiedAndStripped.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	literal.getPredicate().accept(this);
	if (!result) {
		return;
	}

	final int numArgs = literal.numArgs();
	for (int i = 0; i < numArgs; ++i) {
		literal.getArg(i).accept(this);
		if (!result) {
			return;
		}
	}
}
 
Example 16
Source File: GetAllSkolemTerms.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	final boolean isSkolemTerm = AMRServices.isSkolemTerm(literal);
	if (isSkolemTerm) {
		skolemTerms.add(literal);
	}

	if (!shallow || !isSkolemTerm) {
		literal.getPredicate().accept(this);
		final int len = literal.numArgs();
		for (int i = 0; i < len; ++i) {
			literal.getArg(i).accept(this);
		}
	}
}
 
Example 17
Source File: LogicalExpressionSimpleIndenter.java    From UDepLambda with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(Literal literal) {
	final int len = literal.numArgs();
	 TypeRepository typeRepository = LogicLanguageServices
		        .getTypeRepository();
	
	if (LogicLanguageServices.isCoordinationPredicate(literal
			.getPredicate())
			// TODO: Fix this hack. Figure out how 
			|| literal.getPredicate().equals(AND_c)) {
		outputString.append("(");
		literal.getPredicate().accept(this);
		// Visit the arguments to print them. Print a space before each
		// argument.
		++currentDepth;
		for (int i = 0; i < len; ++i) {
			outputString.append("\n"
					+ StringUtils.multiply(indentation, currentDepth));
			literal.getArg(i).accept(this);
		}
		--currentDepth;
		outputString.append(')');
	} else if (literal.getPredicate().equals(EX_ex)) {
		outputString.append("(");
		literal.getPredicate().accept(this);
		// Visit the arguments to print them. Print a space before each
		// argument.
		outputString.append(' ');
		literal.getArg(0).accept(this); // the variable
		
		++currentDepth;
		for (int i = 1; i < len; ++i) {
			outputString.append("\n"
					+ StringUtils.multiply(indentation, currentDepth));
			literal.getArg(i).accept(this);
		}
		--currentDepth;
		outputString.append(')');
	} else if (!HasFreeVariables.of(literal, true)
			&& outputString.length() > 0) {
		++currentDepth;
		outputString.append("\n"
				+ StringUtils.multiply(indentation, currentDepth));
		outputString.append("(");
		literal.getPredicate().accept(this);
		// Visit the arguments to print them. Print a space before each
		// argument.
		// ++currentDepth;
		for (int i = 0; i < len; ++i) {
			// outputString.append("\n"
			// + StringUtils.multiply(indentation, currentDepth));
			outputString.append(' ');
			literal.getArg(i).accept(this);
		}
		// --currentDepth;
		--currentDepth;
		outputString.append(')');
	} else {
		outputString.append("(");
		literal.getPredicate().accept(this);
		// Visit the arguments to print them. Print a space before each
		// argument.
		for (int i = 0; i < len; ++i) {
			outputString.append(' ');
			literal.getArg(i).accept(this);
		}
		outputString.append(')');
	}
}
 
Example 18
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 19
Source File: Evaluation.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Literal literal) {
	testInterruption();
	// Try to get from cache
	if (services.isCached(literal)) {
		result = services.getFromCache(literal);
		return;
	}

	// If it's a coordination update the result variable with the
	// default return value (T for conjunction, F for disjunction)
	final int len = literal.numArgs();
	if (LogicLanguageServices.isCoordinationPredicate(literal
			.getPredicate())) {
		// Case coordination predicate, can short-circuit

		// Get short-circuiting argument value
		final Boolean shortCircuitingValue;
		if (LogicLanguageServices.getConjunctionPredicate().equals(
				literal.getPredicate())) {
			shortCircuitingValue = Boolean.FALSE;
		} else if (LogicLanguageServices.getDisjunctionPredicate().equals(
				literal.getPredicate())) {
			shortCircuitingValue = Boolean.TRUE;
		} else {
			throw new IllegalStateException(
					"unhandled coordination predicate: " + literal);
		}

		for (int i = 0; i < len; ++i) {
			literal.getArg(i).accept(this);
			if (result == null || shortCircuitingValue.equals(result)) {
				// Cache
				services.cacheResult(literal, result);

				return;
			}

		}

		// Case not short-circuited, so return the default value
		result = !shortCircuitingValue;
	} else {
		// Case not a coordination, no shortcuts, use domain executors to
		// evaluate

		// Iterate over the arguments
		final Object[] evalArgs = new Object[len];
		int counter = 0;
		for (int i = 0; i < len; ++i) {
			literal.getArg(i).accept(this);
			if (result == null) {
				// If failed to evaluate, propagate failure to literal

				// Cache
				services.cacheResult(literal, result);

				return;
			} else {
				evalArgs[counter] = result;
			}
			++counter;
		}

		// Execute predicate with arguments, return result
		result = services.evaluateLiteral(literal.getPredicate(), evalArgs);
	}

	// Cache
	services.cacheResult(literal, result);
}
 
Example 20
Source File: MakeCompositionSplits.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
private Set<SplittingPair> doSplitsForRecursiveOrderSensitivePredicate(
		Literal literal) {
	final RecursiveComplexType predicateType = (RecursiveComplexType) literal
			.getPredicateType();
	final Set<SplittingPair> newSplits = new HashSet<SplittingServices.SplittingPair>();
	final int numArgs = literal.numArgs();

	// Variable for x in \lambda x . f(g(x))
	final Variable rootArg = originalLambda.getArgument();

	// Iterate over all span lengths
	for (int length = predicateType.getMinArgs(); length <= SplittingServices.MAX_NUM_SUBS; length++) {
		// Iterate over all spans of the given length
		for (int begin = 0; begin < numArgs - length; begin++) {
			// The current span of arguments
			final LogicalExpression[] argsSublist = literal.argumentCopy(
					begin, begin + length);

			// Body of g
			final Literal gBody = new Literal(literal.getPredicate(),
					argsSublist);
			final Set<Variable> gFreeVars = GetAllFreeVariables.of(gBody);
			if (gFreeVars.size() <= SplittingServices.MAX_NUM_VARS
					&& gFreeVars.remove(rootArg)) {
				// Case the number of free variables is under the threshold
				// and the set of free variables contains the root argument,
				// otherwise skip this subset. Also remove the root argument
				// from the free variables, so we an glue at the prefix
				// of the lambda expression we will create.

				// Iterate over all possible variable orderings
				for (final List<Variable> newArgOrder : SplittingServices
						.allOrders(gFreeVars)) {

					// Construct the variables list
					final List<Variable> newVars = new LinkedList<Variable>();
					// Put the root argument at the beginning of the
					// variables list (this is the variable x, such that h =
					// \lambda x f(g(x))).
					newVars.add(rootArg);
					// And the rest of the variables
					newVars.addAll(newArgOrder);

					// Create the new function g, to extract
					final LogicalExpression g = SplittingServices
							.makeExpression(newVars, gBody);

					// Create f, such that h = \lambda x f(g(x))
					newVars.remove(rootArg);
					final Variable compositionArg = new Variable(
							LogicLanguageServices.getTypeRepository()
									.generalizeType(g.getType().getRange()));
					final LogicalExpression embeddedApplication = SplittingServices
							.makeAssignment(newVars, compositionArg);
					final List<LogicalExpression> newLiteralArguments = Arrays
							.asList(literal.argumentCopy());
					// Remove the sub-list of arguments we extracted
					for (int i = 0; i < length; i++) {
						newLiteralArguments.remove(begin);
					}
					// Add the embedded application instead
					newLiteralArguments.add(begin, embeddedApplication);
					final LogicalExpression newLiteral = new Literal(
							literal.getPredicate(),
							newLiteralArguments
									.toArray(new LogicalExpression[newLiteralArguments
											.size()]));
					// Replace the original literal with the new one. Only
					// support replacing all occurrences.
					final LogicalExpression fBody = ReplaceExpression.of(
							originalLambda.getBody(), literal, newLiteral);

					final Lambda f = new Lambda(compositionArg, fBody);

					// Verify that f has no free variables (can happen if
					// rootArg appears in other places)
					if (GetAllFreeVariables.of(f).size() == 0) {
						newSplits.add(createSplittingPair(f, g));
					}
				}
			}
		}
	}
	return newSplits;
}