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
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
def get_depend_graph(semantics): # 'coord', 'xycoord', 'alpha' or 'roman' grammar = get_grammar() grammar.formalism.cl_output_options("tsformat=coord") coords = zip(*grammar.formalism.semantics_to_coordinates(semantics))[0] funs = zip(*grammar.formalism.semantics_to_functions(semantics))[0] gold_seq = zip(coords, funs) tags = [] for g in gold_seq: t = "%s,%s" % (coordinate_to_roman_name(g[0]).replace("-","").replace("b", ""), g[1]) tags.append(t) gold_graph,gold_time_map = semantics_to_dependency_graph(semantics) depend_graph_tags = eval("%s" % gold_graph.get_graph_pos(tags)) gold_graph = eval("%s" % gold_graph.get_graph_index()) return [gold_graph, depend_graph_tags]
def run(self, args, state): from jazzparser.formalisms.music_halfspan.harmstruct import \ semantics_to_dependency_graph if self.options['res']: resnum = int(args[0]) res = state.results[resnum] song = res.semantics print "Dependency graph for result %d\n" % resnum else: songnum = int(args[0]) name, song = get_song(songnum, state) print "Dependency graph for '%s'\n" % name print "Semantics:" print song print graph, times = semantics_to_dependency_graph(song) print graph
print >>sys.stderr, "Error loading file: %s" % (err) sys.exit(1) gold_graph = graph = common_graph = node_alignments = None transpose = (0,0) # This may be None, if a name is unavailable name = pres.get_name() # Try to get a gold standard result gold_result = pres.get_gold_semantics() if gold_result is None: # Can't print this print >>sys.stderr, "No gold result" else: gold_graph,gold_time_map = semantics_to_dependency_graph(gold_result) if not options.latex: print "Gold dependency graph:" print gold_graph if options.times: for (node,time) in sorted(gold_time_map.items(), key=lambda x:x[1]): print "%d @ %s" % (node, time) print # Get the top result's semantics if len(pres.semantics) == 0: print >>sys.stderr, "No results" else: top_result = pres.semantics[0][1]
def main(): usage = "%prog [options] <results-files> <index>" description = "Prints a dependency tree for a parse result" parser = OptionParser(usage=usage, description=description) parser.add_option("-l", "--latex", dest="latex", action="store_true", help="output Latex for the graphs using tikz-dependency") parser.add_option("--file-options", "--fopt", dest="file_options", action="store", help="options for the input file (--file). Type '--fopt help' for a list of available options.") options, arguments = parser.parse_args() if len(arguments) < 1: print >>sys.stderr, "Specify a file to read the results from" sys.exit(1) filename = arguments[0] if len(arguments) < 2: print >>sys.stderr, "Specify an of the sequence to load" sys.exit(1) index = int(arguments[1]) grammar = get_grammar() # We always need an index, so this is given as an argument # Put it in the options list for loading the file fopts = options.file_options if fopts and len(fopts): fopts += ":index=%d" % index else: fopts = "index=%d" % index # Load the sequence index file dbinput = command_line_input(filename=filename, filetype="db", options=fopts) name = dbinput.name anal = parse_sequence_with_annotations(dbinput, grammar)[0] graph, time_map = semantics_to_dependency_graph(anal.semantics) # Join together chords that are on the same dependency node times = iter(sorted(time_map.values())) dep_time = times.next() current_chord = [] joined_chords = [] finished = False for chord_time,chord in sorted(dbinput.sequence.time_map.items()): if chord_time >= dep_time and not finished: if len(current_chord): joined_chords.append(current_chord) current_chord = [chord] try: dep_time = times.next() except StopIteration: finished = True else: current_chord.append(chord) joined_chords.append(current_chord) chords = [" ".join(filter_latex(str(crd)) for crd in item) for item in joined_chords] annotations = [" ".join(filter_latex(crd.category) for crd in item) for item in joined_chords] graph.words = annotations if options.latex: # Exit with status 1 if we don't output anything exit_status = 1 # Output a full Latex document in one go if name is not None: title = r"""\title{%s} \author{} \date{}""" % name.capitalize() maketitle = r"\maketitle\thispagestyle{empty}\vspace{-20pt}" else: title = "" maketitle = "" # Print the header print r"""\documentclass[a4paper]{article} \usepackage{tikz-dependency} %% You may need to set paperheight (for width) and paperwidth (for height) to get things to fit \usepackage[landscape,margin=1cm,paperheight=50cm]{geometry} \pagestyle{empty} %(title)s \begin{document} %(maketitle)s \tikzstyle{every picture}+=[remember picture] \centering """ % \ { 'title' : title, 'maketitle' : maketitle } if graph is not None: exit_status = 0 print dependency_graph_to_latex(graph, fmt_lab=_fmt_label, extra_rows=[chords]) print "\n\\vspace{15pt}" # Finish off the document print r""" \end{document} """ sys.exit(exit_status) else: # Not outputing Latex print graph
def main(): features = {} input_files = glob.glob(PARSES_FILES) for file_results in input_files: # We read in the whole file (it's pickled, so we have to), but don't # keep the pres object after the loop iteration, because it can # be very big try: pres = ParseResults.from_file(file_results) except ParseResults.LoadError, err: if options.errors: # Print all load errors print >>sys.stderr, "Error loading file: %s" % (err) errors.append(file_results) continue print file_results if len(pres.semantics) == 0: continue top_result = pres.semantics[0][1] gold_result = pres.get_gold_semantics() # 'coord', 'xycoord', 'alpha' or 'roman' grammar = get_grammar() grammar.formalism.cl_output_options("tsformat=coord") coords = zip(*grammar.formalism.semantics_to_coordinates(gold_result))[0] funs = zip(*grammar.formalism.semantics_to_functions(gold_result))[0] gold_seq = zip(coords, funs) tags = [] for g in gold_seq: t = "%s,%s" % (coordinate_to_roman_name(g[0]), g[1]) tags.append(t) gold_graph,gold_time_map = semantics_to_dependency_graph(gold_result) depend_graph = eval("%s" % gold_graph.get_graph_pos(tags)) gold_graph = eval("%s" % gold_graph.get_graph_index()) # Words for g in gold_graph: word1 = g[0].split(",") uni_word = "UNIGRAM:"+str(word1[0]) if uni_word not in features: features[uni_word] = 0 else: features[uni_word] += 1 for dep in depend_graph: word1 = dep[0].split(",") uni_word = "UNIGRAM:"+str(word1[0]) if uni_word not in features: features[uni_word] = 0 else: features[uni_word] += 1 # Tags for dep in depend_graph: word1 = dep[0].split(",") uni_tag = "UNIGRAM:"+str(word1[1]) if uni_tag not in features: features[uni_tag] = 0 else: features[uni_tag] += 1 # Bigram Words for g in gold_graph: word1 = g[0].split(",") if g[1] == "ROOT": bigram_word = "BIGRAM:"+str(word1[0])+":ROOT" else: word2 = g[1].split(",") bigram_word = "BIGRAM:"+str(word1[0])+":"+str(word2[0]) if bigram_word not in features: features[bigram_word] = 0 else: features[bigram_word] += 1 for dep in depend_graph: word1 = dep[0].split(",") if dep[1] == "ROOT": bigram_word = "BIGRAM:"+str(word1[0])+":ROOT" else: word2 = dep[1].split(",") bigram_word = "BIGRAM:"+str(word1[0])+":"+str(word2[0]) if bigram_word not in features: features[bigram_word] = 0 else: features[bigram_word] += 1 # Bigram Tags for dep in depend_graph: word1 = dep[0].split(",") if dep[1] == "ROOT": bigram_tag = "BIGRAM:"+str(word1[1])+":ROOT" else: word2 = dep[1].split(",") bigram_tag = "BIGRAM:"+str(word1[1])+":"+str(word2[1]) if bigram_tag not in features: features[bigram_tag] = 0 else: features[bigram_tag] += 1 # Bigram Words/Tags for dep in depend_graph: word1 = dep[0].split(",") if dep[1] == "ROOT": bigram_words_tags = "BIGRAM:"+str(word1[0])+":"+str(word1[1])+":ROOT" else: word2 = dep[1].split(",") bigram_words_tags = "BIGRAM:"+str(word1[0])+":"+str(word1[1])+":"+str(word2[0])+":"+str(word2[1]) if bigram_words_tags not in features: features[bigram_words_tags] = 0 else: features[bigram_words_tags] += 1 # Trigram words for i in range(len(gold_graph)): if gold_graph[i][1] == "ROOT": # Get trigram if gold_graph[i-1][1] != "ROOT" and gold_graph[i-2][1] != "ROOT": head_root_word = gold_graph[i][0].split(",")[0] head_i1_word = gold_graph[i-1][0].split(",")[0] head_i2_word = gold_graph[i-2][0].split(",")[0] trigram_word = "TRIGRAM:" + head_root_word + ":" + head_i1_word + ":" + head_i2_word if trigram_word not in features: features[trigram_word] = 0 else: features[trigram_word] += 1 for i in range(len(depend_graph)): if depend_graph[i][1] == "ROOT": # Get trigram if depend_graph[i-1][1] != "ROOT" and depend_graph[i-2][1] != "ROOT": head_root_word = depend_graph[i][0].split(",")[0] head_i1_word = depend_graph[i-1][0].split(",")[0] head_i2_word = depend_graph[i-2][0].split(",")[0] trigram_word = "TRIGRAM:" + head_root_word + ":" + head_i1_word + ":" + head_i2_word if trigram_word not in features: features[trigram_word] = 0 else: features[trigram_word] += 1 # Trigram tags for i in range(len(depend_graph)): if depend_graph[i][1] == "ROOT": # Get trigram if depend_graph[i-1][1] != "ROOT" and depend_graph[i-2][1] != "ROOT": head_root_tag = depend_graph[i][0].split(",")[1] head_i1_tag = depend_graph[i-1][0].split(",")[1] head_i2_tag = depend_graph[i-2][0].split(",")[1] trigram_tag = "TRIGRAM:" + head_root_tag + ":" + head_i1_tag + ":" + head_i2_tag if trigram_tag not in features: features[trigram_tag] = 0 else: features[trigram_tag] += 1 # Trigram words/tags for i in range(len(depend_graph)): if depend_graph[i][1] == "ROOT": # Get trigram if depend_graph[i-1][1] != "ROOT" and depend_graph[i-2][1] != "ROOT": head_root = depend_graph[i][0].split(",") head_root_word_tag = head_root[0] + ":" + head_root[1] # words/tags head_i1 = depend_graph[i-1][0].split(",") head_i2 = depend_graph[i-2][0].split(",") head_i1_word_tag = head_i1[0] + ":" + head_i1[1] head_i2_word_tag = head_i2[0] + ":" + head_i2[1] trigram_word_tag = "TRIGRAM:" + head_root_word_tag + ":" + head_i1_word_tag + ":" + head_i2_word_tag if trigram_word_tag not in features: features[trigram_word_tag] = 0 else: features[trigram_word_tag] += 1
def main(): usage = "%prog [options] <results-files> <index>" description = "Prints a dependency tree for a parse result" parser = OptionParser(usage=usage, description=description) parser.add_option("-l", "--latex", dest="latex", action="store_true", help="output Latex for the graphs using tikz-dependency") parser.add_option( "--file-options", "--fopt", dest="file_options", action="store", help= "options for the input file (--file). Type '--fopt help' for a list of available options." ) options, arguments = parser.parse_args() if len(arguments) < 1: print >> sys.stderr, "Specify a file to read the results from" sys.exit(1) filename = arguments[0] if len(arguments) < 2: print >> sys.stderr, "Specify an of the sequence to load" sys.exit(1) index = int(arguments[1]) grammar = get_grammar() # We always need an index, so this is given as an argument # Put it in the options list for loading the file fopts = options.file_options if fopts and len(fopts): fopts += ":index=%d" % index else: fopts = "index=%d" % index # Load the sequence index file dbinput = command_line_input(filename=filename, filetype="db", options=fopts) name = dbinput.name anal = parse_sequence_with_annotations(dbinput, grammar)[0] graph, time_map = semantics_to_dependency_graph(anal.semantics) # Join together chords that are on the same dependency node times = iter(sorted(time_map.values())) dep_time = times.next() current_chord = [] joined_chords = [] finished = False for chord_time, chord in sorted(dbinput.sequence.time_map.items()): if chord_time >= dep_time and not finished: if len(current_chord): joined_chords.append(current_chord) current_chord = [chord] try: dep_time = times.next() except StopIteration: finished = True else: current_chord.append(chord) joined_chords.append(current_chord) chords = [ " ".join(filter_latex(str(crd)) for crd in item) for item in joined_chords ] annotations = [ " ".join(filter_latex(crd.category) for crd in item) for item in joined_chords ] graph.words = annotations if options.latex: # Exit with status 1 if we don't output anything exit_status = 1 # Output a full Latex document in one go if name is not None: title = r"""\title{%s} \author{} \date{}""" % name.capitalize() maketitle = r"\maketitle\thispagestyle{empty}\vspace{-20pt}" else: title = "" maketitle = "" # Print the header print r"""\documentclass[a4paper]{article} \usepackage{tikz-dependency} %% You may need to set paperheight (for width) and paperwidth (for height) to get things to fit \usepackage[landscape,margin=1cm,paperheight=50cm]{geometry} \pagestyle{empty} %(title)s \begin{document} %(maketitle)s \tikzstyle{every picture}+=[remember picture] \centering """ % \ { 'title' : title, 'maketitle' : maketitle } if graph is not None: exit_status = 0 print dependency_graph_to_latex(graph, fmt_lab=_fmt_label, extra_rows=[chords]) print "\n\\vspace{15pt}" # Finish off the document print r""" \end{document} """ sys.exit(exit_status) else: # Not outputing Latex print graph
print >> sys.stderr, "Error loading file: %s" % (err) sys.exit(1) gold_graph = graph = common_graph = node_alignments = None transpose = (0, 0) # This may be None, if a name is unavailable name = pres.get_name() # Try to get a gold standard result gold_result = pres.get_gold_semantics() if gold_result is None: # Can't print this print >> sys.stderr, "No gold result" else: gold_graph, gold_time_map = semantics_to_dependency_graph(gold_result) if not options.latex: print "Gold dependency graph:" print gold_graph if options.times: for (node, time) in sorted(gold_time_map.items(), key=lambda x: x[1]): print "%d @ %s" % (node, time) print # Get the top result's semantics if len(pres.semantics) == 0: print >> sys.stderr, "No results" else:
print >>sys.stderr, "Error loading file: %s" % (err) sys.exit(1) gold_graph = graph = common_graph = node_alignments = None transpose = (0,0) # This may be None, if a name is unavailable name = pres.get_name() # Try to get a gold standard result gold_result = pres.get_gold_semantics() if gold_result is None: # Can't print this print >>sys.stderr, "No gold result" else: gold_graph,gold_time_map = semantics_to_dependency_graph(gold_result) if not options.latex: print "Gold dependency graph:" print gold_graph if options.times: for (node,time) in sorted(gold_time_map.items(), key=lambda x:x[1]): print "%d @ %s" % (node, time) print # Get the top result's semantics if len(pres.semantics) == 0: print >>sys.stderr, "No results" else: top_result = pres.semantics[0][1]