Exemplo n.º 1
0
    def addChild(self, annotation):
        if (annotation.depth == self.getDepth() + 1):
            if annotation.measure != self.getMeasure() and self.getDepth(
            ) > 0 and len(self.children) == 0:
                self.children.append(
                    Node(
                        Annotation(
                            str(self.getDepth() + 1) + ": INSERTED LABEL",
                            self.getMeasure(), self.getBeat())))
            self.children.append(Node(annotation))
            return

        if (len(self.children) > 0):
            self.children[-1].addChild(annotation)
            return

        if (annotation.depth - self.getDepth() == 1):
            self.children = [Node(annotation)]
            return

        self.children = [
            Node(
                Annotation(
                    str(self.getDepth() + 1) + ": INSERTED LABEL",
                    self.getMeasure(), self.getBeat()))
        ]
        self.addChild(annotation)
Exemplo n.º 2
0
 def test_IsAnnotation_TrueNegative(self):
     self.assertFalse(Annotation.IsAnnotation(
         "Allegro"))
     self.assertFalse(Annotation.IsAnnotation(
         "Ludwig van Beethoven"))
     self.assertFalse(Annotation.IsAnnotation(
         "1 A Test"))
     self.assertFalse(Annotation.IsAnnotation(
         "A: A Test"))
Exemplo n.º 3
0
 def test_depth(self):
     self.assertEqual(
         1,
         Annotation("1: Exposition", 1, 4).depth
     )
     self.assertEqual(
         2,
         Annotation("2: First Subject Group", 1, 4).depth
     )
     self.assertEqual(
         3,
         Annotation("3: Theme a", 1, 4).depth
     )
     self.assertEqual(
         4,
         Annotation("4: Sentence", 1, 4).depth
     )
     self.assertEqual(
         5,
         Annotation("5: Presentation", 1, 4).depth
     )
     self.assertEqual(
         6,
         Annotation("6: Basic idea", 1, 4).depth
     )
     self.assertEqual(
         5,
         Annotation("5: Continuation", 6, 1).depth
     )
     self.assertEqual(
         6,
         Annotation("6: Fragmentation", 6, 1).depth
     )
Exemplo n.º 4
0
 def getAnnotations(self) -> [Annotation]:
     self.__text = self.__text.replace(", ", ",")
     annotations = [
         Annotation(text, self.__measure, self.__beat)
         for text in self.__text.split(",")
     ]
     return annotations
Exemplo n.º 5
0
    def parse(self):
        """
        :raises: # TODO better exception when no input file loaded
        :raises: # TODO exception for syntax error in annotation
        """
        if self.__inputFile is None:
            raise Exception("No input file loaded.")

        score = converter.parse(self.__inputFile)

        finalMeasure = 0
        finalBeat = 0
        annotationGroups = []

        finalMeasure = score.recurse().getElementsByClass(
            'Measure')[-1].measureNumber

        testExpressions = [x for x in score.recurse().getElementsByClass('TextExpression') \
                            if Annotation.IsAnnotation(x.content)]

        for element in testExpressions:
            try:
                annotationGroups.append(
                    AnnotationGroup(element.content, element.measureNumber,
                                    element.beat))
            except Exception as e:
                raise ParseError(element.content, finalMeasure)

        self.__annotations = [
            annotation for group in annotationGroups
            for annotation in group.getAnnotations()
        ]
        self.__finalMeasure = finalMeasure
        self.__finalBeat = finalBeat
Exemplo n.º 6
0
    def test_parse_Beethoven_SonateNo1(self):
        parser = MusicXmlParser()

        thisFilePath = os.path.dirname(os.path.abspath(__file__))
        parser.load(thisFilePath + "/../examples/op2no1movt1.musicxml")
        parser.parse()

        annotations = parser.getAnnotations()[:9]

        self.assertEqual([
            Annotation("1: Exposition", 1, 4),
            Annotation("2: First Subject Group", 1, 4),
            Annotation("3: Theme a", 1, 4),
            Annotation("4: Sentence", 1, 4),
            Annotation("5: Presentation", 1, 4),
            Annotation("6: Basic idea", 1, 4),
            Annotation("6: Basic idea", 4, 1),
            Annotation("5: Continuation", 6, 1),
            Annotation("6: Fragmentation", 6, 1),
        ], annotations)
Exemplo n.º 7
0
 def test_IsAnnotation_TruePositive(self):
     self.assertTrue(Annotation.IsAnnotation(
         "1: Exposition, 2: First Subject Group, 3: Theme a, 4: Sentence, 5: Presentation, 6: Basic idea"))
     self.assertTrue(Annotation.IsAnnotation(
         "6: Basic idea"))
     self.assertTrue(Annotation.IsAnnotation(
         "5: Continuation, 6: Fragmentation"))
     self.assertTrue(Annotation.IsAnnotation(
         "6: Cadence"))
     self.assertTrue(Annotation.IsAnnotation(
         "2: Transition, 3: Theme a"))
     self.assertTrue(Annotation.IsAnnotation(
         "4: X"))
     self.assertTrue(Annotation.IsAnnotation(
         "401: X, 502: X"))
     self.assertTrue(Annotation.IsAnnotation(
         "20: Second Subject Group, 3: Theme b, 4: Sentence, 5: Presentation, 6: Basic idea"))
