Java Code Examples for edu.cornell.cs.nlp.spf.mr.lambda.LogicalConstant#read()

The following examples show how to use edu.cornell.cs.nlp.spf.mr.lambda.LogicalConstant#read() . 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: ExtractTypedSubExpressionTest.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test2() {
	final LogicalExpression exp = TestServices
			.getCategoryServices()
			.readSemantics(
					"(lambda $0:<e,t> (lambda $1:e (and:<t*,t> ($0 $1) (boo:<e,<e,t>> $1 goo:e))))");
	final Result result = ExtractTypedSubExpression.of(exp, LogicalConstant
			.read("p:<e,<e,t>>"), LogicLanguageServices.getTypeRepository()
			.getTypeCreateIfNeeded("<e,<e,t>>"), false);
	Assert.assertNotNull(result);
	final LogicalExpression expectedSubExp = LogicalConstant
			.read("boo:<e,<e,t>>");
	final LogicalExpression expectedRemainder = TestServices
			.getCategoryServices()
			.readSemantics(
					"(lambda $0:<e,t> (lambda $1:e (and:<t*,t> ($0 $1) (p:<e,<e,t>> $1 goo:e))))");
	Assert.assertEquals(expectedRemainder,
			result.getExpressionWithPlaceholder());
	Assert.assertEquals(expectedSubExp, result.getExtractedSubExpression());
}
 
Example 2
Source File: ExtractTypedSubExpressionTest.java    From amr with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test3() {
	final LogicalExpression exp = TestServices
			.getCategoryServices()
			.readSemantics(
					"(lambda $0:<e,t> (lambda $1:e (and:<t*,t> (do:<e,<e,<e,t>>> roo:e $1 (a:<<e,t>,e> (lambda $0:e (boo:<e,<e,t>> $0 too:e)))) ($0 $1) (boo:<e,<e,t>> $1 goo:e))))");
	final Result result = ExtractTypedSubExpression.of(exp, LogicalConstant
			.read("p:<e,<e,t>>"), LogicLanguageServices.getTypeRepository()
			.getTypeCreateIfNeeded("<e,<e,t>>"), false);
	Assert.assertNotNull(result);
	final LogicalExpression expectedSubExp = LogicalConstant
			.read("boo:<e,<e,t>>");
	final LogicalExpression expectedRemainder = TestServices
			.getCategoryServices()
			.readSemantics(
					"(lambda $0:<e,t> (lambda $1:e (and:<t*,t> (do:<e,<e,<e,t>>> roo:e $1 (a:<<e,t>,e> (lambda $0:e (boo:<e,<e,t>> $0 too:e)))) ($0 $1) (p:<e,<e,t>> $1 goo:e))))");
	Assert.assertEquals(expectedRemainder,
			result.getExpressionWithPlaceholder());
	Assert.assertEquals(expectedSubExp, result.getExtractedSubExpression());
}
 
Example 3
Source File: LogicalExpressionApplicationTest.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test1() {
	final Variable variable = new Variable(LogicLanguageServices
			.getTypeRepository().getEntityType());
	final LogicalExpression e1 = new Lambda(variable, new Literal(
			LogicalConstant.read("boo:<e,<<e,t>,t>>"), ArrayUtils.create(
					variable,
					new Lambda(variable,
							new Literal(LogicalConstant.read("goo:<e,t>"),
									ArrayUtils.create(variable))))));
	final LogicalExpression e2 = LogicalExpression
			.read("(lambda $0:e (boo:<e,<<e,t>,t>> $0 (lambda $1:e (goo:<e,t> $1))))");
	Assert.assertEquals(e2, e1);

	final LogicalExpression result1 = TestServices.getCategoryServices()
			.apply(e1, LogicalConstant.read("foo:e"));
	final LogicalExpression result2 = TestServices.getCategoryServices()
			.apply(e2, LogicalConstant.read("foo:e"));
	Assert.assertEquals(result1, result2);
	System.out.println(result1);
}
 
Example 4
Source File: DatesGenerator.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DatesGenerator create(Parameters params, IResourceRepository repo) {
	return new DatesGenerator(params.get("label", "dyn-dates"),
			LogicalConstant.read(params.get("day")),
			LogicalConstant.read(params.get("year")),
			LogicalConstant.read(params.get("month")),
			LogicalConstant.read(params.get("entityType")),
			Syntax.read(params.get("syntax")));
}
 
