Ejemplo n.º 1
0
    def __init__(self, input, options={}, grammar=None, *args, **kwargs):
        super(MidiHmmPathBuilder, self).__init__(input, options, *args, **kwargs)
        if grammar is None:
            self.grammar = get_grammar()
        else:
            self.grammar = grammar

        # Make a copy of the options that we will pass through to HmmPath
        options = self.options.copy()
        # Remove the options that the tagger doesn't need
        labeling_model_name = options.pop("labeling_model")
        latticen = options.pop("latticen")
        beam_ratio = options.pop("lattice_beam")
        viterbi = options.pop("label_viterbi")
        partition_labeler = options.pop("partition_labeler")

        # Use an HP chord labeler to label the MIDI data
        # Partition the labeling model if requested and a partition number
        #  was given for the supertagger
        if partition_labeler and "partition" in self.options and self.options["partition"] is not None:
            labeling_model_name += "%d" % self.options["partition"]

        # First run the chord labeler on the MIDI input
        # Load a labeling model
        labeler = HPChordLabeler.load_model(labeling_model_name)
        self.labeler = labeler
        # Get chord labels from the model: get a lattice of possible chords
        lattice = labeler.label_lattice(input, options={"n": latticen, "nokey": True, "viterbi": viterbi}, corpus=True)
        # Store the lattice for later reference
        self.lattice = lattice
        # Beam the lattice to get rid of very low probability labels
        lattice.apply_ratio_beam(ratio=beam_ratio)

        # Tag the lattice
        self.hmmpath = HmmPathBuilder(lattice, options, grammar, *args, **kwargs)
Ejemplo n.º 2
0
    def __init__(self, grammar, input, options={}, logger=None, *args, **kwargs):
        Tagger.__init__(self, grammar, input, options, logger=logger)
        # Make a copy of the options that we will pass through to the tagger
        options = self.options.copy()
        # Remove the options that the tagger doesn't need
        labeling_model_name = options.pop("labeling_model")
        latticen = options.pop("latticen")
        beam_ratio = options.pop("lattice_beam")
        viterbi = options.pop("label_viterbi")
        partition_labeler = options.pop("partition_labeler")
        label_output = options.pop("label_output")
        only_label = options.pop("only_label")

        # Partition the labeling model if requested and a partition number
        #  was given for the supertagger
        if partition_labeler and "partition" in self.options and self.options["partition"] is not None:
            labeling_model_name += "%d" % self.options["partition"]

        self.logger.info("Labeling model: %s" % labeling_model_name)
        # First run the chord labeler on the MIDI input
        # Load a labeling model
        labeler = HPChordLabeler.load_model(labeling_model_name)
        self.labeler = labeler
        # Get chord labels from the model: get a lattice of possible chords
        lattice = labeler.label_lattice(input, options={"n": latticen, "nokey": True, "viterbi": viterbi}, corpus=True)
        # Store the lattice for later reference
        self.lattice = lattice
        # Also store the labeler's emission matrix
        emissions = midi_to_emission_stream(input, remove_empty=False)[0]
        self.labeler_emission_matrix = labeler.get_small_emission_matrix(emissions)
        # Beam the lattice to get rid of very low probability labels
        lattice.apply_ratio_beam(ratio=beam_ratio)

        if label_output:
            # Output the labelling to a file
            with open(label_output, "a") as outfile:
                for chord_tags in lattice:
                    print >> outfile, " ".join(str(crd) for (crd, prob) in chord_tags)

        if only_label:
            self.tagger = FailTagger(grammar, NullInput())
        else:
            self.tagger = MultiChordNgramTagger(grammar, lattice, options, logger=logger, *args, **kwargs)
            self._lex_prob_cache = [{} for i in range(self.input_length)]
Ejemplo n.º 3
0
    def __init__(self, input, options={}, grammar=None, *args, **kwargs):
        super(MidiHmmPathBuilder, self).__init__(input, options, *args,
                                                 **kwargs)
        if grammar is None:
            self.grammar = get_grammar()
        else:
            self.grammar = grammar

        # Make a copy of the options that we will pass through to HmmPath
        options = self.options.copy()
        # Remove the options that the tagger doesn't need
        labeling_model_name = options.pop('labeling_model')
        latticen = options.pop('latticen')
        beam_ratio = options.pop('lattice_beam')
        viterbi = options.pop('label_viterbi')
        partition_labeler = options.pop('partition_labeler')

        # Use an HP chord labeler to label the MIDI data
        # Partition the labeling model if requested and a partition number
        #  was given for the supertagger
        if partition_labeler and 'partition' in self.options and \
                    self.options['partition'] is not None:
            labeling_model_name += "%d" % self.options['partition']

        # First run the chord labeler on the MIDI input
        # Load a labeling model
        labeler = HPChordLabeler.load_model(labeling_model_name)
        self.labeler = labeler
        # Get chord labels from the model: get a lattice of possible chords
        lattice = labeler.label_lattice(input,
                                        options={
                                            'n': latticen,
                                            'nokey': True,
                                            'viterbi': viterbi
                                        },
                                        corpus=True)
        # Store the lattice for later reference
        self.lattice = lattice
        # Beam the lattice to get rid of very low probability labels
        lattice.apply_ratio_beam(ratio=beam_ratio)

        # Tag the lattice
        self.hmmpath = HmmPathBuilder(lattice, options, grammar, *args,
                                      **kwargs)
Ejemplo n.º 4
0
    def __init__(self,
                 grammar,
                 input,
                 options={},
                 logger=None,
                 *args,
                 **kwargs):
        Tagger.__init__(self, grammar, input, options, logger=logger)
        # Make a copy of the options that we will pass through to the tagger
        options = self.options.copy()
        # Remove the options that the tagger doesn't need
        labeling_model_name = options.pop('labeling_model')
        latticen = options.pop('latticen')
        beam_ratio = options.pop('lattice_beam')
        viterbi = options.pop('label_viterbi')
        partition_labeler = options.pop('partition_labeler')
        label_output = options.pop('label_output')
        only_label = options.pop('only_label')

        # Partition the labeling model if requested and a partition number
        #  was given for the supertagger
        if partition_labeler and 'partition' in self.options and \
                    self.options['partition'] is not None:
            labeling_model_name += "%d" % self.options['partition']

        self.logger.info("Labeling model: %s" % labeling_model_name)
        # First run the chord labeler on the MIDI input
        # Load a labeling model
        labeler = HPChordLabeler.load_model(labeling_model_name)
        self.labeler = labeler
        # Get chord labels from the model: get a lattice of possible chords
        lattice = labeler.label_lattice(input,
                                        options={
                                            'n': latticen,
                                            'nokey': True,
                                            'viterbi': viterbi
                                        },
                                        corpus=True)
        # Store the lattice for later reference
        self.lattice = lattice
        # Also store the labeler's emission matrix
        emissions = midi_to_emission_stream(input, remove_empty=False)[0]
        self.labeler_emission_matrix = labeler.get_small_emission_matrix(
            emissions)
        # Beam the lattice to get rid of very low probability labels
        lattice.apply_ratio_beam(ratio=beam_ratio)

        if label_output:
            # Output the labelling to a file
            with open(label_output, 'a') as outfile:
                for chord_tags in lattice:
                    print >> outfile, " ".join(
                        str(crd) for (crd, prob) in chord_tags)

        if only_label:
            self.tagger = FailTagger(grammar, NullInput())
        else:
            self.tagger = MultiChordNgramTagger(grammar,
                                                lattice,
                                                options,
                                                logger=logger,
                                                *args,
                                                **kwargs)
            self._lex_prob_cache = [{} for i in range(self.input_length)]