Exemplo n.º 8
0
 def test_getAnnotations_ManyAnnotations_CorrectString_NoSpaceAfterComma(self):
     grp = AnnotationGroup("1: Exposition,2: First Subject Group,3: Theme a,4: Sentence,5: Presentation,6: Basic idea", 1, 1)
     self.assertEqual(
         [
             Annotation("1: Exposition", 1, 1),
             Annotation("2: First Subject Group", 1, 1),
             Annotation("3: Theme a", 1, 1),
             Annotation("4: Sentence", 1, 1),
             Annotation("5: Presentation", 1, 1),
             Annotation("6: Basic idea", 1, 1),
         ],
         grp.getAnnotations()
     )
Exemplo n.º 9
0
 def test_getAnnotations_SingleAnnotation_CorrectString(self):
     grp = AnnotationGroup("6: Basic idea", 1, 1)
     self.assertEqual(
         [Annotation("6: Basic idea", 1, 1)],
         grp.getAnnotations()
     )
Exemplo n.º 10
0
 def test_id(self):
     self.assertEqual(
         "Exposition",
         Annotation("1:Exposition", 1, 4).id
     )
     self.assertEqual(
         "Exposition",
         Annotation("1: Exposition", 1, 4).id
     )
     self.assertEqual(
         "First Subject Group",
         Annotation("2:First Subject Group", 1, 4).id
     )
     self.assertEqual(
         "First Subject Group",
         Annotation("2: First Subject Group", 1, 4).id
     )
     self.assertEqual(
         "Theme a",
         Annotation("3:Theme a", 1, 4).id
     )
     self.assertEqual(
         "Theme a",
         Annotation("3: Theme a", 1, 4).id
     )
     self.assertEqual(
         "Sentence",
         Annotation("4:Sentence", 1, 4).id
     )
     self.assertEqual(
         "Sentence",
         Annotation("4: Sentence", 1, 4).id
     )
     self.assertEqual(
         "Presentation",
         Annotation("5:Presentation", 1, 4).id
     )
     self.assertEqual(
         "Presentation",
         Annotation("5: Presentation", 1, 4).id
     )
     self.assertEqual(
         "Basic idea",
         Annotation("6:Basic idea", 1, 4).id
     )
     self.assertEqual(
         "Basic idea",
         Annotation("6: Basic idea", 1, 4).id
     )
     self.assertEqual(
         "Continuation",
         Annotation("5:Continuation", 6, 1).id
     )
     self.assertEqual(
         "Continuation",
         Annotation("5: Continuation", 6, 1).id
     )
     self.assertEqual(
         "Fragmentation",
         Annotation("6:Fragmentation", 6, 1).id
     )
     self.assertEqual(
         "Fragmentation",
         Annotation("6: Fragmentation", 6, 1).id
     )
Exemplo n.º 11
0
    def test_renderToFile(self):
        annotations = [
            Annotation("1: Exposition", 1, 4),
            Annotation("2: First Subject Group", 1, 4),
            Annotation("3: Theme a", 1, 4),
            Annotation("4: Sentence", 1, 4),
            Annotation("5: Presentation", 1, 4),
            Annotation("6: Basic idea", 1, 4),
            Annotation("6: Basic idea", 4, 1),
            Annotation("5: Continuation", 6, 1),
            Annotation("6: Fragmentation", 6, 1),
            Annotation("6: Cadence", 8, 1),
            Annotation("2: Transition", 10, 1),
            Annotation("3: Theme a", 10, 1),
        ]

        tree = Tree()

        tree.build(annotations, 19)

        renderer = TableRenderer()
        thisFilePath = os.path.dirname(os.path.abspath(__file__))
        renderer.renderToFile(tree, thisFilePath + "/test.csv")


        self.fail()
Exemplo n.º 12
0
    def test_build(self):
        annotations = [
            Annotation("1: Exposition", 1, 4),
            Annotation("2: First Subject Group", 1, 4),
            Annotation("3: Theme a", 1, 4),
            Annotation("4: Sentence", 1, 4),
            Annotation("5: Presentation", 1, 4),
            Annotation("6: Basic idea", 1, 4),
            Annotation("6: Basic idea", 4, 1),
            Annotation("5: Continuation", 6, 1),
            Annotation("6: Fragmentation", 6, 1),
            Annotation("6: Cadence", 8, 1),
            Annotation("2: Transition", 10, 1),
            Annotation("3: Theme a", 10, 1),
        ]

        tree = Tree()

        # parameter totalMeasure is arbitrary -- use to test leaf distances
        tree.build(annotations, 19)

        tree.print()