def _dialogues_in_turns(corpus, turn1, turn2): """ Given a pair of turns """ # grab a document from the set (assumption here is that # they are all morally the same doc) if not corpus.values(): sys.exit("No documents selected") doc = corpus.values()[0] starting_turn = get_turn(turn1, doc) ending_turn = get_turn(turn2, doc) # there's a bit of fuzz for whitespace before/after the # turns span = Span(starting_turn.text_span().char_start - 1, ending_turn.text_span().char_end + 1) def is_in_range(anno): """ If the annotation is a dialogue that is covered by the turns in question """ return is_dialogue(anno) and span.encloses(anno.span) return [anno_id_to_tuple(x.local_id()) for x in doc.annotations() if is_in_range(x)]
def _dialogues_in_turns(corpus, turn1, turn2): """ Given a pair of turns """ # grab a document from the set (assumption here is that # they are all morally the same doc) if not corpus.values(): sys.exit("No documents selected") doc = corpus.values()[0] starting_turn = get_turn(turn1, doc) ending_turn = get_turn(turn2, doc) # there's a bit of fuzz for whitespace before/after the # turns span = Span(starting_turn.text_span().char_start - 1, ending_turn.text_span().char_end + 1) def is_in_range(anno): """ If the annotation is a dialogue that is covered by the turns in question """ return is_dialogue(anno) and span.encloses(anno.span) return [ anno_id_to_tuple(x.local_id()) for x in doc.annotations() if is_in_range(x) ]
def _rename_in_doc(source, target, doc): """ Rename all annotations with the given source id in the given document NB: modifies doc """ matches = [x for x in doc.annotations() if anno_id_to_tuple(x.local_id()) == source] pretty_source = anno_id_from_tuple(source) pretty_target = anno_id_from_tuple(target) target_author, target_date = target def replace_pointer(pointers): "Given annotation id, return copy with s/src/tgt/" return [pretty_target if ptr == pretty_source else ptr for ptr in pointers] if not matches: sys.exit("No annotations found with id %s" % pretty_source) elif len(matches) > 1: sys.exit("Huh?! More than one annotation with id %s" % pretty_source) evil_set_id(matches[0], target_author, target_date) for anno in doc.relations: if anno.span.t1 == pretty_source: anno.span.t1 = pretty_target if anno.span.t2 == pretty_source: anno.span.t2 = pretty_target for anno in doc.schemas: anno.units = replace_pointer(anno.units) anno.relations = replace_pointer(anno.relations) anno.schemas = replace_pointer(anno.schemas)
def _delete_in_doc(del_id, doc): """Delete the annotations with the given id in the given document NB: modifies doc """ pretty_id = anno_id_from_tuple(del_id) is_ok = lambda x: anno_id_to_tuple(x.local_id()) != del_id matches = [x for x in doc.annotations() if not is_ok(x)] if not matches: print("Skipping... no annotations found with id %s" % pretty_id, file=sys.stderr) return elif len(matches) > 1: sys.exit("Huh?! More than one annotation with id %s" % pretty_id) doc.units = [x for x in doc.units if is_ok(x)] doc.relations = [x for x in doc.relations if is_ok(x)] doc.schemas = [x for x in doc.schemas if is_ok(x)] def oops(reason): "quit because of illegal delete" sys.exit("Can't delete %s because %s " % pretty_id, reason) for anno in doc.relations: if anno.span.t1 == pretty_id: oops("it is the source for a relation: %s" % anno) if anno.span.t2 == pretty_id: oops("it is the target for a relation: %s" % anno) for anno in doc.schemas: if pretty_id in anno.units: oops("it is a unit member of %s" % anno) if pretty_id in anno.relations: oops("it is a relation member of %s" % anno) if pretty_id in anno.schemas: oops("it is a schema member of %s" % anno)
def _has_named_annotation(target, doc): """ Return True if the given document has the target annotation """ return any(anno_id_to_tuple(x.local_id()) == target for x in doc.annotations())