def setUp(self):
        from jazzparser.formalisms.music_halfspan.rules import ApplicationRule
        from jazzparser.formalisms.music_halfspan.syntax import (
            AtomicCategory,
            ComplexCategory,
            HalfCategory,
            Sign,
            Slash,
        )
        from jazzparser.formalisms.music_halfspan.semantics import DummyLogicalForm, Semantics
        from jazzparser.grammar import Grammar

        # Use the default grammar
        self.grammar = Grammar()

        # Get a rule to instantiate: forward application
        self.rule = self.grammar.rules_by_name["appf"]
        # Create some categories we can store as if the rule applied to them
        # Create an atomic category
        self.cat0 = AtomicCategory(HalfCategory("I"), HalfCategory("I"))
        # Create a complex category that could be applied to the atomic one
        self.cat1 = ComplexCategory(
            HalfCategory("V", function="D"), Slash(True), HalfCategory("I", function=["D", "T"])
        )
        # An atomic category, as if 0 was applied to 1
        self.cat2 = AtomicCategory(HalfCategory("V", function="D"), HalfCategory("I"))

        # A dummy semantics to use for all signs
        dummy_sem = Semantics(DummyLogicalForm())

        # Create signs from the categories
        self.sign0 = Sign(self.cat0, dummy_sem.copy())
        self.sign1 = Sign(self.cat1, dummy_sem.copy())
        self.sign2 = Sign(self.cat2, dummy_sem.copy())
Esempio n. 2
0
    def test_back_conversion(self):
        """
		Creates a tonal space path, converts it to state labels and converts 
		it back again. This should produce the original path if all goes 
		well.
		
		Note that the result of the back conversion will always have the 
		path shifted so it starts as close as possible to the origin. This is 
		correct behaviour: the state labels don't encode the enharmonic 
		block that the path starts in and it is merely by convention that we 
		assume the start point.
		
		Each path-chord sequence pair also gives the expected output, which 
		may differ from the original path only in this respect.
		
		@todo: update this test
		
		"""
        # Just return for now: I've not had a chance to update this
        # lf_chords_to_states no longer exists
        return
        self.longMessage = True
        # Run the test on a whole set of paths
        for (coords, chords, output) in self.PATHS:
            # Build a CoordinateList for the path
            ens = [
                EnharmonicCoordinate.from_harmonic_coord((x, y))
                for (x, y, fun) in coords
            ]
            pcs = [PathCoordinate.from_enharmonic_coord(en) for en in ens]
            time = 0
            for pc, (__, __, fun) in zip(pcs, coords):
                pc.function = fun
                pc.duration = 1
                pc.time = time
                time += 1
            path = Semantics(CoordinateList(items=pcs))
            # Build the list of chords
            chords = [Chord.from_name(crd).to_db_mirror() for crd in chords]
            for chord in chords:
                chord.duration = 1
            # Try converting it to states
            states = lf_chords_to_states(path, chords)
            # Now try converting it back
            back = states_chords_to_lf(zip(states, chords))

            # Check that we got the same coordinates out
            in_coords = [(x, y) for (x, y, fun) in output]
            in_funs = [fun for (x, y, fun) in output]
            out_coords = [point.harmonic_coord for point in back.lf]
            out_funs = [point.function for point in back.lf]

            self.assertEqual(
                in_coords,
                out_coords,
                msg=
                "coordinates converted to states and back produced something different.\nState labels:\n%s"
                % (states))
            self.assertEqual(
                in_funs,
                out_funs,
                msg=
                "coordinates converted to states and back produced different functions.\nState labels:\n%s"
                % (states))
