edu.cornell.cs.nlp.spf.mr.lambda.SkolemId Java Examples

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.SkolemId. 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: LogicalExpressionSimpleIndenter.java    From UDepLambda with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		if (!skolemIds.containsKey(variable)) {
			skolemIds.put((SkolemId) variable,
					((SkolemId) variable).getName(skolemIdCounter++));
		}
		outputString.append(skolemIds.get(variable));
	} else {
		outputString.append(getVariableName(variable));
		if (!definedVariables.contains(variable)) {
			outputString.append(Term.TYPE_SEPARATOR);
			outputString.append(variable.getType().getName());
			definedVariables.add(variable);
		}
	}
}
 
Example #2
Source File: AmrToLogicalExpressionConverter.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
public LogicalExpression read(String amr) throws IOException {
	try (StringReader reader = new StringReader(amr)) {
		final String firstToken = getNextToken(reader);
		if (!firstToken.equals("(")) {
			throw new IllegalStateException("AMR doesn't start with '(': "
					+ amr);
		}
		final Map<LogicalConstant, Pair<SkolemId, Type>> skolemIDs = new HashMap<>();
		final LogicalExpression instance = parseInstance(reader,
				new HashMap<String, Variable>(), skolemIDs);

		// Replace all dummy logical constants with the proper skolem IDs.
		final SetReferences visitor = new SetReferences(skolemIDs);
		visitor.visit(instance);
		return visitor.tempReturn;
	}
}
 
Example #3
Source File: GetApplicationArgument.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		variable.equals(variable, scope);
	} else if (variable.equals(applicationArgument)) {
		final LogicalExpression newArgument = createArgument(
				applicationResult, variable, variable, scope);
		if (argument == null) {
			argument = newArgument;
			isValid = true;
		} else if (!argument.equals(newArgument)) {
			isValid = false;
		}
	} else {
		isValid = variable.equals(applicationResult, scope);
	}
}
 
Example #4
Source File: LogicalExpressionToAmr.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
private String getVariableName(SkolemId id, String suggestion) {
	if (skolemMapping.containsKey(id)) {
		return skolemMapping.get(id);
	} else {
		// Use the first character of the suggestion as the default name
		// Append a unique id if the name is already used
		String varName = suggestion.substring(0, 1);
		if (skolemMapping.containsValue(varName)) {
			int index = 2;
			while (skolemMapping.containsValue(varName + index)) {
				index++;
			}
			varName = varName + index;
		}
		skolemMapping.put(id, varName);
		return varName;
	}
}
 
Example #5
Source File: LogicalExpressionToIndentedString.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		if (!skolemIds.containsKey(variable)) {
			skolemIds.put((SkolemId) variable,
					((SkolemId) variable).getName(skolemIdCounter++));
		}
		outputString.append(skolemIds.get(variable));
	} else {
		outputString.append(getVariableName(variable));
		if (!definedVariables.contains(variable)) {
			outputString.append(Term.TYPE_SEPARATOR);
			outputString.append(variable.getType().getName());
			definedVariables.add(variable);
		}
	}
}
 
Example #6
Source File: LogicalExpressionToLatexString.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (!mapping.containsKey(variable)) {
		if (variable instanceof SkolemId) {
			mapping.put(variable, Integer.toString(skolemIdCounter++));
		} else {
			if (variableNameIndex >= VARIABLE_NAMES.length) {
				++variableSuffix;
				variableNameIndex = 0;
			}
			mapping.put(
					variable,
					VARIABLE_NAMES[variableNameIndex++]
							+ (variableSuffix == 0 ? "" : Integer
									.valueOf(variableSuffix)));
		}
	}
	outputString.append(mapping.get(variable));
}
 
Example #7
Source File: SkolemIDFunction.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	// Add the variables to the free variables set.
	if (!(variable instanceof SkolemId)) {
		freeVariables.add(variable);
	}
	result = variable;
}
 
Example #8
Source File: SkolemIdInstanceWrapper.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId
			&& !(variable instanceof SkolemIdInstanceWrapper)) {
		result = of((SkolemId) variable);
	} else {
		result = variable;
	}
}
 
Example #9
Source File: CountVariables.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId && !countSkolemIds) {
		return;
	}

	if (!counted.contains(variable)) {
		count++;
		counted.add(variable);
	}
}
 
Example #10
Source File: StripSkolemIds.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		result = placeholder;
	} else {
		result = variable;
	}
}
 
Example #11
Source File: HasFreeVariables.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
public static boolean of(LogicalExpression exp, boolean ignoreSkolemIds) {
	if (ignoreSkolemIds && exp.numFreeVariables() != 0) {
		for (final Variable variable : exp.getFreeVariables()) {
			if (!(variable instanceof SkolemId)) {
				return true;
			}
		}
		return false;
	} else {
		return exp.numFreeVariables() != 0;
	}

}
 
