예제 #1
0
    def test_on_modifies_false(self):
        def on_modifies(target, modifier, span_between):
            return False

        context = ConTextComponent(nlp, rules=None)
        item = ConTextItem("no evidence of",
                           "NEGATED_EXISTENCE",
                           on_modifies=on_modifies)
        context.add([item])
        doc = nlp("There is no evidence of pneumonia or chf.")
        doc.ents = (doc[5:6], doc[7:8])
        context(doc)

        for ent in doc.ents:
            assert len(ent._.modifiers) == 0
예제 #2
0
    def test_not_remove_modifiers_overlap_target(self):
        """Test that a modifier which overlaps with a target is not pruned but does not modify itself."""
        doc = nlp("The patient has heart failure.")
        doc.ents = (Span(doc, 3, 5, "CONDITION"), )
        context_item = ConTextItem("failure", "MODIFIER")
        tag_object = TagObject(context_item, 4, 5, doc)
        graph = ConTextGraph(remove_overlapping_modifiers=False)

        graph.modifiers = [tag_object]
        graph.targets = doc.ents
        graph.prune_modifiers()
        graph.apply_modifiers()

        assert overlap_target_modifiers(tag_object.span, doc.ents[0])
        assert len(graph.modifiers) == 1
예제 #3
0
    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

        item = ConTextItem("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])
        tag = TagObject(item, 2, 5, doc)

        assert tag.modifies(doc.ents[0]) is True
예제 #4
0
    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
        item = ConTextItem("vs",
                           category="UNCERTAIN",
                           rule="BIDIRECTIONAL",
                           max_scope=None)
        tag_object = TagObject(item, 5, 6, doc)

        for target in doc.ents:
            if tag_object.modifies(target):
                tag_object.modify(target)
        assert tag_object.num_targets == 3
예제 #5
0
    def test_custom_attributes_value1(self):
        custom_attrs = {
            "NEGATED_EXISTENCE": {
                "is_negated": True
            },
        }
        try:
            Span.set_extension("is_negated", default=False)
        except:
            pass
        context = ConTextComponent(nlp, add_attrs=custom_attrs)
        context.add(
            [ConTextItem("no evidence of", "NEGATED_EXISTENCE", "FORWARD")])
        doc = nlp("There is no evidence of pneumonia.")
        doc.ents = (doc[-2:-1], )
        context(doc)

        assert doc.ents[0]._.is_negated is True
예제 #6
0
    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

        item = ConTextItem("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])
        tag = TagObject(item, 2, 5, doc)

        assert tag.modifies(doc.ents[1]) is True
예제 #7
0
    def test_max_targets_none(self):
        """Check that if max_targets is None it will not reduce the targets
        to the two closest ents.
        """
        doc = self.create_num_target_examples()
        assert len(doc.ents) == 3
        item = ConTextItem("vs",
                           category="UNCERTAIN",
                           rule="BIDIRECTIONAL",
                           max_targets=None)
        # Set "vs" to be the modifier
        tag_object = TagObject(item, 5, 6, doc)
        for target in doc.ents:
            tag_object.modify(target)
        assert tag_object.num_targets == 3

        tag_object.reduce_targets()
        assert tag_object.num_targets == 3
예제 #8
0
    def test_max_targets_less_than_targets(self):
        """Check that if max_targets is not None it will reduce the targets
        to the two closest ents.
        """
        doc = self.create_num_target_examples()
        assert len(doc.ents) == 3
        item = ConTextItem("vs",
                           category="UNCERTAIN",
                           rule="BIDIRECTIONAL",
                           max_targets=2)
        # Set "vs" to be the modifier
        tag_object = TagObject(item, 5, 6, doc)
        for target in doc.ents:
            tag_object.modify(target)
        assert tag_object.num_targets == 3

        tag_object.reduce_targets()
        assert tag_object.num_targets == 2
        for target in tag_object._targets:
            assert target.lower_ in ("pneumonia", "copd")
예제 #9
0
    def test_simple_callback(self, capsys):
        context = ConTextComponent(nlp, rules=None)

        def simple_callback(matcher, doc, i, matches):
            match_id, start, end = matches[i]
            span = doc[start:end]
            print("Matched on span:", span)

        context.add([
            ConTextItem(
                "no evidence of",
                "NEGATED_EXISTENCE",
                "FORWARD",
                on_match=simple_callback,
            )
        ])

        doc = nlp("There is no evidence of pneumonia.")
        context(doc)
        captured = capsys.readouterr()
        assert captured.out == "Matched on span: no evidence of\n"
예제 #10
0
 def test_from_json(self, from_json_file):
     assert ConTextItem.from_json(from_json_file)
예제 #11
0
 def test_to_dict(self):
     literal = "no evidence of"
     category = "definite_negated_existence"
     rule = "forward"
     item = ConTextItem(literal, category, rule)
     assert isinstance(item.to_dict(), dict)
예제 #12
0
 def test_custom_patterns_list(self):
     """Test that rules are loaded from a list"""
     item = ConTextItem("evidence of", "DEFINITE_EXISTENCE", "forward")
     context = ConTextComponent(nlp, rules="other", rule_list=[item])
     assert context.item_data
예제 #13
0
 def test_instantiate1(self):
     literal = "no evidence of"
     category = "DEFINITE_NEGATED_EXISTENCE"
     rule = "forward"
     assert ConTextItem(literal, category, rule)
예제 #14
0
 def test_from_dict(self):
     d = dict(literal="reason for examination",
              category="INDICATION",
              rule="FORWARD")
     assert ConTextItem.from_dict(d)
예제 #15
0
 def test_limit_scope(self):
     """Test that a 'TERMINATE' TagObject limits the scope of the tag object"""
     doc, item, tag_object = self.create_objects()
     item2 = ConTextItem("but", "TERMINATE", "TERMINATE")
     tag_object2 = TagObject(item2, 2, 4, doc)
     assert tag_object.limit_scope(tag_object2)
예제 #16
0
 def test_custom_rules_match(self):
     item = ConTextItem("no evidence of", "NEGATED_EXISTENCE", "forward")
     context = ConTextComponent(nlp, rules="other", rule_list=[item])
     matcher = context.phrase_matcher
     assert matcher(nlp("no evidence of"))
예제 #17
0
 def test_default_terminate(self):
     item = ConTextItem("no evidence of",
                        "NEGATED_EXISTENCE",
                        "FORWARD",
                        terminated_by=None)
     assert item.terminated_by == set()
예제 #18
0
 def test_limit_scope2(self):
     doc, item, tag_object = self.create_objects()
     item2 = ConTextItem("but", "TERMINATE", "TERMINATE")
     tag_object2 = TagObject(item2, 2, 4, doc)
     assert not tag_object2.limit_scope(tag_object)
예제 #19
0
 def test_custom_terminate(self):
     item = ConTextItem("no evidence of",
                        "NEGATED_EXISTENCE",
                        "FORWARD",
                        terminated_by={"POSITIVE_EXISTENCE"})
     assert item.terminated_by == {"POSITIVE_EXISTENCE"}