Example 5
Source File: NamedEntityGenerator.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
public NamedEntityGenerator(
		ICategoryServices<LogicalExpression> categoryServices,
		String baseLabel) {
	this.baseLabel = baseLabel;
	this.helperSemantics = categoryServices
			.readSemantics("(lambda $0:e (and:<t*,t> (TYPE:<e,t> $0) (c_name:<e,<e,t>> $0 "
					+ "(a:<id,<<e,t>,e>> na:id (lambda $1:e (and:<t*,t> (name:<e,t> $1) (c_op:<e,<txt,t>> $1 NAME:txt)))))))");
	this.typePlaceholder = LogicalConstant.read("TYPE:<e,t>");
	this.namePlceholder = LogicalConstant.read("NAME:txt");
	this.singularNounSyntax = Syntax.read("N[sg]");
	this.singularNPSyntax = Syntax.read("NP[sg]");
}
 
Example 6
Source File: NamedEntitiesCandidates.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
	try {
		// //////////////////////////////////////////
		// Init logging
		// //////////////////////////////////////////

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

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

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

		final LogicalConstant namePredicate = LogicalConstant
				.read("c_name:<e,<txt,t>>");

		final AMROntology ontology = AMROntology.read(new File(args[1]));

		for (final SingleSentence sentence : SingleSentenceCollection.read(
				new File(args[2]), new Tokenizer())) {
			System.out.println(sentence.getSample());
			for (final Pair<String, String> entityType : GetNamedEntities
					.of(sentence.getLabel(), namePredicate)) {
				if (entityType.second() == null
						|| !ontology.isType(entityType.second())) {
					System.out.println(entityType.first());
				} else {
					System.out.println(String.format("%s\t%s",
							entityType.first(), entityType.second()));
				}
			}
			System.out.println();
		}
	} catch (final IOException e) {
		throw new IllegalStateException(e);
	}
}
 
Example 7
Source File: DatesGeneratorTest.java    From amr with GNU General Public License v2.0 5 votes vote down vote up
public DatesGeneratorTest() {
	TestServices.init();
	this.gen = new DatesGenerator("dyn-dates",
			LogicalConstant.read("c_day:<e,<i,t>>"),
			LogicalConstant.read("c_year:<e,<i,t>>"),
			LogicalConstant.read("c_month:<e,<i,t>>"),
			LogicalConstant.read("date-entity:<e,t>"),
			Syntax.read("NP[sg]"));
}
 
Example 8
Source File: ConstantsReader.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
public static List<LogicalConstant> readConstantsFile(File file)
		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 List<LogicalConstant> constants = new LinkedList<LogicalConstant>();
	final LispReader lispReader = new LispReader(new StringReader(
			strippedFile.toString()));
	while (lispReader.hasNext()) {
		final LogicalConstant exp = LogicalConstant.read(lispReader.next());
		constants.add(exp);
	}

	return constants;

}
 
Example 9
Source File: LogicalExpressionEqualsTest.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test7() {
	final Variable variable = new Variable(LogicLanguageServices
			.getTypeRepository().getEntityType());
	final LogicalExpression e1 = new Lambda(variable, new Literal(
			LogicalConstant.read("boo:<e,<<e,t>,t>>"), ArrayUtils.create(
					variable,
					new Lambda(variable,
							new Literal(LogicalConstant.read("goo:<e,t>"),
									ArrayUtils.create(variable))))));
	final LogicalExpression e2 = LogicalExpression
			.read("(lambda $0:e (boo:<e,<<e,t>,t>> $0 (lambda $1:e (goo:<e,t> $1))))");
	Assert.assertEquals(e2, e1);
}
 
Example 10
Source File: LogicalExpressionEqualsTest.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test9() {
	final Variable variable = new Variable(LogicLanguageServices
			.getTypeRepository().getEntityType());
	final LogicalExpression e1 = new Lambda(variable, new Lambda(variable,
			new Literal(LogicalConstant.read("pred:<e,<e,t>>"),
					ArrayUtils.create(variable, variable))));
	final LogicalExpression e2 = LogicalExpression
			.read("(lambda $0:e (lambda $1:e (pred:<e,<e,t>> $0 $1)))");
	Assert.assertNotEquals(e2, e1);
	Assert.assertNotEquals(e1, e2);
}
 
Example 11
Source File: AToExistsTest.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
public AToExistsTest() {
	// Make sure test services is initialized
	TestServices.init();
	this.existsPredicate = LogicalConstant.read("exists:<<e,t>,t>");
	this.aPredicate = LogicalConstant.read("a:<<e,t>,e>");
	this.equalsPredicates = new HashMap<Type, LogicalConstant>();
	this.equalsPredicates.put(LogicLanguageServices.getTypeRepository()
			.getEntityType(), LogicalConstant.read("eq:<e,<e,t>>"));

}
 