Example #12
Source File: GetStructure.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		tempReturn = LogicalConstant.create(anonnymousTag,
				variable.getType(), true);
	} else {
		tempReturn = variable;
	}
}
 
Example #13
Source File: LogicalExpressionToString.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		if (!skolemIds.containsKey(variable)) {
			skolemIds.put((SkolemId) variable,
					((SkolemId) variable).getName(skolemIdCounter++));
		}
		outputString.append(skolemIds.get(variable));
	} else {
		processVariable(variable, false);
	}
}
 
Example #14
Source File: CreateFactorGraph.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		currentRoot = new SkolemIdNode((SkolemId) variable, idCounter++);
	} else {
		currentRoot = new VariableNode(variable, idCounter++);
	}
}
 
Example #15
Source File: AmrToLogicalExpressionConverter.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(LogicalConstant logicalConstant) {
	if (skolemIDs.containsKey(logicalConstant)) {
		final SkolemId referenceID = skolemIDs.get(logicalConstant)
				.first();
		tempReturn = new Literal(
				AMRServices.createRefPredicate(skolemIDs.get(
						logicalConstant).second()),
				ArrayUtils.create(referenceID));
	} else {
		tempReturn = logicalConstant;
	}
}
 
Example #16
Source File: AmrToLogicalExpressionConverter.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
private Literal parseRelation(StringReader reader, String argKey,
		Variable variable, Map<String, Variable> variables,
		Map<LogicalConstant, Pair<SkolemId, Type>> skolemIDs)
		throws IOException {
	if (!argKey.startsWith(":")) {
		throw new IllegalStateException("Arg not starting with :");
	}
	// Create the predicate. The predicate name is stripped of the
	// leading ':'.
	final String predicateName = createModifierPrediacteName(argKey);

	final char peek = peek(reader);
	if (peek == '"') {
		// Case quoted text.
		final LogicalConstant constant = parseQuote(getNextToken(reader));
		return createBinaryLiteral(predicateName, variable, constant,
				LogicLanguageServices.getTypeRepository()
						.getTruthValueType());
	} else if (peek == '(') {
		// String the opening parenthesis.
		getNextToken(reader);
		final LogicalExpression argInstance = parseInstance(reader,
				variables, skolemIDs);
		return createBinaryLiteral(predicateName, variable, argInstance,
				LogicLanguageServices.getTypeRepository()
						.getTruthValueType());
	} else {
		// Case constant.
		final String token = getNextToken(reader);
		// If this is a reference variable, create an entity anyway. We
		// don't want to have variables used in such cases. References
		// should be resolved using IDs.
		final LogicalExpression arg = createEntity(token);
		return createBinaryLiteral(predicateName, variable, arg,
				LogicLanguageServices.getTypeRepository()
						.getTruthValueType());
	}
}
 
Example #17
Source File: GetAmrSubExpressions.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests if the given {@link LogicalExpression} has free variables that are
 * not {@link SkolemId}s.
 */
private static boolean isClosed(LogicalExpression exp) {
	for (final Variable variable : exp.getFreeVariables()) {
		if (!(variable instanceof SkolemId)) {
			return false;
		}
	}
	return true;
}
 
Example #18
Source File: SkolemIdNode.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public void setSkolemId(SkolemId skolemId) {
	this.skolemId = skolemId;
}
 
Example #19
Source File: SkolemIdNode.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public SkolemId getSkolemId() {
	return skolemId;
}
 
Example #20
Source File: SkolemIdNode.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public SkolemIdNode(SkolemId skolemId, int id,
		LogicalExpression[] assignments) {
	super(id, Collections.emptySet(), assignments);
	this.skolemId = skolemId;
}
 
Example #21
Source File: SkolemIdNode.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public SkolemIdNode(SkolemId skolemId, int id) {
	this(skolemId, id, ArrayUtils.<LogicalExpression> create(skolemId));
}
 
Example #22
Source File: GetApplicationFunction.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (isDirectlyMatched(variable)) {
		return;
	}

	if (variable instanceof SkolemId) {
		if (!(argument instanceof SkolemId)) {
			result = false;
			return;
		}
		final SkolemId argSkolemId = (SkolemId) argument;
		final SkolemId id = (SkolemId) variable;
		final Variable mappedValue = scope.peek(id);
		if (mappedValue == argSkolemId
				&& scope.peekValue(mappedValue) == id) {
			return;
		} else if (argSkolemId instanceof SkolemIdInstanceWrapper) {
			throw new IllegalArgumentException(
					"skolem ID instance wrapper not supported");
		} else if (!scope.containsValue(argSkolemId)) {
			scope.push(id, argSkolemId);
		} else {
			result = false;
		}
	} else {
		if (!(argument instanceof Variable)) {
			result = false;
			return;
		}
		final Variable argVariable = (Variable) argument;
		final Variable mapValue = scope.peek(variable);
		if (mapValue == argument
				&& scope.peekValue(argVariable) == variable) {
			// Comparison through mapping of variables.
			return;
		} else if (!scope.containsValue(argVariable)) {
			// Case both are not mapped, do instance comparison for free
			// variables.
			result = argVariable == variable;
		} else {
			// Not equal.
			result = false;
			return;
		}
	}
}
 
