def test_flatten(self): """ Check the flattening of coordinations during beta-reduction is working """ # A 3-way coordination sem0 = semantics_from_string(r"(\$x.leftonto($x)) & (\$x.rightonto($x)) & (\$x.leftonto($x))") # We've already checked that this comes out nested as expected # Now check it flattens during beta-reduction sem0.beta_reduce() self.assertIsInstance(sem0.lf, Coordination) self.assertEqual(len(sem0.lf), 3) # Try the exact same thing, but with brackets to make it combine the # other way initially sem1 = semantics_from_string(r"((\$x.leftonto($x)) & (\$x.rightonto($x))) & (\$x.leftonto($x))") sem1.beta_reduce() self.assertIsInstance(sem1.lf, Coordination) self.assertEqual(len(sem1.lf), 3) # This should produce the same thing as before self.assertEqual(sem0, sem1) # This doesn't make sense, but Coordination shouldn't complain sem0 = semantics_from_string(r"<1,2> & ((\$x.rightonto($x)) & (\$x.leftonto($x))) & <3,4> & <5,6>") sem1 = semantics_from_string(r"<1,2> & (\$x.rightonto($x)) & (\$x.leftonto($x)) & <3,4> & <5,6>") sem0.beta_reduce() sem1.beta_reduce() self.assertEqual(sem0, sem1)
def test_leftonto(self): # Check the leftonto gets carried to the first item of the list sem = semantics_from_string("leftonto([<1,2>])") sem.beta_reduce() self.assertIsInstance(sem.lf, List) self.assertIsInstance(sem.lf[0], FunctionApplication) self.assertIsInstance(sem.lf[0].functor, Leftonto) # Check the same thing with a multi-element list sem = semantics_from_string("leftonto([<1,2>,<3,4>])") sem.beta_reduce() self.assertIsInstance(sem.lf, List) self.assertIsInstance(sem.lf[0], FunctionApplication) self.assertIsInstance(sem.lf[0].functor, Leftonto) # Now with multiple predicates sem = semantics_from_string("leftonto(leftonto([<1,2>]))") sem.beta_reduce() self.assertIsInstance(sem.lf, List) self.assertIsInstance(sem.lf[0], FunctionApplication) self.assertIsInstance(sem.lf[0].functor, Leftonto) self.assertIsInstance(sem.lf[0].argument, FunctionApplication) self.assertIsInstance(sem.lf[0].argument.functor, Leftonto) # Check rightonto works as well sem = semantics_from_string("rightonto([<1,2>,<3,4>])") sem.beta_reduce() self.assertIsInstance(sem.lf, List) self.assertIsInstance(sem.lf[0], FunctionApplication) self.assertIsInstance(sem.lf[0].functor, Rightonto)
def test_general(self): # Try doing some mixtures of things # Just make sure the parsing of these doesn't generate any errors sem0 = semantics_from_string("[<1,2>,<3,4>]+[<1,2>]") sem1 = semantics_from_string("\\$x. ($x <1,2>)") sem2 = semantics_from_string("\\$x. leftonto($x)") sem3 = semantics_from_string("\\$x. leftonto(($x <1,2>))") sem4 = semantics_from_string("[\\$x. leftonto(($x [<1,2>]))]")
def test_predicates(self): sem = semantics_from_string("leftonto($X)") # Check this returned the right type self.assertIsInstance(sem.lf, FunctionApplication) self.assertIsInstance(sem.lf.functor, Leftonto) sem = semantics_from_string("rightonto($X)") # Check this returned the right type self.assertIsInstance(sem.lf, FunctionApplication) self.assertIsInstance(sem.lf.functor, Rightonto)
def test_now(self): # Build a now predicate and check there are no errors sem0 = semantics_from_string(r"now@1($x)") sem0.beta_reduce() sem1 = semantics_from_string(r"\$x.now@1(leftonto($x))") sem1.beta_reduce() sem2 = semantics_from_string(r"now@10((\$x.leftonto($x)) & (\$x.leftonto(leftonto($x))))") sem2.beta_reduce()
def test_coordination(self): # Create a simple coordination sem = semantics_from_string(r"(\$x.leftonto($x)) & (\$x.leftonto($x))") self.assertIsInstance(sem.lf, Coordination) self.assertEqual(len(sem.lf), 2) # Creat a three-way coordination (in fact it will be nested) sem = semantics_from_string(r"(\$x.leftonto($x)) & (\$x.rightonto($x)) & (\$x.leftonto($x))") self.assertIsInstance(sem.lf, Coordination) # This is actually length 2, because it's nested # It would beta-reduce to length 3 self.assertEqual(len(sem.lf), 2) self.assertIsInstance(sem.lf[1], Coordination) self.assertEqual(len(sem.lf[1]), 2)
def test_list(self): sem = semantics_from_string("[<1,2>]") # Check this returned the right type self.assertIsInstance(sem.lf, List) # Check the list was correctly interpreted self.assertEqual(len(sem.lf), 1) # And the coordinate in it self.assertIsInstance(sem.lf[0], EnharmonicCoordinate) self.assertEqual((sem.lf[0].x,sem.lf[0].y), (1,2)) # Multi-element list sem = semantics_from_string("[<1,2>,<3,4>]") # Check this returned the right type self.assertIsInstance(sem.lf, List)
def test_call_me_irresponsible(self): # Call Me Irresponsible cadence sem = semantics_from_string(r"[<0,0>, "\ r"(((\$y.(((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\ r"& (\$x.leftonto(leftonto($x)))) leftonto($y))) "\ r"& (\$x.leftonto(leftonto($x)))) <0,0>)]") trees = semantics_to_dependency_trees(sem) self.assertEqual(len(trees), 2) # One node in first tree self.assertEqual(len(trees[0]), 1) # Second tree is the hard one! # Should look like this: # <(0,0)/(0,0)> # (leftonto( # leftonto(leftonto(leftonto(leftonto(leftonto)))) # leftonto(leftonto)) # leftonto(leftonto)) # Check some things about the structure tree = trees[1].root # First split (just after root) self.assertEqual(len(tree), 2) # Other split self.assertEqual(len(tree[0]), 2) # Others should not split self.assertEqual(len(tree[0][0]), 1) self.assertEqual(len(tree[0][0][0]), 1) self.assertEqual(len(tree[0][0][0][0]), 1) self.assertEqual(len(tree[0][0][0][0][0]), 1) # This is the lowest leaf self.assertTrue(tree[0][0][0][0][0][0].is_leaf())
def test_call_me_irresponsible(self): # Call Me Irresponsible cadence sem = semantics_from_string( r"[<0,0>, " r"(((\$y.(((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) " r"& (\$x.leftonto(leftonto($x)))) leftonto($y))) " r"& (\$x.leftonto(leftonto($x)))) <0,0>)]" ) trees = semantics_to_dependency_trees(sem) self.assertEqual(len(trees), 2) # One node in first tree self.assertEqual(len(trees[0]), 1) # Second tree is the hard one! # Should look like this: # <(0,0)/(0,0)> # (leftonto( # leftonto(leftonto(leftonto(leftonto(leftonto)))) # leftonto(leftonto)) # leftonto(leftonto)) # Check some things about the structure tree = trees[1].root # First split (just after root) self.assertEqual(len(tree), 2) # Other split self.assertEqual(len(tree[0]), 2) # Others should not split self.assertEqual(len(tree[0][0]), 1) self.assertEqual(len(tree[0][0][0]), 1) self.assertEqual(len(tree[0][0][0][0]), 1) self.assertEqual(len(tree[0][0][0][0][0]), 1) # This is the lowest leaf self.assertTrue(tree[0][0][0][0][0][0].is_leaf())
def test_coordinate(self): # Build a semantics of a single point sem = semantics_from_string("[<3,1>]") # Build a tree for this trees = semantics_to_dependency_trees(sem) # Should be only one tree self.assertEqual(len(trees), 1) # Should just contain the root node corresponding to the point self.assertEqual(len(trees[0]), 1)
def test_coordinate(self): # Build a semantics of a single point sem = semantics_from_string("[<3,1>]") # Build a tree for this trees = semantics_to_dependency_trees(sem) # Should be only one tree self.assertEqual(len(trees), 1) # Should just contain the root node corresponding to the point self.assertEqual(len(trees[0]), 1)
def test_coordinates(self): # Build a semantics of two points sem = semantics_from_string("[<3,1>, <2,2>]") # Build a tree for this trees = semantics_to_dependency_trees(sem) # Should be two trees self.assertEqual(len(trees), 2) # Should just contain the root node each corresponding to the points self.assertEqual(len(trees[0]), 1) self.assertEqual(len(trees[1]), 1)
def test_coordinates(self): # Build a semantics of two points sem = semantics_from_string("[<3,1>, <2,2>]") # Build a tree for this trees = semantics_to_dependency_trees(sem) # Should be two trees self.assertEqual(len(trees), 2) # Should just contain the root node each corresponding to the points self.assertEqual(len(trees[0]), 1) self.assertEqual(len(trees[1]), 1)
def test_variable(self): sem = semantics_from_string("$var13") # Check this returned the right type self.assertIsInstance(sem.lf, Variable) # Check the variable was correctly interpreted self.assertEqual(sem.lf.name, "var") self.assertEqual(sem.lf.index, 13) # Try one without a number sem = semantics_from_string("$var") # Check the variable was correctly interpreted self.assertEqual(sem.lf.name, "var") self.assertEqual(sem.lf.index, 0) # And a single-character sem = semantics_from_string("$X") # Check the variable was correctly interpreted self.assertEqual(sem.lf.name, "X") self.assertEqual(sem.lf.index, 0)
def test_predicates(self): # Build a semantics of a leftonto/rightonto with resolution sem = semantics_from_string("[leftonto(<0,0>)]") # Build a tree for this trees = semantics_to_dependency_trees(sem) # Should have two nodes, leaf should be leftonto self.assertEqual(len(trees[0]), 2) self.assertEqual(trees[0][0].label, "leftonto") sem = semantics_from_string("[rightonto(<0,0>)]") trees = semantics_to_dependency_trees(sem) # Should have two nodes, leaf should be rightonto self.assertEqual(len(trees[0]), 2) self.assertEqual(trees[0][0].label, "rightonto") # Try multiple applications sem = semantics_from_string("[leftonto(leftonto(leftonto(<0,0>)))]") trees = semantics_to_dependency_trees(sem) # Should have 4 nodes self.assertEqual(len(trees[0]), 4)
def test_predicates(self): # Build a semantics of a leftonto/rightonto with resolution sem = semantics_from_string("[leftonto(<0,0>)]") # Build a tree for this trees = semantics_to_dependency_trees(sem) # Should have two nodes, leaf should be leftonto self.assertEqual(len(trees[0]), 2) self.assertEqual(trees[0][0].label, "leftonto") sem = semantics_from_string("[rightonto(<0,0>)]") trees = semantics_to_dependency_trees(sem) # Should have two nodes, leaf should be rightonto self.assertEqual(len(trees[0]), 2) self.assertEqual(trees[0][0].label, "rightonto") # Try multiple applications sem = semantics_from_string("[leftonto(leftonto(leftonto(<0,0>)))]") trees = semantics_to_dependency_trees(sem) # Should have 4 nodes self.assertEqual(len(trees[0]), 4)
def test_coordination(self): # Build a semantics of a coordination sem = semantics_from_string(r"[((\$x.leftonto($x)) & (\$y.leftonto($y)) leftonto(<0,0>))]") trees = semantics_to_dependency_trees(sem) # Should look like this: # <(0,0)/(0,0)>(leftonto(leftonto leftonto)) self.assertEqual(len(trees), 1) tree = trees[0] self.assertEqual(tree.root.label, (0, 0)) self.assertEqual(tree[0].label, "leftonto") self.assertEqual(tree[0][0].label, "leftonto") self.assertEqual(tree[0][1].label, "leftonto")
def test_coordination(self): # Build a semantics of a coordination sem = semantics_from_string(r"[((\$x.leftonto($x)) & (\$y.leftonto($y)) leftonto(<0,0>))]") trees = semantics_to_dependency_trees(sem) # Should look like this: # <(0,0)/(0,0)>(leftonto(leftonto leftonto)) self.assertEqual(len(trees), 1) tree = trees[0] self.assertEqual(tree.root.label, (0,0)) self.assertEqual(tree[0].label, "leftonto") self.assertEqual(tree[0][0].label, "leftonto") self.assertEqual(tree[0][1].label, "leftonto")
def test_lambda_abstraction(self): sem = semantics_from_string("\\$X.$X") # Check this returned the right type self.assertIsInstance(sem.lf, LambdaAbstraction) # Check the variable was correctly interpreted self.assertIsInstance(sem.lf.variable, Variable) # Check the expression was correctly interpreted self.assertIsInstance(sem.lf.expression, Variable) # Something different sem = semantics_from_string("\\$X.<1,2>") self.assertIsInstance(sem.lf.expression, EnharmonicCoordinate) # Try a multi-variable abstraction sem = semantics_from_string("\\$X,$Y.$X") # Check this returned the right type self.assertIsInstance(sem.lf, LambdaAbstraction) # Check the first variable was correctly interpreted self.assertIsInstance(sem.lf.variable, Variable) # Check the second abstraction is right too self.assertIsInstance(sem.lf.expression, LambdaAbstraction) self.assertIsInstance(sem.lf.expression.variable, Variable)
def test_function_application(self): sem = semantics_from_string("($X $X)") # Check this returned the right type self.assertIsInstance(sem.lf, FunctionApplication) # Check the variable was correctly interpreted self.assertIsInstance(sem.lf.functor, Variable) # Check the expression was correctly interpreted self.assertIsInstance(sem.lf.argument, Variable) sem = semantics_from_string("($X <1,2>)") # Check this returned the right type self.assertIsInstance(sem.lf, FunctionApplication) # Check the variable was correctly interpreted self.assertIsInstance(sem.lf.functor, Variable) # Check the expression was correctly interpreted self.assertIsInstance(sem.lf.argument, EnharmonicCoordinate) sem = semantics_from_string("(($X <1,2>) <3,4>)") # Check this returned the right type self.assertIsInstance(sem.lf, FunctionApplication) # Check the variable was correctly interpreted self.assertIsInstance(sem.lf.functor, FunctionApplication) # Check the expression was correctly interpreted self.assertIsInstance(sem.lf.argument, EnharmonicCoordinate)
def test_function_application(self): # Check that \x.x works as it should: this should reduce to just a coord sem0 = semantics_from_string(r"(\$x.$x <1,2>)") sem0.beta_reduce() sem1 = semantics_from_string("<1,2>") self.assertEqual(sem0, sem1) # Try a trivial abstraction that throws away its argument sem0 = semantics_from_string(r"(\$x.<1,2> $y)") sem0.beta_reduce() sem1 = semantics_from_string("<1,2>") self.assertEqual(sem0, sem1) # Try a multiple abstraction applied to all its args sem0 = semantics_from_string(r"((\$x,$y.$x <1,2>) <3,4>)") sem0.beta_reduce() sem1 = semantics_from_string("<1,2>") self.assertEqual(sem0, sem1)
def test_cat(self): sem = semantics_from_string("[<1,2>]+[<3,4>]") # Check this returned the right type self.assertIsInstance(sem.lf, ListCat) # Check the cat was correctly interpreted self.assertEqual(len(sem.lf), 2)
def test_coordinate(self): sem = semantics_from_string("<3,4>") # Check this returned the right type self.assertIsInstance(sem.lf, EnharmonicCoordinate) # Check the coordinate was correctly interpreted self.assertEqual(sem.lf.harmonic_coord, (3,4))