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
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): 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(gold_graph, graph, label_compare=_label_compare) # Get the common dependency graph common_graph, node_map1, node_map2 = alignment_to_graph(alignment, gold_graph, graph, label_compare=_label_compare) graphs.append((common_graph, node_map1, node_map2, alignment,transx,transy)) # Score on the basis of the shared dependencies alignment_score,common_graph,node_map1,node_map2,alignment,transx,transy = \ max([(len(g),g,nm1,nm2,al,tx,ty) for (g,nm1,nm2,al,tx,ty) in graphs], key=lambda x:x[0]) transpose = (transx,transy) # Produce the list of node alignments we used # Reverse the mapping from the graphs to the common graph node_rmap1 = dict([(common,node) for (node,common) in node_map1.items()]) node_rmap2 = dict([(common,node) for (node,common) in node_map2.items()])
# 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( gold_graph, graph, label_compare=_label_compare) # Get the common dependency graph common_graph, node_map1, node_map2 = alignment_to_graph( alignment, gold_graph, graph, label_compare=_label_compare) graphs.append((common_graph, node_map1, node_map2, alignment, transx, transy)) # Score on the basis of the shared dependencies alignment_score,common_graph,node_map1,node_map2,alignment,transx,transy = \ max([(len(g),g,nm1,nm2,al,tx,ty) for (g,nm1,nm2,al,tx,ty) in graphs], key=lambda x:x[0]) transpose = (transx, transy)