Extractors Module
anonipy.anonymize.extractors
Module containing the extractors
.
The extractors
module provides a set of extractors used to identify relevant
information within a document.
Classes:
Name | Description |
---|---|
NERExtractor |
The class representing the named entity recognition (NER) extractor. |
PatternExtractor |
The class representing the pattern extractor. |
MultiExtractor |
The class representing the multi extractor. |
anonipy.anonymize.extractors.NERExtractor
Bases: ExtractorInterface
The class representing the named entity recognition (NER) extractor.
Examples:
>>> from anonipy.constants import LANGUAGES
>>> from anonipy.anonymize.extractors import NERExtractor
>>> labels = [{"label": "PERSON", "type": "string"}]
>>> extractor = NERExtractor(labels, lang=LANGUAGES.ENGLISH)
>>> extractor("John Doe is a 19 year old software engineer.", detect_repeats=False)
Doc, [Entity]
Attributes:
Name | Type | Description |
---|---|---|
labels |
List[dict]
|
The list of labels to extract. |
lang |
str
|
The language of the text to extract. |
score_th |
float
|
The score threshold. |
use_gpu |
bool
|
Whether to use GPU. |
gliner_model |
str
|
The gliner model to use. |
pipeline |
Language
|
The spacy pipeline for extracting entities. |
spacy_style |
str
|
The style the entities should be stored in the spacy doc. |
Methods:
Name | Description |
---|---|
__call__ |
Extract the entities from the text. |
display |
Display the entities in the text. |
Source code in anonipy/anonymize/extractors/ner_extractor.py
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
__init__(labels, *args, lang=LANGUAGES.ENGLISH, score_th=0.5, use_gpu=False, gliner_model='urchade/gliner_multi_pii-v1', spacy_style='ent', **kwargs)
Initialize the named entity recognition (NER) extractor.
Examples:
>>> from anonipy.constants import LANGUAGES
>>> from anonipy.anonymize.extractors import NERExtractor
>>> labels = [{"label": "PERSON", "type": "string"}]
>>> extractor = NERExtractor(labels, lang=LANGUAGES.ENGLISH)
NERExtractor()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels
|
List[dict]
|
The list of labels to extract. |
required |
lang
|
LANGUAGES
|
The language of the text to extract. |
ENGLISH
|
score_th
|
float
|
The score threshold. Entities with a score below this threshold will be ignored. |
0.5
|
use_gpu
|
bool
|
Whether to use GPU. |
False
|
gliner_model
|
str
|
The gliner model to use to identify the entities. |
'urchade/gliner_multi_pii-v1'
|
spacy_style
|
str
|
The style the entities should be stored in the spacy doc. Options: |
'ent'
|
Source code in anonipy/anonymize/extractors/ner_extractor.py
__call__(text, detect_repeats=False, *args, **kwargs)
Extract the entities from the text.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to extract entities from. |
required |
detect_repeats
|
bool
|
Whether to check text again for repeated entities. |
False
|
Returns:
Type | Description |
---|---|
Doc
|
The spacy document. |
List[Entity]
|
The list of extracted entities. |
Source code in anonipy/anonymize/extractors/ner_extractor.py
display(doc, page=False, jupyter=None)
Display the entities in the text.
Examples:
>>> doc, entities = extractor("John Doe is a 19 year old software engineer.")
>>> extractor.display(doc)
HTML
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc
|
Doc
|
The spacy doc to display. |
required |
page
|
bool
|
Whether to display the doc in a web browser. |
False
|
jupyter
|
bool
|
Whether to display the doc in a jupyter notebook. |
None
|
Returns:
Type | Description |
---|---|
str
|
The HTML representation of the document and the extracted entities. |
Source code in anonipy/anonymize/extractors/ner_extractor.py
anonipy.anonymize.extractors.PatternExtractor
Bases: ExtractorInterface
The class representing the pattern extractor.
Examples:
>>> from anonipy.constants import LANGUAGES
>>> from anonipy.anonymize.extractors import PatternExtractor
>>> labels = [{"label": "PERSON", "type": "string", "regex": "([A-Z][a-z]+ [A-Z][a-z]+)"}]
>>> extractor = PatternExtractor(labels, lang=LANGUAGES.ENGLISH)
>>> extractor("John Doe is a 19 year old software engineer.", detect_repeats=False)
Doc, [Entity]
Attributes:
Name | Type | Description |
---|---|---|
labels |
List[dict]
|
The list of labels and patterns to extract. |
lang |
str
|
The language of the text to extract. |
pipeline |
Language
|
The spacy pipeline for extracting entities. |
token_matchers |
Matcher
|
The spacy token pattern matcher. |
global_matchers |
function
|
The global pattern matcher. |
Methods:
Name | Description |
---|---|
__call__ |
Extract the entities from the text. |
display |
Display the entities in the text. |
Source code in anonipy/anonymize/extractors/pattern_extractor.py
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
__init__(labels, *args, lang=LANGUAGES.ENGLISH, spacy_style='ent', **kwargs)
Initialize the pattern extractor.
Examples:
>>> from anonipy.constants import LANGUAGES
>>> from anonipy.anonymize.extractors import PatternExtractor
>>> labels = [{"label": "PERSON", "type": "string", "regex": "([A-Z][a-z]+ [A-Z][a-z]+)"}]
>>> extractor = PatternExtractor(labels, lang=LANGUAGES.ENGLISH)
PatternExtractor()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels
|
List[dict]
|
The list of labels and patterns to extract. |
required |
lang
|
LANGUAGES
|
The language of the text to extract. |
ENGLISH
|
spacy_style
|
str
|
The style the entities should be stored in the spacy doc. Options: |
'ent'
|
Source code in anonipy/anonymize/extractors/pattern_extractor.py
__call__(text, detect_repeats=False, *args, **kwargs)
Extract the entities from the text.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to extract entities from. |
required |
detect_repeats
|
bool
|
Whether to check text again for repeated entities. |
False
|
Returns:
Type | Description |
---|---|
Doc
|
The spacy document. |
List[Entity]
|
The list of extracted entities. |
Source code in anonipy/anonymize/extractors/pattern_extractor.py
display(doc, page=False, jupyter=None)
Display the entities in the text.
Examples:
>>> doc, entities = extractor("John Doe is a 19 year old software engineer.")
>>> extractor.display(doc)
HTML
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc
|
Doc
|
The spacy doc to display. |
required |
page
|
bool
|
Whether to display the doc in a web browser. |
False
|
jupyter
|
bool
|
Whether to display the doc in a jupyter notebook. |
None
|
Returns:
Type | Description |
---|---|
str
|
The HTML representation of the document and the extracted entities. |
Source code in anonipy/anonymize/extractors/pattern_extractor.py
anonipy.anonymize.extractors.MultiExtractor
The class representing the multi extractor.
Examples:
>>> from anonipy.constants import LANGUAGES
>>> from anonipy.anonymize.extractors import NERExtractor, PatternExtractor, MultiExtractor
>>> extractors = [
>>> NERExtractor(ner_labels, lang=LANGUAGES.ENGLISH),
>>> PatternExtractor(pattern_labels, lang=LANGUAGES.ENGLISH),
>>> ]
>>> extractor = MultiExtractor(extractors)
>>> extractor("John Doe is a 19 year old software engineer.", detect_repeats=False)
[(Doc, [Entity]), (Doc, [Entity])], [Entity]
Attributes:
Name | Type | Description |
---|---|---|
extractors |
List[ExtractorInterface]
|
The list of extractors to use. |
Methods:
Name | Description |
---|---|
__call__ |
Extract the entities fron the text using the provided extractors. |
display |
Display the entities extracted from the text document. |
Source code in anonipy/anonymize/extractors/multi_extractor.py
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 |
|
__init__(extractors)
Initialize the multi extractor.
Examples:
>>> from anonipy.constants import LANGUAGES
>>> from anonipy.anonymize.extractors import NERExtractor, PatternExtractor, MultiExtractor
>>> extractors = [
>>> NERExtractor(ner_labels, lang=LANGUAGES.ENGLISH),
>>> PatternExtractor(pattern_labels, lang=LANGUAGES.ENGLISH),
>>> ]
>>> extractor = MultiExtractor(extractors)
MultiExtractor()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extractors
|
List[ExtractorInterface]
|
The list of extractors to use. |
required |
Source code in anonipy/anonymize/extractors/multi_extractor.py
__call__(text, detect_repeats=False)
Extract the entities fron the text using the provided extractors.
Examples:
>>> extractor("John Doe is a 19 year old software engineer.", detect_repeats=False)
[(Doc, [Entity]), (Doc, [Entity])], [Entity]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to extract entities from. |
required |
detect_repeats
|
bool
|
Whether to check text again for repeated entities. |
False
|
Returns:
Type | Description |
---|---|
List[Tuple[Doc, List[Entity]]]
|
The list of extractor outputs containing the tuple (spacy document, extracted entities). |
List[Entity]
|
The list of joint entities. |
Source code in anonipy/anonymize/extractors/multi_extractor.py
display(doc, page=False, jupyter=None)
Display the entities in the text.
Examples:
>>> extractor_outputs, entities = extractor("John Doe is a 19 year old software engineer.")
>>> extractor.display(extractor_outputs[0][0])
HTML
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc
|
Doc
|
The spacy doc to display. |
required |
page
|
bool
|
Whether to display the doc in a web browser. |
False
|
jupyter
|
bool
|
Whether to display the doc in a jupyter notebook. |
None
|
Returns:
Type | Description |
---|---|
str
|
The HTML representation of the document and the extracted entities. |
Source code in anonipy/anonymize/extractors/multi_extractor.py
anonipy.anonymize.extractors.ExtractorInterface
The class representing the extractor interface.
All extractors should inherit from this class.
Methods:
Name | Description |
---|---|
__call__ |
Extract entities from the text. |