Exemplo n.º 1
0
 def fscore_match(self, sem1, sem2):
     from jazzparser.formalisms.music_halfspan.harmstruct import \
                                             semantics_to_dependency_graph
     from jazzparser.data.dependencies import optimal_node_alignment, \
                             alignment_to_graph
     from jazzparser.formalisms.music_halfspan.semantics import \
                             EnharmonicCoordinate
     
     if sem1 is None:
         max_score1 = 0.0
     else:
         graph1,timings1 = semantics_to_dependency_graph(sem1)
         max_score1 = float(len(graph1))
     
     if sem2 is None:
         max_score2 = 0.0
     else:
         graph2,timings2 = semantics_to_dependency_graph(sem2)
         max_score2 = float(len(graph2))
     
     if sem1 is None or sem2 is None:
         # Empty input: give zero score to everything
         alignment_score = 0.0
         alignment = []
         transpose = None
     else:
         graph1,timings1 = semantics_to_dependency_graph(sem1)
         graph2,timings2 = semantics_to_dependency_graph(sem2)
         
         node_pairs = []
         # Always align the root nodes to each other
         node_pairs.append((min(graph1.nodes), min(graph2.nodes)))
         
         # Align nodes that occur at the same time
         time_nodes1 = dict([(time,node) for (node,time) in timings1.items()])
         for node2,time in sorted(timings2.items(), key=lambda x:x[1]):
             if time in time_nodes1:
                 node_pairs.append((time_nodes1[time], node2))
 
         def _label_compare(label1, label2):
             if isinstance(label1, EnharmonicCoordinate) and \
                     isinstance(label2, EnharmonicCoordinate):
                 return label1.zero_coord == label2.zero_coord
             else:
                 return label1 == label2
         # Get the graph of shared dependencies that results from aligning 
         #  simultaneous nodes
         graph,node_map1,node_map2 = alignment_to_graph(node_pairs, 
                 graph1, graph2, label_compare=_label_compare)
         
         # Score on the basis of the shared dependencies
         alignment_score = len(graph)
     
     return alignment_score,max_score1,max_score2
Exemplo n.º 2
0
 def fscore_match(self, sem1, sem2):
     from jazzparser.formalisms.music_halfspan.harmstruct import \
                                             semantics_to_dependency_graph
     from jazzparser.data.dependencies import optimal_node_alignment, \
                             alignment_to_graph
     from jazzparser.formalisms.music_halfspan.semantics import \
                             EnharmonicCoordinate
     
     if sem1 is None:
         max_score1 = 0.0
     else:
         graph1,timings1 = semantics_to_dependency_graph(sem1)
         max_score1 = float(len(graph1))
     
     if sem2 is None:
         max_score2 = 0.0
     else:
         graph2,timings2 = semantics_to_dependency_graph(sem2)
         max_score2 = float(len(graph2))
     
     if sem1 is None or sem2 is None:
         # Empty input: give zero score to everything
         alignment_score = 0.0
         alignment = []
         transpose = None
     else:
         graph1,timings1 = semantics_to_dependency_graph(sem1)
         graph2,timings2 = semantics_to_dependency_graph(sem2)
         
         graphs = []
         # Try all possible transpositions and assume the best
         for transx in range(4):
             for transy in range(3):
                 def _label_compare(label1, label2):
                     if isinstance(label1, EnharmonicCoordinate) and \
                             isinstance(label2, EnharmonicCoordinate):
                         coord1 = label1.zero_coord
                         x2,y2 = label2.zero_coord
                         return coord1 == ((x2+transx)%4, (y2+transy)%3)
                     else:
                         return label1 == label2
                 
                 # Find the alignment of the nodes that matches most dependencies
                 alignment = optimal_node_alignment(graph1, graph2, label_compare=_label_compare)
                 # Get the common dependency graph
                 graph, node_map1, node_map2 = alignment_to_graph(alignment, 
                             graph1, graph2, label_compare=_label_compare)
                 
                 graphs.append(graph)
         
         # Score on the basis of the shared dependencies
         alignment_score,graph = max([(len(graph),graph) for graph in graphs], key=lambda x:x[0])
     
     return alignment_score,max_score1,max_score2
Exemplo n.º 3
0
     
     if not options.latex:
         print "Nodes aligned by time (gold -- result):"
         for i,(node1,node2) in enumerate(node_pairs):
             print "%d: %d -- %d" % (i, node1,node2)
 
         def _label_compare(label1, label2):
             if isinstance(label1, EnharmonicCoordinate) and \
                     isinstance(label2, EnharmonicCoordinate):
                 return label1.zero_coord == label2.zero_coord
             else:
                 return label1 == label2
     
     # Get the graph of shared dependencies that results from aligning 
     #  simultaneous nodes
     common_graph,node_map1,node_map2 = alignment_to_graph(node_pairs, 
             gold_graph, graph, label_compare=_label_compare)
     if not options.latex:
         print "Common dependencies:"
         print common_graph
         print "Graph size: %d" % len(common_graph)
         print
 
 
 if options.align_max:
     graphs = []
     # Try all possible transpositions and assume the best
     for transx in range(4):
         for transy in range(3):
             def _label_compare(label1, label2):
                 if isinstance(label1, EnharmonicCoordinate) and \
                         isinstance(label2, EnharmonicCoordinate):
Exemplo n.º 4
0
            if not options.latex:
                print "Nodes aligned by time (gold -- result):"
                for i, (node1, node2) in enumerate(node_pairs):
                    print "%d: %d -- %d" % (i, node1, node2)

                def _label_compare(label1, label2):
                    if isinstance(label1, EnharmonicCoordinate) and \
                            isinstance(label2, EnharmonicCoordinate):
                        return label1.zero_coord == label2.zero_coord
                    else:
                        return label1 == label2

            # Get the graph of shared dependencies that results from aligning
            #  simultaneous nodes
            common_graph, node_map1, node_map2 = alignment_to_graph(
                node_pairs, gold_graph, graph, label_compare=_label_compare)
            if not options.latex:
                print "Common dependencies:"
                print common_graph
                print "Graph size: %d" % len(common_graph)
                print

        if options.align_max:
            graphs = []
            # Try all possible transpositions and assume the best
            for transx in range(4):
                for transy in range(3):

                    def _label_compare(label1, label2):
                        if isinstance(label1, EnharmonicCoordinate) and \
                                isinstance(label2, EnharmonicCoordinate):