medspacy.target_matcher.target_rule
TargetRule
Bases: BaseRule
TargetRule defines rules for extracting entities from text using the TargetMatcher.
Source code in medspacy/target_matcher/target_rule.py
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
__init__(literal, category, pattern=None, on_match=None, attributes=None, metadata=None)
Creates a new TargetRule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
literal
|
str
|
The string representation of a concept. If |
required |
category
|
str
|
The semantic class of the matched span. This corresponds to the |
required |
pattern
|
Optional[Union[List[Dict[str, str]], str]]
|
A list or string to use as a spaCy pattern rather than |
None
|
on_match
|
Optional[Callable[[Matcher, Doc, int, List[Tuple[int, int, int]]], Any]]
|
An optional callback function or other callable which takes 4 arguments: |
None
|
attributes
|
Optional[Dict[str, Any]]
|
Optional custom attribute names to set for a Span matched by the direction. These attribute
names are stored under Span..[attribute_name]. For example, if |
None
|
metadata
|
Optional[Dict[Any, Any]]
|
Optional dictionary of any extra metadata. |
None
|
Source code in medspacy/target_matcher/target_rule.py
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 | |
from_dict(rule_dict)
classmethod
Reads a dictionary into a ConTextRule. Used when reading from a json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_dict
|
Dict
|
the dictionary to convert |
required |
Returns:
| Type | Description |
|---|---|
TargetRule
|
The ConTextRule created from the dictionary |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the json is invalid |
Source code in medspacy/target_matcher/target_rule.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
from_json(filepath)
classmethod
Read in a lexicon of modifiers from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
the .json file containing modifier rules |
required |
Returns:
| Name | Type | Description |
|---|---|---|
context_item |
List[TargetRule]
|
A list of ConTextRule objects. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the dictionary contains any keys other than those accepted by ConTextRule.init |
Source code in medspacy/target_matcher/target_rule.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
to_dict()
Converts TargetRules to a python dictionary. Used when writing target rules to a json file.
Returns:
| Type | Description |
|---|---|
|
The dictionary containing the TargetRule info. |
Source code in medspacy/target_matcher/target_rule.py
119 120 121 122 123 124 125 126 127 128 129 130 | |
to_json(target_rules, filepath)
classmethod
Writes ConTextItems to a json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_rules
|
List[TargetRule]
|
a list of TargetRules that will be written to a file. |
required |
filepath
|
str
|
the .json file to contain modifier rules |
required |
Source code in medspacy/target_matcher/target_rule.py
105 106 107 108 109 110 111 112 113 114 115 116 117 | |