def test_no_types(self): """Test that not specifying allowed_types or excluded_types will modify all targets.""" doc = self.create_target_type_examples() rule = ConTextRule( "no history of travel to", category="DEFINITE_NEGATED_EXISTENCE", direction="FORWARD", ) modifier = ConTextModifier(rule, 0, 5, doc) modifier.set_scope() travel, condition = doc.ents # "puerto rico", "pneumonia" assert modifier.modifies(travel) is True assert modifier.modifies(condition) is True
def test_overlapping_target(self): """Test that a modifier will not modify a target if it is in the same span as the modifier. """ doc = nlp("Pt presents for r/o of pneumonia.") rule = ConTextRule("r/o", "UNCERTAIN", direction="BIDIRECTIONAL") modifier = ConTextModifier(rule, 3, 4, doc) target = Span(doc, 3, 4, "TEST") assert modifier.modifies(target) is False
def test_on_modifies_false(self): def on_modifies(target, modifier, span_between): return False rule = ConTextRule("no evidence of", "NEGATED_EXISTENCE", on_modifies=on_modifies) doc = nlp("There is no evidence of pneumonia or chf.") doc.ents = (doc[5:6], doc[7:8]) modifier = ConTextModifier(rule, 2, 5, doc) assert modifier.modifies(doc.ents[0]) is False
def test_on_modifies_true(self): def on_modifies(target, modifier, span_between): return True rule = ConTextRule("no evidence of", "NEGATED_EXISTENCE", on_modifies=on_modifies) doc = nlp("There is no evidence of pneumonia or chf.") doc.ents = (Span(doc, 5, 6, "CONDITION"), Span(doc, 6, 8, "CONDITION")) mod = ConTextModifier(rule, 2, 5, doc) assert mod.modifies(doc.ents[0]) is True
def test_on_modifies_arg_types(self): def check_arg_types(target, modifier, span_between): for arg in (target, modifier, span_between): if not isinstance(arg, spacy.tokens.Span): return False return True rule = ConTextRule("no evidence of", "NEGATED_EXISTENCE", on_modifies=check_arg_types) doc = nlp("There is no evidence of pneumonia or chf.") doc.ents = (doc[5:6], doc[7:8]) modifier = ConTextModifier(rule, 2, 5, doc) assert modifier.modifies(doc.ents[0]) is True
def test_max_scope_none(self): """Test that if max_scope is not None it will reduce the range of text which is modified. """ doc = self.create_num_target_examples() assert len(doc.ents) == 3 rule = ConTextRule("vs", category="UNCERTAIN", direction="BIDIRECTIONAL", max_scope=None) modifier = ConTextModifier(rule, 5, 6, doc) for target in doc.ents: if modifier.modifies(target): modifier.modify(target) assert modifier.num_targets == 3
def test_on_modifies_arg_values(self): def check_arg_types(target, modifier, span_between): if target.lower_ != "chf": return False if modifier.lower_ != "no evidence of": return False if span_between.lower_ != "pneumonia or": return False return True rule = ConTextRule("no evidence of", "NEGATED_EXISTENCE", on_modifies=check_arg_types) doc = nlp("There is no evidence of pneumonia or chf.") doc.ents = (doc[5:6], doc[7:8]) modifier = ConTextModifier(rule, 2, 5, doc) assert modifier.modifies(doc.ents[1]) is True