medkit.text.context.hypothesis_detector
medkit.text.context.hypothesis_detector#
Classes:
|
Annotator creating hypothesis Attributes with boolean values indicating if an hypothesis has been found. |
|
Regexp-based rule to use with HypothesisDetector |
|
Metadata dict added to hypothesis attributes with True value detected by a rule |
|
Metadata dict added to hypothesis attributes with True value detected by a rule. |
- class HypothesisDetector(output_label='hypothesis', rules=None, verbs=None, modes_and_tenses=None, max_length=150, uid=None)[source]#
Annotator creating hypothesis Attributes with boolean values indicating if an hypothesis has been found.
Hypothesis will be considered present either because of the presence of a certain text pattern in a segment, or because of the usage of a certain verb at a specific mode and tense (for instance conditional).
Because hypothesis attributes will be attached to whole segments, each input segment should be “local”-enough (ie a sentence or a syntagma) rather than a big chunk of text.
Instantiate the hypothesis detector
- Parameters
output_label (
str
) – The label of the created attributesrules (
Optional
[List
[HypothesisDetectorRule
]]) – The set of rules to use when detecting hypothesis. If none provided, the rules in “hypothesis_detector_default_rules.yml” will be usedverbs (
Optional
[Dict
[str
,Dict
[str
,Dict
[str
,List
[str
]]]]]) – Conjugated verbs forms, to be used in association with modes_and_tenses. Conjugated forms of a verb at a specific mode and tense must be provided in nested dicts with the 1st key being the verb’s root, the 2d key the mode and the 3d key the tense. For instance verb[“aller”][“indicatif][“présent”] would hold the list [“vais”, “vas”, “va”, “allons”, aller”, “vont”] When verbs is provided, modes_and_tenses must also be provided. If none provided, the rules in “hypothesis_detector_default_verbs.yml” will be used.modes_and_tenses (
Optional
[List
[Tuple
[str
,str
]]]) – List of tuples of all modes and tenses associated with hypothesis. Will be used to select conjugated forms in verbs that denote hypothesis.max_length (
int
) – Maximum number of characters in a hypothesis segment. Segments longer than this will never be considered as hypothesisuid (str) – Identifier of the detector
Methods:
check_rules_sanity
(rules)Check consistency of a set of rules
Instantiate an HypothesisDetector with example rules and verbs, designed for usage with EDS documents
load_rules
(path_to_rules[, encoding])Load all rules stored in a yml file
load_verbs
(path_to_verbs[, encoding])Load all conjugated verb forms stored in a yml file.
run
(segments)Add an hypothesis attribute to each segment with a boolean value indicating if an hypothesis has been detected.
save_rules
(rules, path_to_rules[, encoding])Store rules in a yml file
set_prov_tracer
(prov_tracer)Enable provenance tracing.
Attributes:
Contains all the operation init parameters.
- run(segments)[source]#
Add an hypothesis attribute to each segment with a boolean value indicating if an hypothesis has been detected.
Hypothesis attributes with a True value have a metadata dict with fields described in either
HypothesisRuleMetadata
orHypothesisVerbMetadata
.- Parameters
segments (
List
[Segment
]) – List of segments to detect as being hypothesis or not
- static load_verbs(path_to_verbs, encoding=None)[source]#
Load all conjugated verb forms stored in a yml file. Conjugated verb forms at a specific mode and tense must be stored in nested mappings with the 1st key being the verb root, the 2d key the mode and the 3d key the tense.
- Parameters
path_to_verbs (
Path
) – Path to a yml file containing a list of verbs form, arranged by mode and tense.encoding (
Optional
[str
]) – Encoding on the file to open
- Return type
Dict
[str
,Dict
[str
,Dict
[str
,List
[str
]]]]- Returns
List[Dict[str, Dict[str, List[str]]]] – List of verb forms in path_to_verbs, can be used to init an HypothesisDetector
- static load_rules(path_to_rules, encoding=None)[source]#
Load all rules stored in a yml file
- Parameters
path_to_rules (
Path
) – Path to a yml file containing a list of mappings with the same structure as HypothesisDetectorRuleencoding (
Optional
[str
]) – Encoding of the file to open
- Return type
List
[HypothesisDetectorRule
]- Returns
List[HypothesisDetectorRule] – List of all the rules in path_to_rules, can be used to init an HypothesisDetector
- classmethod get_example()[source]#
Instantiate an HypothesisDetector with example rules and verbs, designed for usage with EDS documents
- Return type
- static save_rules(rules, path_to_rules, encoding=None)[source]#
Store rules in a yml file
- Parameters
rules (
List
[HypothesisDetectorRule
]) – The rules to savepath_to_rules (
Path
) – Path to a .yml file that will contain the rulesencoding (
Optional
[str
]) – Encoding of the .yml file
- property description: medkit.core.operation_desc.OperationDescription#
Contains all the operation init parameters.
- Return type
- set_prov_tracer(prov_tracer)#
Enable provenance tracing.
- Parameters
prov_tracer (
ProvTracer
) – The provenance tracer used to trace the provenance.
- class HypothesisDetectorRule(regexp, exclusion_regexps=<factory>, id=None, case_sensitive=False, unicode_sensitive=False)[source]#
Regexp-based rule to use with HypothesisDetector
- Variables
regexp (str) – The regexp pattern used to match a hypothesis
exclusion_regexps (List[str]) – Optional exclusion patterns
id (Optional[str]) – Unique identifier of the rule to store in the metadata of the entities
case_sensitive (bool) – Whether to ignore case when running regexp and `exclusion_regexps
unicode_sensitive (bool) – Whether to replace all non-ASCII chars by the closest ASCII chars on input text before running regexp and `exclusion_regexps. If True, then regexp and `exclusion_regexps shouldn’t contain non-ASCII chars because they would never be matched.
- class HypothesisRuleMetadata(_typename, _fields=None, /, **kwargs)[source]#
Metadata dict added to hypothesis attributes with True value detected by a rule
- Parameters
type (Literal['rule']) – Metadata type, here “rule” (use to differentiate between rule/verb metadata dict)
rule_id (str) – Identifier of the rule used to detect an hypothesis. If the rule has no uid, then the index of the rule in the list of rules is used instead
- class HypothesisVerbMetadata(_typename, _fields=None, /, **kwargs)[source]#
Metadata dict added to hypothesis attributes with True value detected by a rule.
- Parameters
type (Literal['verb']) – Metadata type, here “verb” (use to differentiate between rule/verb metadata dict).
matched_verb (str) – Root of the verb used to detect an hypothesis.