Example 12
Source File: DateStamp.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
public DateStamp() {
	super(UnaryRuleName.create(LABEL));
	this.sourceSyntax = Syntax.read("NP[sg]");
	this.dateInstanceType = LogicalConstant.read("date-entity:<e,t>");
}
 
Example 13
Source File: TableTest.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test() {
	final ColumnHeader h1 = new ColumnHeader(new LogicalConstantNode(
			LogicalConstant.read("boo:e"), ArrayUtils.create(
					LogicalConstant.read("boo1:e"),
					LogicalConstant.read("boo2:e")), 1));
	final ColumnHeader h2 = new ColumnHeader(new LogicalConstantNode(
			LogicalConstant.read("bo:e"), ArrayUtils.create(
					LogicalConstant.read("bo1:e"),
					LogicalConstant.read("bo2:e"),
					LogicalConstant.read("bo3:e")), 2));
	final Table table = new Table(false, h1, h2);
	table.setAll(1.0);
	Assert.assertEquals(
			3.0,
			table.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo1:e"))), 0.0);
	Assert.assertEquals(
			3.0,
			table.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo2:e"))), 0.0);
	Assert.assertEquals(
			2.0,
			table.get(MappingPair.of(h2.getNode(),
					LogicalConstant.read("bo2:e"))), 0.0);
	for (final LogicalExpression h1Assignment : h1.getNode()
			.slowGetAssignments()) {
		for (final LogicalExpression h2Assignment : h2.getNode()
				.slowGetAssignments()) {
			Assert.assertEquals(1.0, table.get(
					MappingPair.of(h1.getNode(), h1Assignment),
					MappingPair.of(h2.getNode(), h2Assignment)), 0.0);
		}
	}

	final Table table2 = new Table(false, h1);
	table2.setAll(2.0);
	table2.set(10.0,
			MappingPair.of(h1.getNode(), LogicalConstant.read("boo1:e")));
	table2.set(3.0,
			MappingPair.of(h1.getNode(), LogicalConstant.read("boo2:e")));
	for (final LogicalExpression assignment : h1.getNode()
			.slowGetAssignments()) {
		final MappingPair pair = MappingPair.of(h1.getNode(), assignment);
		table2.multiply(table.get(pair), pair);
	}
	Assert.assertEquals(
			30.0,
			table2.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo1:e"))), 0.0);
	Assert.assertEquals(
			9.0,
			table2.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo2:e"))), 0.0);
}
 
Example 14
Source File: TableTest.java    From amr with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test2() {
	final ColumnHeader h1 = new ColumnHeader(new LogicalConstantNode(
			LogicalConstant.read("boo:e"), ArrayUtils.create(
					LogicalConstant.read("boo1:e"),
					LogicalConstant.read("boo2:e")), 1));
	final ColumnHeader h2 = new ColumnHeader(new LogicalConstantNode(
			LogicalConstant.read("bo:e"), ArrayUtils.create(
					LogicalConstant.read("bo1:e"),
					LogicalConstant.read("bo2:e"),
					LogicalConstant.read("bo3:e")), 2));
	final Table table = new Table(true, h1, h2);
	table.setAll(1.0);
	Assert.assertEquals(
			2.0986122886681096,
			table.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo1:e"))), 0.1);
	Assert.assertEquals(
			2.0986122886681096,
			table.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo2:e"))), 0.1);
	Assert.assertEquals(
			1.6931471805599452,
			table.get(MappingPair.of(h2.getNode(),
					LogicalConstant.read("bo2:e"))), 0.1);
	for (final LogicalExpression h1Assignment : h1.getNode()
			.slowGetAssignments()) {
		for (final LogicalExpression h2Assignment : h2.getNode()
				.slowGetAssignments()) {
			Assert.assertEquals(1.0, table.get(
					MappingPair.of(h1.getNode(), h1Assignment),
					MappingPair.of(h2.getNode(), h2Assignment)), 0.0);
		}
	}

	final Table table2 = new Table(true, h1);
	table2.setAll(2.0);
	table2.set(10.0,
			MappingPair.of(h1.getNode(), LogicalConstant.read("boo1:e")));
	table2.set(3.0,
			MappingPair.of(h1.getNode(), LogicalConstant.read("boo2:e")));
	for (final LogicalExpression assignment : h1.getNode()
			.slowGetAssignments()) {
		final MappingPair pair = MappingPair.of(h1.getNode(), assignment);
		table2.multiply(table.get(pair), pair);
	}
	Assert.assertEquals(
			10.0 * 2.0986122886681096,
			table2.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo1:e"))), 0.1);
	Assert.assertEquals(
			3.0 * 2.0986122886681096,
			table2.get(MappingPair.of(h1.getNode(),
					LogicalConstant.read("boo2:e"))), 0.1);
}