def main(): usage = "%prog [options] <seq-file> <index>" description = "Displays a tree for the annotated derivation of a chord "\ "sequence in the gold standard" parser = OptionParser(usage=usage, description=description) options, arguments = parser.parse_args() if len(arguments) < 2: print "You must specify a sequence file and index" sys.exit(1) index = int(arguments[1]) # Get the chord sequence sequence = SequenceIndex.from_file(arguments[0]).sequence_by_index(index) try: # Show the song name print "Tree for '%s'" % sequence.string_name tree = build_tree_for_sequence(sequence) # Output the linear textual form of the tree print tree # Display the tree using NLTK ntree = tree_to_nltk(tree) ntree.draw() except TreeBuildError, err: print >> sys.stderr, "Error parsing: %s" % err sys.exit(1)
def main(): parser = OptionParser() parser.add_option("-s", "--debug-stack", dest="debug_stack", action="store_true", help="print out the stack before each input is processed") parser.add_option("-d", "--draw", dest="draw", action="store_true", help="use NLTK to draw the tree for the sequence") options, arguments = parser.parse_args() if len(arguments) == 0: print >>sys.stderr, "You need to specify a sequence ID to parse" sys.exit(1) try: # Try getting a chord sequence for the given ID try: sequence = ChordSequence.objects.get(id=int(arguments[0])) if sequence.analysis_omitted: print >>sys.stderr, "Ignoring %s, since it's marked as unannotated" % sequence.name.encode('ascii','ignore') return except ChordSequence.DoesNotExist: raise TreeBuildError, "there is no chord sequence with ID %s" % arguments[0] # Build the explicit tree print "Building tree for '%s'" % sequence.string_name # Use the sequence's mirror sequence = sequence.mirror tree = build_tree_for_sequence(sequence, debug_stack=options.debug_stack) # Output the linear textual form of the tree print tree if options.draw: # Display the tree using NLTK ntree = tree_to_nltk(tree) ntree.draw() except TreeBuildError, err: print >>sys.stderr, "Error parsing: %s" % err sys.exit(1)
def main(): usage = "%prog [options] <seq-file> <index>" description = "Displays a tree for the annotated derivation of a chord "\ "sequence in the gold standard" parser = OptionParser(usage=usage, description=description) options, arguments = parser.parse_args() if len(arguments) < 2: print "You must specify a sequence file and index" sys.exit(1) index = int(arguments[1]) # Get the chord sequence sequence = SequenceIndex.from_file(arguments[0]).sequence_by_index(index) try: # Show the song name print "Tree for '%s'" % sequence.string_name tree = build_tree_for_sequence(sequence) # Output the linear textual form of the tree print tree # Display the tree using NLTK ntree = tree_to_nltk(tree) ntree.draw() except TreeBuildError, err: print >>sys.stderr, "Error parsing: %s" % err sys.exit(1)
def _sequence_train(self, sequence): """ Adds counts to the model for a single chord sequence. """ # Prepare the input and annotations input = DbInput.from_sequence(sequence) categories = [chord.category for chord in sequence.iterator()] str_inputs = input.inputs # Build the implicit normal-form tree from the annotations try: tree = build_tree_for_sequence(sequence) except TreeBuildError, err: raise ModelTrainingError, "could not build a tree for '%s': %s" % \ (sequence.string_name, err)
def parse_sequence_with_annotations(sequence, grammar, logger=None, allow_subparses=True, popts={}, parse_logger=None): """ Parses an annotated sequence. This is essentially just constructing the implicit derivation encoded in the annotations (categories and coordination markers) and returning the top-level result that this derivation yields. If there are missing annotations, this will not yield a single result, but the results of each parse of a subsequence. Returns a list of parse results. If the annotation is complete, the return value should be a single-item list containing the parse result. @type allow_subparses: boolean @param allow_subparses: if True (default) will return multiple subparses if the annotation is incomplete. If False, raises a ParseError in this case. @type popts: dict @param popts: options to pass to the parser @type parse_logger: bool @param parse_logger: logger to which to report a trace of the shift-reduce parse of the annotations. If None, nothing is output """ # Prepare the input and annotations if isinstance(sequence, DbInput): input = sequence sequence = sequence.sequence durations = input.durations times = input.times else: raise ValueError, "parse_sequence_with_annotations can only be "\ "applied to a DbInput. Got: %s" % type(sequence).__name__ categories = [chord.category for chord in sequence.iterator()] try: tree = build_tree_for_sequence(sequence, grammar=grammar) except TreeBuildError, err: raise ParseError, "could not build a tree for '%s': %s" % \ (sequence.string_name, err)
def main(): parser = OptionParser() parser.add_option( "-s", "--debug-stack", dest="debug_stack", action="store_true", help="print out the stack before each input is processed") parser.add_option("-d", "--draw", dest="draw", action="store_true", help="use NLTK to draw the tree for the sequence") options, arguments = parser.parse_args() if len(arguments) == 0: print >> sys.stderr, "You need to specify a sequence ID to parse" sys.exit(1) try: # Try getting a chord sequence for the given ID try: sequence = ChordSequence.objects.get(id=int(arguments[0])) if sequence.analysis_omitted: print >> sys.stderr, "Ignoring %s, since it's marked as unannotated" % sequence.name.encode( 'ascii', 'ignore') return except ChordSequence.DoesNotExist: raise TreeBuildError, "there is no chord sequence with ID %s" % arguments[ 0] # Build the explicit tree print "Building tree for '%s'" % sequence.string_name # Use the sequence's mirror sequence = sequence.mirror tree = build_tree_for_sequence(sequence, debug_stack=options.debug_stack) # Output the linear textual form of the tree print tree if options.draw: # Display the tree using NLTK ntree = tree_to_nltk(tree) ntree.draw() except TreeBuildError, err: print >> sys.stderr, "Error parsing: %s" % err sys.exit(1)
def main(): usage = "%prog <model-name>" description = "Debug a PCFG model" parser = OptionParser(usage=usage, description=description) parser.add_option("-g", "--grammar", dest="grammar", action="store", \ help="use the named grammar instead of the default.") parser.add_option("-d", "--debug", dest="debug", action="store_true", \ help="output debugging information during generation") 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 = parse_args_with_config(parser) if len(arguments) < 1: print "Specify a model name" sys.exit(1) model_name = arguments[0] if len(arguments) < 2: print "Specify an input file" grammar = get_grammar(options.grammar) PcfgModel = grammar.formalism.PcfgModel # Load the trained model model = PcfgModel.load_model(model_name) # Try getting a file from the command-line options input_data = command_line_input(filename=arguments[1], filetype="db", options=options.file_options) # Prepare the input and annotations sequence = input_data.sequence categories = [chord.category for chord in sequence.iterator()] str_inputs = input_data.inputs # Build the implicit normal-form tree from the annotations try: tree = build_tree_for_sequence(sequence) except TreeBuildError, err: raise ModelTrainingError, "could not build a tree for '%s': %s" % \ (sequence.string_name, err)