Esempio n. 3
0
    def test_paper_example(self):
        """
        Generates the example that we used in the paper (from Alice in 
        Wonderland) as if it's coming from the combinators. Checks that 
        the right overall LF comes out.
        
        This is pretty insane, but a great test that the semantics is 
        behaving correctly in the contexts in which we'll be using it.
        
        """
        sem = semantics_from_string
        
        # Lexical
        sem_0_1 = sem(r"[<0,0>]")
        sem_1_2 = sem(r"\$x.$x")
        # Bapply
        sem_0_2 = apply(sem_1_2, sem_0_1)
        self.assertTrue(sem_0_2.alpha_equivalent(sem(r"[<0,0>]")))
        
        # Lexical
        sem_2_3 = sem(r"\$x.leftonto($x)")
        sem_3_4 = sem(r"\$x.leftonto($x)")
        # Fcomp
        sem_2_4 = compose(sem_2_3, sem_3_4)
        self.assertTrue(sem_2_4.alpha_equivalent(
                sem(r"\$x.leftonto(leftonto($x))")))
        
        # Lexical
        sem_4_5 = sem(r"\$x.leftonto($x)")
        # Fcomp
        sem_2_5 = compose(sem_2_4, sem_4_5)
        self.assertTrue(sem_2_5.alpha_equivalent(
                sem(r"\$x.leftonto(leftonto(leftonto($x)))")))
        
        # Lexical
        sem_5_6 = sem(r"\$x.leftonto($x)")
        # Fcomp
        sem_2_6 = compose(sem_2_5, sem_5_6)
        self.assertTrue(sem_2_6.alpha_equivalent(
                sem(r"\$x.leftonto(leftonto(leftonto(leftonto($x))))")))

        # Lexical
        sem_6_7 = sem(r"\$x.leftonto($x)")
        # Fcomp
        sem_2_7 = compose(sem_2_6, sem_6_7)
        self.assertTrue(sem_2_7.alpha_equivalent(
                sem(r"\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))")))

        # Lexical
        sem_7_8 = sem(r"\$x.leftonto($x)")
        sem_8_9 = sem(r"\$x.$x")
        # Fcomp
        sem_7_9 = compose(sem_7_8, sem_8_9)
        self.assertTrue(sem_7_9.alpha_equivalent(
                sem(r"\$x.leftonto($x)")))
        
        # Lexical
        sem_9_10 = sem(r"\$x.leftonto($x)")
        # Fcomp
        sem_7_10 = compose(sem_7_9, sem_9_10)
        self.assertTrue(sem_7_10.alpha_equivalent(
                sem(r"\$x.leftonto(leftonto($x))")))
        
        # Coord
        sem_2_10 = Semantics(Coordination([sem_2_7.lf, sem_7_10.lf]))
        sem_2_10.beta_reduce()
        semtest = sem(r"(\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\
                        "& (\$x.leftonto(leftonto($x)))")
        semtest.beta_reduce()
        self.assertTrue(sem_2_10.alpha_equivalent(semtest))
        
        # Lexical
        sem_10_11 = sem(r"\$x.leftonto($x)")
        # Fcomp
        sem_2_11 = compose(sem_2_10, sem_10_11)
        semtest = sem(r"\$y.("\
                        "((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\
                         "& (\$x.leftonto(leftonto($x)))) leftonto($y))")
        semtest.beta_reduce()
        self.assertTrue(sem_2_11.alpha_equivalent(semtest))
        
        # Skip a couple of lexical LFs
        sem_11_13 = sem(r"\$x.leftonto(leftonto($x))")
        # Coord
        sem_2_13 = Semantics(Coordination([sem_2_11.lf, sem_11_13.lf]))
        sem_2_13.beta_reduce()
        semtest = sem(r"(\$y.("\
                        "((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\
                         "& (\$x.leftonto(leftonto($x)))) leftonto($y))) "\
                        "& (\$x.leftonto(leftonto($x)))")
        semtest.beta_reduce()
        self.assertTrue(sem_2_13.alpha_equivalent(semtest))
        
        # Lexical
        sem_13_14 = sem(r"[<0,0>]")
        # Fapply
        sem_2_14 = apply(sem_2_13, sem_13_14)
        semtest = sem(r"((\$y.("\
                        "((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\
                         "& (\$x.leftonto(leftonto($x)))) leftonto($y))) "\
                        "& (\$x.leftonto(leftonto($x))) [<0,0>])")
        semtest.beta_reduce()
        self.assertTrue(sem_2_14.alpha_equivalent(semtest))
        
        # Finally, development
        sem_0_14 = Semantics(ListCat([sem_0_2.lf, sem_2_14.lf]))
        sem_0_14.beta_reduce()
        semtest = sem(r"[<0,0>, "\
                        "((\$y.("\
                        "((\$x.leftonto(leftonto(leftonto(leftonto(leftonto($x)))))) "\
                         "& (\$x.leftonto(leftonto($x)))) leftonto($y))) "\
                        "& (\$x.leftonto(leftonto($x))) <0,0>)]")
        semtest.beta_reduce()
        self.assertTrue(sem_0_14.alpha_equivalent(semtest))