Example #23
Source File: AmrToLogicalExpressionConverter.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public SetReferences(
		Map<LogicalConstant, Pair<SkolemId, Type>> skolemIDs) {
	this.skolemIDs = skolemIDs;
}
 
Example #24
Source File: AmrToLogicalExpressionConverter.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
private Literal parseNameInstance(StringReader reader,
		Map<LogicalConstant, Pair<SkolemId, Type>> skolemIDs,
		Variable variable, Map<String, Variable> variables, String varName)
		throws IOException {
	final int initialPosition = reader.position();
	final List<Pair<String, String>> ops = new LinkedList<>();
	final List<Literal> relations = new LinkedList<>();
	while (peek(reader) != ')') {
		final String argKey = getNextToken(reader);
		if (argKey.startsWith(":op") && peek(reader) == '"') {
			// Get the argument value.
			ops.add(Pair.of(argKey, getNextToken(reader)));
		} else {
			// Non text relation.
			relations.add(parseRelation(reader, argKey, variable,
					variables, skolemIDs));
		}
	}

	// If the list of ops (quoted chucks of text is empty), fail and try to
	// to read it as regular instance.
	if (ops.isEmpty()) {
		reader.reset(initialPosition);
		return null;
	}

	// Remove the closing parenthesis.
	getNextToken(reader);

	// Sort the arguments list.
	Collections.sort(ops, (o1, o2) -> o1.first().compareTo(o2.first()));

	final List<String> nameList = ops.stream().map((p) -> p.second())
			.collect(Collectors.toList());

	// Create the logical constant to return.
	final LogicalConstant nameConstant = AMRServices
			.createTextConstant(nameList);

	// Verify we can create back the original tokens.
	if (!nameList.equals(AMRServices.textConstantToStrings(nameConstant))) {
		throw new IllegalStateException(
				String.format(
						"Failed to recover name list from text constant: %s <-> %s [%s]",
						nameList, nameConstant, reader));
	}

	// Create name instance, e.g., (a:<id,<<e,t>,e>> (lambda $0:e
	// (and:<t*,t> (name:<e,t> $0) (op:<e,<txt,t>> $0 NAME:txt))))

	// Create the skolem ID and keep it.
	final SkolemId skolemId = new SkolemId();
	skolemIDs.put(LogicalConstant.create(varName, LogicLanguageServices
			.getTypeRepository().getEntityType(), true), Pair.of(skolemId,
			LogicLanguageServices.getTypeRepository().getEntityType()));

	relations.add(
			0,
			createBinaryLiteral("c_op", variable, nameConstant,
					LogicLanguageServices.getTypeRepository()
							.getTruthValueType()));
	relations.add(
			0,
			createUnaryLiteral("name", variable, LogicLanguageServices
					.getTypeRepository().getTruthValueType()));

	final Literal nameSkolemTerm = new Literal(
			AMRServices.createSkolemPredicate(LogicLanguageServices
					.getTypeRepository().getEntityType()),
			ArrayUtils.create(skolemId, new Lambda(variable, new Literal(
					LogicLanguageServices.getConjunctionPredicate(),
					relations.toArray(new Literal[relations.size()])))));

	return nameSkolemTerm;
}
 
Example #25
Source File: GetSkolemIds.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
public static Set<SkolemId> of(LogicalExpression exp) {
	final GetSkolemIds visitor = new GetSkolemIds();
	visitor.visit(exp);
	return visitor.ids;
}
 
Example #26
Source File: GetSkolemIds.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Variable variable) {
	if (variable instanceof SkolemId) {
		ids.add((SkolemId) variable);
	}
}
 
Example #27
Source File: SkolemIdInstanceWrapper.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
private SkolemIdInstanceWrapper(SkolemId base) {
	this.base = base;
}
 
Example #28
Source File: SkolemIdInstanceWrapper.java    From spf with GNU General Public License v2.0 4 votes vote down vote up
public static SkolemIdInstanceWrapper of(SkolemId base) {
	return new SkolemIdInstanceWrapper(base);
}
 
Example #29
Source File: IsUnderspecifiedAndStripped.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(Variable variable) {
	result = !(variable instanceof SkolemId);
}