edu.cornell.cs.nlp.spf.mr.language.type.TypeRepository Java Examples
The following examples show how to use
edu.cornell.cs.nlp.spf.mr.language.type.TypeRepository.
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: Lambda.java From spf with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unchecked") private Lambda(Variable argument, LogicalExpression body, TypeRepository typeRepository) { assert argument != null; assert body != null; this.argument = argument; this.body = body; this.type = typeRepository.getTypeCreateIfNeeded(body.getType(), argument.getType()); assert type != null && type.isComplex() : String.format( "Invalid lambda type: arg=%s, body=%s, inferred type=%s", argument, body, type); if (body.numFreeVariables() == 1 && body.containsFreeVariable(argument)) { // Case single free variables in the body and it's the argument, so // we have no free variables. this.freeVariables = ReferenceSets.EMPTY_SET; } else { final ReferenceSet<Variable> variables = new ReferenceOpenHashSet<Variable>( body.getFreeVariables()); variables.remove(argument); this.freeVariables = ReferenceSets.unmodifiable(variables); } }
Example #2
Source File: Literal.java From spf with GNU General Public License v2.0 | 5 votes |
public static Pair<Type, Type[]> computeLiteralTyping( ComplexType predicateType, Type[] argTypes, ITypeComparator typeComparator, TypeRepository typeRepository) { final Type[] impliedSignatureTypes = new Type[argTypes.length]; final Type computedType = computeLiteralTyping(predicateType, argTypes, typeComparator, typeRepository, impliedSignatureTypes); if (computedType == null) { return null; } else { return Pair.of(computedType, impliedSignatureTypes); } }
Example #3
Source File: CreateSentenceConstantsFile.java From spf with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) { // ////////////////////////////////////////// // Init logging // ////////////////////////////////////////// Logger.DEFAULT_LOG = new Log(System.err); Logger.setSkipPrefix(true); LogLevel.setLogLevel(LogLevel.INFO); // ////////////////////////////////////////// // Init lambda calculus system. // ////////////////////////////////////////// try { // Init the logical expression type system LogicLanguageServices .setInstance(new LogicLanguageServices.Builder( new TypeRepository(new File(args[1])), new FlexibleTypeComparator()).build()); } catch (final IOException e) { throw new RuntimeException(e); } // ////////////////////////////////////////// // Print output file. // ////////////////////////////////////////// // Read input data. final SingleSentenceCollection data = SingleSentenceCollection.read(new File( args[0])); for (final SingleSentence sentence : data) { System.out.println(sentence.getSample()); System.out.println(ListUtils.join( GetConstantsMultiSet.of(sentence.getLabel()), " ")); System.out.println(); } }
Example #4
Source File: SkolemId.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public SkolemId read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator, LogicalExpressionReader reader) { if (!mapping.containsKey(string)) { mapping.push(string, new SkolemId()); } return (SkolemId) mapping.peek(string); }
Example #5
Source File: LogicalExpressionReader.java From spf with GNU General Public License v2.0 | 5 votes |
/** * Read a logical expression from a string. * * @param string * LISP formatted string. * @param mapping * Mapping of labels to logical expressions created during * parsing (for example, so we could re-use variables). */ protected LogicalExpression read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator) { for (final IReader<? extends LogicalExpression> reader : readers) { if (reader.test(string)) { return reader.read(string, mapping, typeRepository, typeComparator, this); } } throw new IllegalArgumentException( "Invalid logical expression syntax: " + string); }
Example #6
Source File: LogicalExpressionReader.java From spf with GNU General Public License v2.0 | 5 votes |
/** {@see #read(String)} */ private LogicalExpression read(String string, TypeRepository typeRepository, ITypeComparator typeComparator) { // Flatten the string. Replace all white space sequences with a single // space. final String flatString = WHITE_SPACE_REPLACER.replace(string); try { return LambdaWrapped.of(read(flatString, new ScopeMapping<String, LogicalExpression>(), typeRepository, typeComparator)); } catch (final RuntimeException e) { LOG.error("Logical expression syntax error: %s", flatString); throw e; } }
Example #7
Source File: LogicalConstant.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public LogicalConstant read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator, LogicalExpressionReader reader) { return LogicalConstant.read(string, typeRepository); }
Example #8
Source File: LogicalConstant.java From spf with GNU General Public License v2.0 | 5 votes |
protected static LogicalConstant read(String string, TypeRepository typeRepository) { try { final String[] split = string.split(Term.TYPE_SEPARATOR); if (split.length != 2) { throw new LogicalExpressionRuntimeException( "Constant sytax error: " + string); } // The type is the part of the string after the colon Type type = typeRepository.getType(split[1]); if (type == null) { // Try to create the type if not found, if this is a primitive // type // that is unknown, the type repository will return null type = typeRepository.getTypeCreateIfNeeded(split[1]); } if (type == null) { // If we still fail, the type is unknown throw new LogicalExpressionRuntimeException( String.format("Unknown type for: %s", string)); } return create(string, type, false); } catch (final RuntimeException e) { LOG.error("Logical constant syntax error: %s", string); throw e; } }
Example #9
Source File: Literal.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public Literal read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator, LogicalExpressionReader reader) { try { final LispReader lispReader = new LispReader(new StringReader( string)); // First is the literal predicate. Get its signature and verify // it // exists final String predicateString = lispReader.next(); final LogicalExpression predicate = reader.read( predicateString, mapping, typeRepository, typeComparator); // The rest of the elements are the arguments final List<LogicalExpression> arguments = new ArrayList<LogicalExpression>(); while (lispReader.hasNext()) { final String stringElement = lispReader.next(); final LogicalExpression argument = reader.read( stringElement, mapping, typeRepository, typeComparator); arguments.add(argument); } // Create the literal, all checks are done within the // constructor return new Literal(predicate, arguments.toArray(new LogicalExpression[arguments .size()]), typeComparator, typeRepository); } catch (final RuntimeException e) { LOG.error("Literal syntax error: %s", string); throw e; } }
Example #10
Source File: SimpleLogicalExpressionReader.java From UDepLambda with Apache License 2.0 | 5 votes |
public static LogicalExpression read(String string) { TypeRepository typeRepository = LogicLanguageServices .getTypeRepository(); ITypeComparator typeComparator = LogicLanguageServices .getTypeComparator(); LogicalExpression exp = LogicalExpressionReader.INSTANCE.read(string, new ScopeMapping<>(), typeRepository, typeComparator); return Simplify.of(exp); }
Example #11
Source File: LogicLanguageServices.java From spf with GNU General Public License v2.0 | 5 votes |
private static Set<LogicalConstant> readConstantsFromFiles( List<File> files, TypeRepository typeRepository) throws IOException { final Set<LogicalConstant> constants = new HashSet<LogicalConstant>(); for (final File file : files) { constants.addAll(readConstantsFromFile(file, typeRepository)); } return constants; }
Example #12
Source File: LogicLanguageServices.java From spf with GNU General Public License v2.0 | 5 votes |
private static Set<LogicalConstant> readConstantsFromFile(File file, TypeRepository typeRepository) throws IOException { // First, strip the comments and prepare a clean LISP string to // parse final StringBuilder strippedFile = new StringBuilder(); try (final BufferedReader reader = new BufferedReader( new FileReader(file))) { String line = null; while ((line = reader.readLine()) != null) { line = line.trim(); line = line.split("\\s*//")[0]; if (!line.equals("")) { strippedFile.append(line).append(" "); } } } // Read the constants final Set<LogicalConstant> constants = new HashSet<LogicalConstant>(); final LispReader lispReader = new LispReader(new StringReader( strippedFile.toString())); while (lispReader.hasNext()) { final LogicalConstant exp = LogicalConstant.read( lispReader.next(), typeRepository); constants.add(exp); } return constants; }
Example #13
Source File: LogicLanguageServices.java From spf with GNU General Public License v2.0 | 5 votes |
private LogicLanguageServices(TypeRepository typeRepository, String numeralTypeName, ITypeComparator typeComparator, Ontology ontology, LogicalConstant conjunctionPredicate, LogicalConstant disjunctionPredicate, LogicalConstant negationPredicate, LogicalConstant indexIncreasePredicate, LogicalConstant trueConstant, LogicalConstant falseConstant, ILogicalExpressionPrinter printer, ILogicalExpressionComparator comparator) { this.typeRepository = typeRepository; this.ontology = ontology; this.printer = printer; this.comparator = comparator; this.numeralType = numeralTypeName == null ? null : typeRepository .getType(numeralTypeName); this.typeComparator = typeComparator; // Basic predicates this.conjunctionPredicate = conjunctionPredicate; this.disjunctionPredicate = disjunctionPredicate; this.negationPredicate = negationPredicate; this.indexIncreasePredicate = indexIncreasePredicate; // Predicate specific simplifiers this.setSimplifier(conjunctionPredicate, new AndSimplifier( trueConstant, falseConstant), true); this.setSimplifier(disjunctionPredicate, new OrSimplifier(trueConstant, falseConstant), true); this.setSimplifier(negationPredicate, NotSimplifier.INSTANCE, true); this.setSimplifier(indexIncreasePredicate, IncSimplifier.INSTANCE, false); // Special constants this.trueConstant = trueConstant; this.falseConstant = falseConstant; this.collapsibleConstants.add(trueConstant); this.collapsibleConstants.add(falseConstant); }
Example #14
Source File: Variable.java From spf with GNU General Public License v2.0 | 5 votes |
@Override public Variable read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator, LogicalExpressionReader reader) { try { final Pair<String, Variable> defintion = readVariableDefintion( string, typeRepository); if (defintion != null && !mapping.containsKey(string)) { mapping.push(defintion.first(), defintion.second()); return defintion.second(); } else if (defintion != null) { throw new LogicalExpressionRuntimeException( "Re-define a global variable: " + string); } else { // Case variable reference. if (mapping.containsKey(string)) { return (Variable) mapping.peek(string); } else { throw new LogicalExpressionRuntimeException( "Undefined variable reference: " + string); } } } catch (final RuntimeException e) { LOG.error("Variable error: %s", string); throw e; } }
Example #15
Source File: Variable.java From spf with GNU General Public License v2.0 | 5 votes |
/** * Reads a variable definition into a pair of a {@link Variable} and a * {@link String} (its name). */ static Pair<String, Variable> readVariableDefintion(String string, TypeRepository typeRepository) { final String[] split = string.split(Term.TYPE_SEPARATOR); if (split.length == 2) { final Type type = typeRepository.getTypeCreateIfNeeded(split[1]); if (type == null) { throw new LogicalExpressionRuntimeException( "Invalid type for variable: " + string); } return Pair.of(split[0], new Variable(type)); } else { return null; } }
Example #16
Source File: Literal.java From spf with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") private Literal(LogicalExpression predicate, LogicalExpression[] arguments, ITypeComparator typeComparator, TypeRepository typeRepository) { // Assert that the predicate is not null and has a complex type. assert predicate != null : String.format("Null predicate"); assert predicate.getType().isComplex() : String.format( "Predicate without a complex type: %s", predicate); this.predicate = predicate; this.arguments = arguments; // Compute the type. If the computed type is null, throw an exception. // Also check against null arguments. final Type[] argTypes = new Type[arguments.length]; // Track if there's only one sub-expression with free variables. We do // this to optimize and and try to re-use the set of free variables from // the sub-expression, if possible. boolean singleSubExpWithFreeVariables = true; // Track the index of the sub-expression with free variables, if there // is only one. If none has been observed, set to -1. Otherwise, 0 for // predicate, and i for argument i-1. int subExpWithFreeVariables = predicate.numFreeVariables() == 0 ? -1 : 0; for (int i = 0; i < arguments.length; ++i) { assert arguments[i] != null : "Null argument to literal"; argTypes[i] = arguments[i].getType(); if (singleSubExpWithFreeVariables && arguments[i].numFreeVariables() > 0) { if (subExpWithFreeVariables < 0) { subExpWithFreeVariables = i + 1; } else { singleSubExpWithFreeVariables = false; } } } // Set the set of free variables. if (singleSubExpWithFreeVariables) { if (subExpWithFreeVariables == 0) { this.freeVariables = predicate.getFreeVariables(); } else if (subExpWithFreeVariables > 0) { this.freeVariables = arguments[subExpWithFreeVariables - 1] .getFreeVariables(); } else { this.freeVariables = ReferenceSets.EMPTY_SET; } } else { // Case we have multiple sub-expression with free variables. We need // to take their union. This is a relatively expensive process, so // we tried to avoid it so far. final ReferenceSet<Variable> variables = new ReferenceOpenHashSet<Variable>(); if (predicate.numFreeVariables() > 0) { variables.addAll(predicate.getFreeVariables()); } for (int i = 0; i < arguments.length; ++i) { if (arguments[i].numFreeVariables() > 0) { variables.addAll(arguments[i].getFreeVariables()); } } this.freeVariables = ReferenceSets.unmodifiable(variables); } final Type[] impliedSignatureTypes = new Type[argTypes.length]; final Type literalType = computeLiteralTyping( (ComplexType) predicate.getType(), argTypes, typeComparator, typeRepository, impliedSignatureTypes); assert literalType != null : String.format( "Failed to compute literal type. predicate=%s, arguments=%s", predicate, Arrays.toString(arguments)); this.type = literalType; this.signature = impliedSignatureTypes; }
Example #17
Source File: Literal.java From spf with GNU General Public License v2.0 | 4 votes |
private static Type computeLiteralTyping(ComplexType predicateType, Type[] argTypes, ITypeComparator typeComparator, TypeRepository typeRepository, Type[] impliedSignatureTypes) { assert impliedSignatureTypes.length == argTypes.length : "Invalid length for array given to populate implied signature types"; // Calculate the type of the literal and verify the types of the // arguments with regard to the signature. Type currentDomain; Type currentRange = predicateType; // Counts the number of arguments for the current sequence. A sequence // might change if we switch to a different recursive type. int currentNumArgs = 0; int i = 0; while (i < argTypes.length && currentRange.isComplex()) { final Type argType = argTypes[i]; ++i; currentDomain = currentRange.getDomain(); currentRange = currentRange.getRange(); // If we have a recursive complex type, and the final return // type is complex, we might be seeing a switch to the argument // of the final return type. For example: (pred:<e*,<t,t>> boo:e // foo:e too:t). So try to switch to the type of the final range // before the validity check. if (!typeComparator.verifyArgType(currentDomain, argType) && currentRange instanceof RecursiveComplexType && ((RecursiveComplexType) currentRange).getFinalRange() .isComplex()) { // Verify that the current recursive range was given at least // the minimal number of arguments. if (currentNumArgs < ((RecursiveComplexType) currentRange) .getMinArgs()) { LOG.debug( "Recursive type %s requires a minimum of %d arguments, %d were provided.", currentRange, ((RecursiveComplexType) currentRange).getMinArgs(), currentNumArgs); return null; } // Need to update both the current domain and range, basically // switching to work with the complex final return type. currentDomain = ((ComplexType) ((RecursiveComplexType) currentRange) .getFinalRange()).getDomain(); currentRange = ((ComplexType) ((RecursiveComplexType) currentRange) .getFinalRange()).getRange(); currentNumArgs = 0; } // Validate the type of the argument against the current position in // the signature. if (!typeComparator.verifyArgType(currentDomain, argType)) { LOG.debug("Invalid argument type (%s) for signature type (%s)", argType, currentDomain); return null; } impliedSignatureTypes[i - 1] = currentDomain; currentNumArgs++; // Return null if we have more arguments than the signature // supports. if (i < argTypes.length && !currentRange.isComplex()) { LOG.debug("Too many arguments for predicate of type %s: %s", predicateType, argType); return null; } } if (currentRange instanceof RecursiveComplexType) { // Case special predicate, such as "and" final RecursiveComplexType recursivePredicateType = (RecursiveComplexType) currentRange; if (currentNumArgs >= recursivePredicateType.getMinArgs()) { return recursivePredicateType.getFinalRange(); } else { return typeRepository.getTypeCreateIfNeeded( recursivePredicateType.getDomain(), recursivePredicateType.getFinalRange(), new Option( recursivePredicateType.isOrderSensitive(), recursivePredicateType.getMinArgs() - currentNumArgs)); } } else { // Case regular complex type return currentRange; } }
Example #18
Source File: Lambda.java From spf with GNU General Public License v2.0 | 4 votes |
@Override public Lambda read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator, LogicalExpressionReader reader) { try { final LispReader lispReader = new LispReader(new StringReader( string)); // The first argument is the 'lambda' keyword. We just ignore // it. lispReader.next(); // The second argument is the variable definition. final Pair<String, Variable> variableDef = Variable .readVariableDefintion(lispReader.next(), typeRepository); if (variableDef == null) { throw new LogicalExpressionRuntimeException( "Invalid lambda argument: " + string); } // Update the scope mapping. mapping.push(variableDef.first(), variableDef.second()); // The next argument is the body expression. final LogicalExpression lambdaBody = reader.read( lispReader.next(), mapping, typeRepository, typeComparator); // Verify that we don't have any more elements. if (lispReader.hasNext()) { throw new LogicalExpressionRuntimeException(String.format( "Invalid lambda expression: %s", string)); } // Remove the variable from the mapping. if (mapping.pop(variableDef.first()) == null) { throw new LogicalExpressionRuntimeException( "Failed to remove variable from mapping. Something werid is happening: " + string); } return new Lambda(variableDef.second(), lambdaBody); } catch (final RuntimeException e) { LOG.error("Lambda syntax error: %s", string); throw e; } }
Example #19
Source File: LogicLanguageServices.java From spf with GNU General Public License v2.0 | 4 votes |
public static TypeRepository getTypeRepository() { return INSTANCE.typeRepository; }
Example #20
Source File: LogicalExpressionReader.java From spf with GNU General Public License v2.0 | 4 votes |
LOGEXP read(String string, ScopeMapping<String, LogicalExpression> mapping, TypeRepository typeRepository, ITypeComparator typeComparator, LogicalExpressionReader reader);
Example #21
Source File: ResultPrinter.java From UDepLambda with Apache License 2.0 | 4 votes |
public static void main(String[] args) { if (args.length == 0 || args.length % 2 != 0) { System.err .println("Specify pipeline arguments, e.g., annotator, languageCode, preprocess.capitalize. See the NlpPipelineTest file."); System.exit(0); } Map<String, String> options = new HashMap<>(); for (int i = 0; i < args.length; i += 2) { options.put(args[i], args[i + 1]); } Printer printer = new LogicalExpressionSimpleIndenter.Printer(" "); String defined_types = NlpPipeline.DEPLAMBDA_DEFINED_TYPES_FILE; try { TypeRepository types = new MutableTypeRepository(options.get(defined_types)); System.err.println(String.format("%s=%s", defined_types, options.get(defined_types))); LogicLanguageServices.setInstance(new LogicLanguageServices.Builder( types, new FlexibleTypeComparator()) .closeOntology(false) .setNumeralTypeName("i") // .setPrinter(new LogicalExpressionVjIndenter.Printer(" ")) .build()); } catch (IOException z) { z.printStackTrace(); } String input = read(System.in); JsonParser jsonParser = new JsonParser(); JsonObject jsonSentence = jsonParser .parse(input) .getAsJsonObject(); Sentence sentence = new Sentence(jsonSentence); System.out.println("----\nSentence: " + cleanString(""+jsonSentence.get("sentence"))); System.out.println("\nDepTree:\n" + TreePrinter.toIndentedString(sentence.getRootNode())); JsonPrimitive c = (JsonPrimitive) jsonSentence.get(SentenceKeys.DEPLAMBDA_EXPRESSION); String depLambda = cleanString(c.toString()); JsonElement logic = jsonSentence.get(SentenceKeys.DEPENDENCY_LAMBDA); System.out.println("\nDepLambda Expr:\n " + printer.toString(SimpleLogicalExpressionReader.read(depLambda))); System.out.println("\nDepLambda simplified: "); printJsonArray((JsonArray) logic); }
Example #22
Source File: LogicalExpressionSimpleIndenter.java From UDepLambda with Apache License 2.0 | 4 votes |
@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 #23
Source File: LogicLanguageServices.java From spf with GNU General Public License v2.0 | 2 votes |
/** * @param typeRepository * Type repository to be used by the system. * @param typeComparator * Type comparator to be used to compare types through the * system. Setting this accordingly allows to ignore certain * distinctions between finer types. */ public Builder(TypeRepository typeRepository, ITypeComparator typeComparator) { this.typeRepository = typeRepository; this.typeComparator = typeComparator; }
Example #24
Source File: LogicLanguageServices.java From spf with GNU General Public License v2.0 | 2 votes |
/** * @param typeRepository * Type repository to be used by the system. */ public Builder(TypeRepository typeRepository) { this(typeRepository, new StrictTypeComparator()); }