Skip to content

medspacy.postprocess.postprocessing_rule

PostprocessingRule

Source code in medspacy/postprocess/postprocessing_rule.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class PostprocessingRule:
    def __init__(
        self,
        patterns: Iterable[PostprocessingPattern],
        action: Callable,
        name: str = None,
        description: str = None,
        span_group_name: str = "medspacy_spans",
        **kwargs,
    ):
        """
        A PostprocessingRule checks conditions of a spaCy Span entity and executes some action if all rules are met.

        patterns: A list of PostprocessingPatterns, each of which check a condition of an entity.
        action: A function to call with the entity as an argument. This function should take the following arguments:
            ent: The spacy span
            i: The index of ent
            input_span_type: "ents" or "group". Describes where to look for spans.
            span_group_name: The name of the span group used when `input_span_type` is "group".
            kwargs: Any additional keyword arguments for action.
        name: Optional name of direction.
        description: Optional description of the direction.
        kwargs: Optional keyword arguments to send to `action`.

        """
        self.patterns = patterns
        self.action = action
        self.name = name
        self.description = description
        self.input_span_type = None
        self.span_group_name = span_group_name
        self.kwargs = kwargs

    def __call__(self, ent, i, debug=False):
        """
        Iterate through all the rules in self.rules.
        If any pattern does not pass (ie., return True), then returns False.
        If they all pass, execute self.action and return True.
        """
        for pattern in self.patterns:
            # If this is a tuple, at least one has to pass
            if isinstance(pattern, tuple):
                passed = False
                for subpattern in pattern:
                    rslt = subpattern(ent)
                    if rslt is True:
                        passed = True
                        break
                if passed is False:
                    return False
            # Otherwise just check a single value
            else:
                rslt = pattern(ent)
                if rslt is False:
                    return False

        # Every pattern passed - do the action
        if debug:
            print("Passed:", self, "on ent:", ent, ent.sent)

        try:
            if self.kwargs:
                self.action(
                    ent, i, self.input_span_type, self.span_group_name, **self.kwargs
                )
            else:
                self.action(ent, i, self.input_span_type, self.span_group_name)
        except TypeError:
            _raise_action_error(
                self.action,
                (ent, i, self.input_span_type, self.span_group_name, self.kwargs),
            )

    def __repr__(self):
        return f"PostprocessingRule: {self.name} - {self.description}"

__call__(ent, i, debug=False)

Iterate through all the rules in self.rules. If any pattern does not pass (ie., return True), then returns False. If they all pass, execute self.action and return True.

Source code in medspacy/postprocess/postprocessing_rule.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def __call__(self, ent, i, debug=False):
    """
    Iterate through all the rules in self.rules.
    If any pattern does not pass (ie., return True), then returns False.
    If they all pass, execute self.action and return True.
    """
    for pattern in self.patterns:
        # If this is a tuple, at least one has to pass
        if isinstance(pattern, tuple):
            passed = False
            for subpattern in pattern:
                rslt = subpattern(ent)
                if rslt is True:
                    passed = True
                    break
            if passed is False:
                return False
        # Otherwise just check a single value
        else:
            rslt = pattern(ent)
            if rslt is False:
                return False

    # Every pattern passed - do the action
    if debug:
        print("Passed:", self, "on ent:", ent, ent.sent)

    try:
        if self.kwargs:
            self.action(
                ent, i, self.input_span_type, self.span_group_name, **self.kwargs
            )
        else:
            self.action(ent, i, self.input_span_type, self.span_group_name)
    except TypeError:
        _raise_action_error(
            self.action,
            (ent, i, self.input_span_type, self.span_group_name, self.kwargs),
        )

__init__(patterns, action, name=None, description=None, span_group_name='medspacy_spans', **kwargs)

A PostprocessingRule checks conditions of a spaCy Span entity and executes some action if all rules are met.

patterns: A list of PostprocessingPatterns, each of which check a condition of an entity. action: A function to call with the entity as an argument. This function should take the following arguments: ent: The spacy span i: The index of ent input_span_type: "ents" or "group". Describes where to look for spans. span_group_name: The name of the span group used when input_span_type is "group". kwargs: Any additional keyword arguments for action. name: Optional name of direction. description: Optional description of the direction. kwargs: Optional keyword arguments to send to action.

Source code in medspacy/postprocess/postprocessing_rule.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    patterns: Iterable[PostprocessingPattern],
    action: Callable,
    name: str = None,
    description: str = None,
    span_group_name: str = "medspacy_spans",
    **kwargs,
):
    """
    A PostprocessingRule checks conditions of a spaCy Span entity and executes some action if all rules are met.

    patterns: A list of PostprocessingPatterns, each of which check a condition of an entity.
    action: A function to call with the entity as an argument. This function should take the following arguments:
        ent: The spacy span
        i: The index of ent
        input_span_type: "ents" or "group". Describes where to look for spans.
        span_group_name: The name of the span group used when `input_span_type` is "group".
        kwargs: Any additional keyword arguments for action.
    name: Optional name of direction.
    description: Optional description of the direction.
    kwargs: Optional keyword arguments to send to `action`.

    """
    self.patterns = patterns
    self.action = action
    self.name = name
    self.description = description
    self.input_span_type = None
    self.span_group_name = span_group_name
    self.kwargs = kwargs