Java Code Examples for edu.cornell.cs.nlp.spf.mr.lambda.LogicLanguageServices#isCoordinationPredicate()

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.LogicLanguageServices#isCoordinationPredicate() . 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: ExtractTypedSubExpression.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(Literal literal) {
	increaseDepth();
	boolean literalHasOnlyVariables = true;
	literal.getPredicate().accept(this);
	literalHasOnlyVariables &= variablesOnly;
	final int len = literal.numArgs();
	for (int i = 0; i < len; ++i) {
		literal.getArg(i).accept(this);
		literalHasOnlyVariables &= variablesOnly;
	}
	--depth;

	if (!literalHasOnlyVariables
			&& (!skipCoordinations || !LogicLanguageServices
					.isCoordinationPredicate(literal.getPredicate()))) {
		addCurrent(literal);
	}
}
 
Example 2
Source File: SupervisedFilterFactoryTest.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test() {
	final LogicalExpression exp = TestServices.getCategoryServices()
			.readSemantics(
					"(a:<id,<<e,t>,e>> !1 (lambda $0:e (and:<t*,t> (state-01:<e,t> $0) (c_ARG0:<e,<e,t>> $0 (a:<id,<<e,t>,e>> !2 (lambda $1:e (and:<t*,t> (person:<e,t> $1) (c_name:<e,<e,t>> $1 Megawati:e))))))))");
	final LogicalExpression partialExp = TestServices.getCategoryServices()
			.readSemantics(
					"(a:<id,<<e,t>,e>> !2 (lambda $1:e (and:<t*,t> (person:<e,t> $1) (c_name:<e,<e,t>> $1 Megawati:e))))");
	final SupervisedFilterFactory<SingleSentence> factory = new SupervisedFilterFactory<SingleSentence>(
			e -> !LogicLanguageServices.isCoordinationPredicate(e));
	Assert.assertTrue(factory.create(exp)
			.test(new ParsingOp<LogicalExpression>(
					Category.create(Syntax.N, partialExp),
					new SentenceSpan(1, 1, 2),
					RuleName.create("dummy", Direction.FORWARD))));
}
 
Example 3
Source File: CoordinationServices.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a coordination skolem term, return all the coordinated elements
 * (i.e., the second arguments in all c_opX binary literals in the upper
 * most conjunction).
 */
public static List<LogicalExpression> getCoordinatedItems(
		Literal coordination) {
	final LogicalExpression coordinationBody = ((Lambda) coordination
			.getArg(1)).getBody();
	if (coordinationBody instanceof Literal
			&& LogicLanguageServices
					.isCoordinationPredicate(((Literal) coordinationBody)
							.getPredicate())) {
		final Literal coordinationBodyLiteral = (Literal) coordinationBody;
		final int coordinationBodyLiteralNumArgs = coordinationBodyLiteral
				.numArgs();
		final List<LogicalExpression> items = new ArrayList<>(
				coordinationBodyLiteralNumArgs);
		for (int i = 0; i < coordinationBodyLiteralNumArgs; ++i) {
			final LogicalExpression arg = coordinationBodyLiteral.getArg(i);
			if (arg instanceof Literal
					&& ((Literal) arg).numArgs() == 2
					&& ((Literal) arg).getPredicate() instanceof LogicalConstant
					&& isCOpPredicate((LogicalConstant) ((Literal) arg)
							.getPredicate())) {
				items.add(((Literal) arg).getArg(1));
			}
		}
		return items;
	} else {
		return Collections.emptyList();
	}
}
 
Example 4
Source File: AMRSupervisedFilter.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Literal literal) {
	final int numArgs = literal.numArgs();
	final LogicalExpression predicate = literal
			.getPredicate() instanceof LogicalConstant
					? OverloadedLogicalConstant.getWrapped(
							(LogicalConstant) literal.getPredicate())
					: literal.getPredicate();
	if (numArgs == 2
			&& !LogicLanguageServices.isCoordinationPredicate(predicate)
			&& predicate instanceof LogicalConstant
			&& constantFilter.test((LogicalConstant) predicate)) {
		countRelationalPairs((LogicalConstant) predicate,
				literal.getArg(1));
		if (!isValid) {
			return;
		}
	}

	if (LogicLanguageServices.isCoordinationPredicate(predicate)) {
		// Try to construct the instance-type-related-type triplets as
		// much as possible. We are trying to do as early as possible,
		// even before the skolem term is closed.
		countInstanceTypeRelatedTypeTriplets(literal);
		if (!isValid) {
			return;
		}
	}

	predicate.accept(this);
	for (int i = 0; i < numArgs; ++i) {
		literal.getArg(i).accept(this);
		if (!isValid) {
			return;
		}
	}
}
 
Example 5
Source File: GetAllPredicates.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(LogicalConstant logicalConstant) {
	if (logicalConstant.getType().isComplex()
			&& !LogicLanguageServices
					.isCoordinationPredicate(logicalConstant)
			&& !LogicLanguageServices
					.isArrayIndexPredicate(logicalConstant)
			&& !LogicLanguageServices.isArraySubPredicate(logicalConstant)) {
		// Case found a predicate, add it to the return set
		predicates.add(logicalConstant);
	}
}
 
