Esempio n. 1
0
    def run(self, args, state):
        from jazzparser.formalisms.music_halfspan.evaluation import \
                        tonal_space_local_alignment, tonal_space_distance
        songnum = int(args[0])

        name, song = get_song(songnum, state)
        songset = state.get_data("songset")
        distances = []
        # Try comparing this song to each song in the set
        for other_name, other_song in songset.analyses:
            # Align locally and globally
            ops,steps1,steps2,local_distance = \
                    tonal_space_local_alignment(other_song.lf, song.lf)
            global_distance = \
                    tonal_space_distance(other_song.lf, song.lf)
            distances.append((other_name, local_distance, global_distance))

        # Sort the results
        if self.options['local']:
            distances.sort(key=lambda x: x[1])
        else:
            distances.sort(key=lambda x: x[2])
        # Print out each one
        print "Aligned %s with:" % name
        for other_name, local_distance, global_distance in distances:
            print "%s:  local: %s,  global: %s" % \
                (other_name,local_distance,global_distance)
Esempio n. 2
0
    def run(self, args, state):
        from jazzparser.formalisms.music_halfspan.evaluation import \
                            tonal_space_local_alignment, tonal_space_alignment, \
                            arrange_alignment

        if len(args) < 2:
            raise ShellError, "Give a result number and a song number"

        resnum = int(args[0])
        songnum = int(args[1])

        song = get_song(songnum, state)
        songsem = song[1]

        if self.options['song']:
            # Compare a song instead of a result
            compsong = get_song(resnum, state)
            resultsem = compsong[1]
            print "Comparing '%s' to '%s'" % (compsong[0], song[0])
        else:
            # Normal behaviour: compare a result to a song
            if resnum >= len(state.results):
                raise ShellError, "No result number %d" % resnum
            result = state.results[resnum]
            resultsem = result.semantics
            print "Comparing result %d to '%s'" % (resnum, song[0])

        # Do the comparison
        if self.options['local']:
            ops, song_steps, result_steps, distance = \
                    tonal_space_local_alignment(songsem.lf, resultsem.lf)
        else:
            ops, song_steps, result_steps, distance = \
                    tonal_space_alignment(songsem.lf, resultsem.lf, distance=True)
        print "Steps in '%s':" % song[0]
        print song_steps
        if self.options['song']:
            print "Steps in '%s'" % compsong[0]
        else:
            print "Steps in result path:"
        print result_steps
        print "Alignment operations:"
        print ops

        if self.options['alignment']:
            print "Full alignment:"
            # Print the alignment in three rows
            WRAP_TO = 70
            wrapped_rows = []
            current_row = []
            current_width = 0
            # Wrap the rows
            for cells in arrange_alignment(song_steps, result_steps, ops):
                if len(cells[0]) + current_width > WRAP_TO:
                    # Start a new row
                    wrapped_rows.append(current_row)
                    current_row = []
                    current_width = 0
                current_row.append(cells)
                current_width += len(cells[0])
            # Add the incomplete last row
            wrapped_rows.append(current_row)
            for row in wrapped_rows:
                lefts, rights, opses = zip(*row)
                print " ".join(lefts)
                print " ".join(rights)
                print " ".join(opses)
                print
        print "Distance: %s" % distance