Example 6
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 7
Source File: FactoringServices.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isFactorable(LogicalConstant constant) {
	return !LogicLanguageServices.isCoordinationPredicate(constant)
			&& !LogicLanguageServices.isArrayIndexPredicate(constant)
			&& !LogicLanguageServices.isArraySubPredicate(constant)
			&& !LogicLanguageServices.getTypeRepository().getIndexType()
					.equals(constant.getType())
			&& INSTANCE.filter.apply(constant)
			&& !INSTANCE.unfactoredConstants.contains(constant);
}
 
Example 8
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 9
Source File: LemmatizeCandidates.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
	// //////////////////////////////////////////
	// Init logging
	// //////////////////////////////////////////

	Logger.DEFAULT_LOG = new Log(System.err);
	Logger.setSkipPrefix(true);
	LogLevel.INFO.set();

	// //////////////////////////////////////////
	// Init AMR.
	// //////////////////////////////////////////

	Init.init(new File(args[0]), false);

	for (final SingleSentence sentence : SingleSentenceCollection.read(
			new File(args[1]), new Tokenizer())) {
		System.out.println(sentence.getSample());
		for (final LogicalConstant c : GetConstantsMultiSet.of(sentence
				.getLabel())) {
			if (!LogicLanguageServices.isCoordinationPredicate(c)
					&& !c.getBaseName().startsWith("c_")
					&& !LogicLanguageServices.getNumeralType().equals(
							c.getType())
					&& !AMRServices.isRefPredicate(c)
					&& !LogicLanguageServices.getNegationPredicate()
							.equals(c) && !AMRServices.isSkolemPredicate(c)
					&& !AMRServices.isTextType(c.getType())) {
				final Matcher matcher = PROPBANK_PREDICATE.matcher(c
						.getBaseName());
				if (matcher.matches()) {
					System.out.println(matcher.group("sense"));
				} else {
					System.out.println(c.getBaseName());
				}
			}
		}
		System.out.println();
	}

}
 
Example 10
Source File: LogicalExpressionToLatexString.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Literal literal) {
	final int numArgs = literal.numArgs();
	if (LogicLanguageServices.isCoordinationPredicate(literal
			.getPredicate())) {
		// Case coordination predicate.
		for (int i = 0; i < numArgs; ++i) {
			literal.getArg(i).accept(this);
			if (i + 1 < numArgs) {
				outputString.append(' ');
				literal.getPredicate().accept(this);
				outputString.append(' ');
			}
		}
	} else if (numArgs > 1
			&& literal.getArg(0).getType()
					.equals(SkolemServices.getIDType())
			&& literal.getArg(1).getType()
					.equals(SkolemServices.getIDType())) {
		// Case skolem terms with referring ID: pred_{id}^{ref}(args).
		literal.getPredicate().accept(this);
		outputString.append("_{");
		literal.getArg(0).accept(this);
		outputString.append('}');
		outputString.append("^{");
		literal.getArg(1).accept(this);
		outputString.append('}');
		if (numArgs > 2) {
			outputString.append('(');
			for (int i = 2; i < numArgs; ++i) {
				literal.getArg(i).accept(this);
				if (i + 1 < numArgs) {
					outputString.append(", ");
				}
			}
			outputString.append(')');
		}
	} else if (numArgs > 0
			&& literal.getArg(0).getType()
					.equals(SkolemServices.getIDType())) {
		// Case skolem term without reference: pred_{id}(args).
		literal.getPredicate().accept(this);
		outputString.append("_{");
		literal.getArg(0).accept(this);
		outputString.append('}');
		if (numArgs > 1) {
			for (int i = 1; i < numArgs; ++i) {
				outputString.append('(');
				literal.getArg(i).accept(this);
				if (i + 1 < numArgs) {
					outputString.append(", ");
				}
			}
			outputString.append(')');
		}
	} else {
		literal.getPredicate().accept(this);
		outputString.append('(');
		for (int i = 0; i < numArgs; ++i) {
			literal.getArg(i).accept(this);
			if (i + 1 < numArgs) {
				outputString.append(", ");
			}
		}
		outputString.append(')');
	}
}
 
Example 11
Source File: LogicalExpressionToIndentedString.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Literal literal) {
	final int len = literal.numArgs();
	if (LogicLanguageServices.isCoordinationPredicate(literal
			.getPredicate())) {
		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 (!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 12
Source File: CountLogicalConstants.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(LogicalConstant logicalConstant) {
	if (!LogicLanguageServices.isCoordinationPredicate(logicalConstant)) {
		++count;
	}
}
 
Example 13
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 14
Source File: AbstractEvaluationServices.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean isInterpretable(LogicalConstant constant) {
	return LogicLanguageServices.isCoordinationPredicate(constant);
}