diff --git a/docs/tutorials/00_GettingStarted.md b/docs/tutorials/00_GettingStarted.md index 87eab5e5..84fae5cf 100644 --- a/docs/tutorials/00_GettingStarted.md +++ b/docs/tutorials/00_GettingStarted.md @@ -108,8 +108,8 @@ Inspecting the debug data gives a lot of info: - `text`: Effective text considered for detection. - `EmergencyRegex`: melusine used an `EmergencyRegex` object to run detection. - `match_result`: The `EmergencyRegex` did not match the text. -- `positive_match_data`: The `EmergencyRegex` matched **positively** the text pattern "Urgent" (Required condition). -- `negative_match_data`: The `EmergencyRegex` matched **negatively** the text pattern "Mr. Annoying" (Forbidden condition). +- `positive`: The `EmergencyRegex` matched **positively** the text pattern "Urgent" (Required condition). +- `negative`: The `EmergencyRegex` matched **negatively** the text pattern "Mr. Annoying" (Forbidden condition). - `BLACKLIST`: Detection groups can be defined to easily link a matching pattern to the corresponding regex. DEFAULT is used if no detection group is specified. ```Python @@ -118,12 +118,12 @@ Inspecting the debug data gives a lot of info: 'text': 'Latest news\nUrgent update about Mr. Annoying'}, 'EmergencyRegex': { 'match_result': False, - 'negative_match_data': { + 'negative': { 'BLACKLIST': [ {'match_text': 'Mr. Annoying', 'start': 32, 'stop': 44} ]}, - 'neutral_match_data': {}, - 'positive_match_data': { + 'neutral': {}, + 'positive': { 'DEFAULT': [ {'match_text': 'Urgent', 'start': 12, 'stop': 18} ] diff --git a/docs/tutorials/08_MelusineRegex.md b/docs/tutorials/08_MelusineRegex.md index e974b798..eba78324 100644 --- a/docs/tutorials/08_MelusineRegex.md +++ b/docs/tutorials/08_MelusineRegex.md @@ -48,8 +48,8 @@ to_delete_detection = to_delete_regex( "You shall not pass through the magical portal of the lake from Monday to Thursday as it is currently under repair." ) -print(to_delete_detection[MelusineRegex.MATCH_RESULT]) # (1)! -print(to_delete_detection[MelusineRegex.POSITIVE_MATCH_FIELD]) # (2)! +print(to_delete_detection.match_result) # (1)! +print(to_delete_detection.positive) # (2)! ``` 1. Prints `True` @@ -96,7 +96,7 @@ to_delete_regex = AnnoyingEmailsRegex() email = "You shall not pass through the magical portal of the lake from Monday to Thursday as it is currently under repair." to_delete_detection = to_delete_regex(email) -print(to_delete_detection[MelusineRegex.MATCH_RESULT]) # (1)! +print(to_delete_detection.match_result) # (1)! ``` 1. Prints `False` @@ -116,9 +116,9 @@ class PreMatchHookAnnoyingEmailsRegex(AnnoyingEmailsRegex): return text preprocessed_to_delete_regex = PreMatchHookAnnoyingEmailsRegex() - spell_result = preprocessed_to_delete_regex.get_match_result( + spell_result = preprocessed_to_delete_regex( "Andthen,, I told Morgana 'You sholl not pass!' as she wanted topass... Im stil wonddering why she did not find it fundny..." - ) + ).match_result ``` ## Using the regex result @@ -129,9 +129,9 @@ The complete `to_delete_detection` object looks like this: ``` json { "match_result":False, - "neutral_match_data":{ + "neutral":{ }, - "negative_match_data":{ + "negative":{ "PORTAL_MENTIONNED":[ { "start":31, @@ -140,7 +140,7 @@ The complete `to_delete_detection` object looks like this: } ] }, - "positive_match_data":{ + "positive":{ "GANDALF_BEING_GANDALF":[ { "start":0, @@ -155,7 +155,7 @@ The complete `to_delete_detection` object looks like this: For a more straightforward approach, if you only need the regex result, you can use the following syntax: ``` python -spell_analysis_result: bool = to_delete_regex.get_match_result(email) # (1)! +spell_analysis_result: bool = to_delete_regex(email).match_result # (1)! ``` 1. Returns `True` Some of the older fairies might need a more detailed explanation on what triggered the deletion of an email. diff --git a/melusine/__init__.py b/melusine/__init__.py index d95d8520..ffbf66f3 100644 --- a/melusine/__init__.py +++ b/melusine/__init__.py @@ -3,11 +3,19 @@ import logging from melusine._config import config +from melusine.base import MelusineDetector, MelusineRegex, MelusineRegexFullMatch, MelusineTransformer from melusine.pipeline import MelusinePipeline -__all__ = ["config", "MelusinePipeline"] +__all__ = [ + "config", + "MelusineDetector", + "MelusineRegex", + "MelusineRegexFullMatch", + "MelusineTransformer", + "MelusinePipeline", +] -VERSION = (3, 3, 4) +VERSION = (3, 4, 0) __version__ = ".".join(map(str, VERSION)) # ------------------------------- # diff --git a/melusine/base.py b/melusine/base.py index 9bce041d..2ec460f8 100644 --- a/melusine/base.py +++ b/melusine/base.py @@ -20,6 +20,7 @@ import warnings from abc import ABC, abstractmethod from collections.abc import Callable, Iterable +from dataclasses import dataclass, field from typing import Any, TypeAlias, TypeVar import pandas as pd @@ -39,6 +40,19 @@ class TransformError(Exception): """Exception raised when an error occurs during the transform operation.""" +class DeprecatedClassAttribute: + """Descriptor exposing a deprecated class/instance attribute with warning.""" + + def __init__(self, value: str, message: str): + self._value = value + self._message = message + + def __get__(self, instance, owner) -> str: + """Get""" + warnings.warn(self._message, DeprecationWarning, stacklevel=2) + return self._value + + class MelusineTransformer(IoMixin): """Define a MelusineTransformer object. @@ -350,25 +364,138 @@ class MissingFieldError(Exception): """Exception raised when a missing field is encountered by a MelusineTransformer""" -MatchData = dict[str, list[dict[str, Any]]] +@dataclass(frozen=True) +class MelusineRegexUnitMatch: + """Dataclass to structure a Unitary MelusineRegex match""" + + start: int + stop: int + match_text: str + + def __getitem__(self, index): + """GetItem""" + return getattr(self, index) + + +MatchData: TypeAlias = dict[str, list[MelusineRegexUnitMatch]] + + +@dataclass +class MelusineRegexFullMatch: + """Dataclass to structure a Full MelusineRegex match""" + + match_result: bool = False + positive: MatchData = field(default_factory=dict) + neutral: MatchData = field(default_factory=dict) + negative: MatchData = field(default_factory=dict) + + @property + def positive_match_data(self): + """Legacy attribute name for compatibility""" + warnings.warn( + "positive_match_data attribute is deprecated, please use positive instead", + DeprecationWarning, + stacklevel=2, + ) + return self.positive + + @property + def neutral_match_data(self): + """Legacy attribute name for compatibility""" + warnings.warn( + "neutral_match_data attribute is deprecated, please use neutral instead", + DeprecationWarning, + stacklevel=2, + ) + return self.neutral + + @property + def negative_match_data(self): + """Legacy attribute name for compatibility""" + warnings.warn( + "negative_match_data attribute is deprecated, please use negative instead", + DeprecationWarning, + stacklevel=2, + ) + return self.negative + + def reset_match(self, value: bool = False): + """Reset the match value""" + self.match_result = value + + def reset_positive(self, values: MatchData | None = None) -> None: + """Reset the positive value""" + self.positive = values or {} + + def reset_neutral(self, values: MatchData | None = None) -> None: + """Reset the neutral value""" + self.neutral = values or {} + + def reset_negative(self, values: MatchData | None = None) -> None: + """Reset the negative value""" + self.negative = values or {} + + @property + def positive_keys(self): + """Return the keys of positive match data.""" + return self.positive.keys() + + @property + def neutral_keys(self): + """Return the keys of neutral match data.""" + return self.neutral.keys() + + @property + def negative_keys(self): + """Return the keys of negative match data.""" + return self.negative.keys() + + def __getitem__(self, index): + """Legacy dict like access""" + warnings.warn( + "Dict like access is deprecated, please use direct attribute access instead", + DeprecationWarning, + stacklevel=2, + ) + return getattr(self, index) class MelusineRegex(ABC): - """Class to standardise text pattern detection using regex.""" + """Class to standardize text pattern detection using regex.""" REGEX_FLAGS: re.RegexFlag = re.IGNORECASE | re.MULTILINE PAIRED_MATCHING_PREFIX: str = "_" - # Match fields - MATCH_RESULT: str = "match_result" - NEUTRAL_MATCH_FIELD: str = "neutral_match_data" - POSITIVE_MATCH_FIELD: str = "positive_match_data" - NEGATIVE_MATCH_FIELD: str = "negative_match_data" - - # Match data - MATCH_START: str = "start" - MATCH_STOP: str = "stop" - MATCH_TEXT: str = "match_text" + # Legacy aliases kept for compatibility + MATCH_RESULT = DeprecatedClassAttribute( + value="match_result", + message="MATCH_RESULT is deprecated, please use output.match_result instead", + ) + NEUTRAL_MATCH_FIELD = DeprecatedClassAttribute( + value="neutral_match_data", + message="NEUTRAL_MATCH_FIELD is deprecated, please use my_result.neutral instead", + ) + POSITIVE_MATCH_FIELD = DeprecatedClassAttribute( + value="positive_match_data", + message="POSITIVE_MATCH_FIELD is deprecated, please use output.positive instead", + ) + NEGATIVE_MATCH_FIELD = DeprecatedClassAttribute( + value="negative_match_data", + message="NEGATIVE_MATCH_FIELD is deprecated, please use output.negative instead", + ) + + MATCH_START = DeprecatedClassAttribute( + value="start", + message="MATCH_START is deprecated", + ) + MATCH_STOP = DeprecatedClassAttribute( + value="stop", + message="MATCH_STOP is deprecated", + ) + MATCH_TEXT = DeprecatedClassAttribute( + value="match_text", + message="MATCH_TEXT is deprecated", + ) def __init__(self, substitution_pattern: str = " ", default_match_group: str = "DEFAULT"): if not isinstance(substitution_pattern, str) or (len(substitution_pattern) > 1): @@ -437,9 +564,7 @@ def no_match_list(self) -> list[str]: """ - def _get_match( - self, text: str, base_regex: str | dict[str, str], regex_group: str | None = None - ) -> dict[str, list[dict[str, Any]]]: + def _get_match(self, text: str, base_regex: str | dict[str, str], regex_group: str | None = None) -> MatchData: """Run specified regex on the input text and return a dict with matching group as key. Args: @@ -469,11 +594,11 @@ def _get_match( start, stop = match.span() match_data_dict[regex_group].append( - { - self.MATCH_START: start, - self.MATCH_STOP: stop, - self.MATCH_TEXT: text[start:stop], - } + MelusineRegexUnitMatch( + start=start, + stop=stop, + match_text=text[start:stop], + ) ) return match_data_dict @@ -481,7 +606,7 @@ def _get_match( def ignore_text( self, text: str, - match_data_dict: dict[str, list[dict[str, Any]]], + match_data_dict: MatchData, ) -> str: """Replace neutral regex match text with substitution text to ignore it. @@ -495,8 +620,8 @@ def ignore_text( """ for _, match_list in match_data_dict.items(): for match_data in match_list: - start = match_data[self.MATCH_START] - stop = match_data[self.MATCH_STOP] + start = match_data.start + stop = match_data.stop # Mask text to ignore text = text[:start] + self.substitution_pattern * (stop - start) + text[stop:] @@ -504,20 +629,16 @@ def ignore_text( return text def get_match_result(self, text: str) -> bool: - """Apply MelusineRegex patterns (neutral, negative and positive) on the input text. - Return a boolean output of the match result. - - Args: - text: input text. - - Returns: - _: True if the MelusineRegex matches the input text. - - """ - result = self(text) - return result[self.MATCH_RESULT] + """Legacy method for compatibility""" + warnings.warn( + "get_match_result method is deprecated, please use result.match_result instead.", + DeprecationWarning, + stacklevel=2, + ) + result: MelusineRegexFullMatch = self(text) + return result.match_result - def __call__(self, text: str) -> dict[str, Any]: + def __call__(self, text: str) -> MelusineRegexFullMatch: """Apply MelusineRegex patterns (neutral, negative and positive) on the input text. Return a detailed output of the match results as a dict. @@ -531,31 +652,31 @@ def __call__(self, text: str) -> dict[str, Any]: # Apply pre match hook text = self.pre_match_hook(text) - match_dict = { - self.MATCH_RESULT: False, - self.NEUTRAL_MATCH_FIELD: {}, - self.NEGATIVE_MATCH_FIELD: {}, - self.POSITIVE_MATCH_FIELD: {}, - } + match_dict = MelusineRegexFullMatch( + match_result=False, + neutral={}, + negative={}, + positive={}, + ) negative_match = False if self.neutral: neutral_match_data = self._get_match(text=text, base_regex=self.neutral) - match_dict[self.NEUTRAL_MATCH_FIELD] = neutral_match_data + match_dict.neutral = neutral_match_data text = self.ignore_text(text, neutral_match_data) if self.negative: negative_match_data = self._get_match(text=text, base_regex=self.negative) negative_match = bool(negative_match_data) - match_dict[self.NEGATIVE_MATCH_FIELD] = negative_match_data + match_dict.negative = negative_match_data positive_match_data = self._get_match(text=text, base_regex=self.positive) positive_match = bool(positive_match_data) - match_dict[self.POSITIVE_MATCH_FIELD] = positive_match_data + match_dict.positive = positive_match_data - match_dict[self.MATCH_RESULT] = positive_match and not negative_match + match_dict.match_result = positive_match and not negative_match # Apply post match hook match_dict = self.post_match_hook(match_dict) @@ -571,7 +692,7 @@ def describe(self, text: str, position: bool = False) -> None: """ - def _describe_match_field(match_field_data: dict[str, list[dict[str, Any]]]) -> None: + def _describe_match_field(match_field_data: MatchData) -> None: """Format and print result description text. Args: @@ -580,39 +701,39 @@ def _describe_match_field(match_field_data: dict[str, list[dict[str, Any]]]) -> """ for group, match_list in match_field_data.items(): for match_dict in match_list: - print(f"{indent}({group}) {match_dict[self.MATCH_TEXT]}") + print(f"{indent}({group}) {match_dict.match_text}") if position: - print(f"{indent}start: {match_dict[self.MATCH_START]}") - print(f"{indent}stop: {match_dict[self.MATCH_STOP]}") + print(f"{indent}start: {match_dict.start}") + print(f"{indent}stop: {match_dict.stop}") indent = " " * 4 match_data = self(text) - if match_data[self.MATCH_RESULT]: + if match_data.match_result: print("The MelusineRegex match result is : POSITIVE") else: print("The MelusineRegex match result is : NEGATIVE") if not any( [ - match_data[self.NEUTRAL_MATCH_FIELD], - match_data[self.NEGATIVE_MATCH_FIELD], - match_data[self.POSITIVE_MATCH_FIELD], + match_data.neutral, + match_data.negative, + match_data.positive, ] ): print("The input text did not match anything.") - if match_data[self.NEUTRAL_MATCH_FIELD]: + if match_data.neutral: print("The following text was ignored:") - _describe_match_field(match_data[self.NEUTRAL_MATCH_FIELD]) + _describe_match_field(match_data.neutral) - if match_data[self.NEGATIVE_MATCH_FIELD]: + if match_data.negative: print("The following text matched negatively:") - _describe_match_field(match_data[self.NEGATIVE_MATCH_FIELD]) + _describe_match_field(match_data.negative) - if match_data[self.POSITIVE_MATCH_FIELD]: + if match_data.positive: print("The following text matched positively:") - _describe_match_field(match_data[self.POSITIVE_MATCH_FIELD]) + _describe_match_field(match_data.positive) def apply_paired_matching(self, negative_match_data: MatchData, positive_match_data: MatchData) -> bool: """Check if negative match is effective in the case of paired matching. @@ -650,7 +771,7 @@ def pre_match_hook(self, text: str) -> str: """ return text - def post_match_hook(self, match_dict: dict[str, Any]) -> dict[str, Any]: + def post_match_hook(self, match_dict: MelusineRegexFullMatch) -> MelusineRegexFullMatch: """Hook to run after the Melusine regex match. Args: @@ -661,12 +782,10 @@ def post_match_hook(self, match_dict: dict[str, Any]) -> dict[str, Any]: """ # Paired matching - negative_match = self.apply_paired_matching( - match_dict[self.NEGATIVE_MATCH_FIELD], match_dict[self.POSITIVE_MATCH_FIELD] - ) - positive_match = bool(match_dict[self.POSITIVE_MATCH_FIELD]) + negative_match = self.apply_paired_matching(match_dict.negative, match_dict.positive) + positive_match = bool(match_dict.positive) - match_dict[self.MATCH_RESULT] = positive_match and not negative_match + match_dict.match_result = positive_match and not negative_match return match_dict @@ -674,11 +793,11 @@ def test(self) -> None: """Test the MelusineRegex on the match_list and no_match_list.""" for text in self.match_list: match = self(text) - assert match[self.MATCH_RESULT] is True, f"Expected match for text\n{text}\nObtained: {match}" + assert match.match_result is True, f"Expected match for text\n{text}\nObtained: {match}" for text in self.no_match_list: match = self(text) - assert match[self.MATCH_RESULT] is False, f"Expected no match for text:\n{text}\nObtained: {match}" + assert match.match_result is False, f"Expected no match for text:\n{text}\nObtained: {match}" def __repr__(self) -> str: """Repr for the class MelusineRegex.""" diff --git a/melusine/detectors.py b/melusine/detectors.py index dc929376..85bdfece 100644 --- a/melusine/detectors.py +++ b/melusine/detectors.py @@ -124,7 +124,7 @@ def detect(self, row: MelusineItem, debug_mode: bool = False) -> MelusineItem: text: str = row[self.THANKS_TEXT_COL] detection_data = self.thanks_regex(text) - detection_result = detection_data[self.thanks_regex.MATCH_RESULT] + detection_result = detection_data.match_result # Save debug data if debug_mode: @@ -249,7 +249,7 @@ def detect(self, row: MelusineItem, debug_mode: bool = False) -> MelusineItem: text: str = row[self.CONST_TEXT_COL_NAME] detection_data = self.vacation_reply_regex(text) - detection_result = detection_data[self.vacation_reply_regex.MATCH_RESULT] + detection_result = detection_data.match_result # Save debug data if debug_mode: @@ -374,7 +374,7 @@ def detect(self, row: MelusineItem, debug_mode: bool = False) -> MelusineItem: text: str = row[self.CONST_ANALYSED_TEXT_COL] detection_data = self.reply_regex(text) - detection_result = detection_data[MelusineRegex.MATCH_RESULT] + detection_result = detection_data.match_result # Save debug data if debug_mode: @@ -504,7 +504,7 @@ def detect(self, row: MelusineItem, debug_mode: bool = False) -> MelusineItem: meta: str = row[self.messages_column][0].meta detection_data = self.transfer_regex(text) - detection_result = detection_data[MelusineRegex.MATCH_RESULT] + detection_result = detection_data.match_result # Save debug data if debug_mode: @@ -630,7 +630,7 @@ def detect(self, row: MelusineItem, debug_mode: bool = False) -> MelusineItem: text: str = row[self.CONST_TEXT_COL_NAME] detection_data = self.regex(text) - detection_result = detection_data[self.regex.MATCH_RESULT] + detection_result = detection_data.match_result # Save debug data if debug_mode: diff --git a/tests/base/test_melusine_regex.py b/tests/base/test_melusine_regex.py index f056fd62..f86a209f 100644 --- a/tests/base/test_melusine_regex.py +++ b/tests/base/test_melusine_regex.py @@ -2,7 +2,7 @@ import pytest -from melusine.base import MelusineRegex +from melusine.base import MelusineRegex, MelusineRegexFullMatch, MelusineRegexUnitMatch class VirusRegex(MelusineRegex): @@ -58,29 +58,29 @@ def test_match_method(): regex = VirusRegex() match_data = regex("The computer virus in the ladybug software caused a bug in the corona virus dashboard") - assert match_data[MelusineRegex.MATCH_RESULT] is False - assert match_data[MelusineRegex.POSITIVE_MATCH_FIELD] == { - "DEFAULT": [{"match_text": "virus", "start": 13, "stop": 18}] + assert match_data.match_result is False + assert match_data.positive == { + "DEFAULT": [MelusineRegexUnitMatch(**{"match_text": "virus", "start": 13, "stop": 18})] } - assert match_data[MelusineRegex.NEUTRAL_MATCH_FIELD] == { - "NEUTRAL_INSECT": [{"match_text": "ladybug", "start": 26, "stop": 33}], - "NEUTRAL_MEDICAL_VIRUS": [{"match_text": "corona virus", "start": 63, "stop": 75}], + assert match_data.neutral == { + "NEUTRAL_INSECT": [MelusineRegexUnitMatch(**{"match_text": "ladybug", "start": 26, "stop": 33})], + "NEUTRAL_MEDICAL_VIRUS": [MelusineRegexUnitMatch(**{"match_text": "corona virus", "start": 63, "stop": 75})], } - assert match_data[MelusineRegex.NEGATIVE_MATCH_FIELD] == { - "NEGATIVE_BUG": [{"match_text": "bug", "start": 52, "stop": 55}] + assert match_data.negative == { + "NEGATIVE_BUG": [MelusineRegexUnitMatch(**{"match_text": "bug", "start": 52, "stop": 55})] } def test_direct_match_method(): regex = VirusRegex() - bool_match_result = regex.get_match_result("The computer virus") + bool_match_result = regex("The computer virus").match_result assert bool_match_result is True - bool_match_result = regex.get_match_result( + bool_match_result = regex( "The computer virus in the ladybug software caused a bug in the corona virus dashboard" - ) + ).match_result assert bool_match_result is False @@ -161,20 +161,20 @@ def pre_match_hook(self, text: str) -> str: def test_pre_match_hook(): reg = PreMatchHookVirusRegex() - bool_match_result = reg.get_match_result("I see a virrrrus !") + bool_match_result = reg("I see a virrrrus !").match_result assert bool_match_result is True class PostMatchHookVirusRegex(VirusRegex): - def post_match_hook(self, match_dict: Dict[str, Any]) -> Dict[str, Any]: + def post_match_hook(self, match_dict: MelusineRegexFullMatch) -> MelusineRegexFullMatch: """Test custom post processing of match data""" if ( - match_dict[self.MATCH_RESULT] is True - and "NEUTRAL_MEDICAL_VIRUS" in match_dict[self.NEUTRAL_MATCH_FIELD] - and "NEUTRAL_INSECT" in match_dict[self.NEUTRAL_MATCH_FIELD] + match_dict.match_result is True + and "NEUTRAL_MEDICAL_VIRUS" in match_dict.neutral + and "NEUTRAL_INSECT" in match_dict.neutral ): - match_dict[self.MATCH_RESULT] = False + match_dict.match_result = False return match_dict @@ -182,10 +182,10 @@ def post_match_hook(self, match_dict: Dict[str, Any]) -> Dict[str, Any]: def test_post_match_hook(): reg = PostMatchHookVirusRegex() - bool_match_result = reg.get_match_result("I see a virus, a corona virus and a ladybug") + bool_match_result = reg("I see a virus, a corona virus and a ladybug").match_result assert bool_match_result is False - bool_match_result = reg.get_match_result("I see a virus and a ladybug") + bool_match_result = reg("I see a virus and a ladybug").match_result assert bool_match_result is True @@ -229,3 +229,38 @@ def no_match_list(self) -> List[str]: def test_paired_matching_test(): regex = PairedMatchRegex() regex.test() + + +@pytest.mark.parametrize( + "attr_name,expected", + [ + ("MATCH_RESULT", "match_result"), + ("NEUTRAL_MATCH_FIELD", "neutral_match_data"), + ("POSITIVE_MATCH_FIELD", "positive_match_data"), + ("NEGATIVE_MATCH_FIELD", "negative_match_data"), + ("MATCH_START", "start"), + ("MATCH_STOP", "stop"), + ("MATCH_TEXT", "match_text"), + ], +) +def test_legacy_regex_attributes_warn_on_class_access(attr_name, expected): + with pytest.deprecated_call(match=attr_name): + assert getattr(MelusineRegex, attr_name) == expected + + +@pytest.mark.parametrize( + "attribute", + [ + "match_result", + "positive_match_data", + "neutral_match_data", + "negative_match_data", + ], +) +def test_legacy_dict_access(attribute): + regex = VirusRegex() + + match_data = regex("The computer virus in the ladybug software caused a bug in the corona virus dashboard") + + with pytest.deprecated_call(match="Dict like access is deprecated, please use direct attribute access instead"): + assert match_data[attribute] == getattr(match_data, attribute) diff --git a/tests/detectors/test_emergency_detector.py b/tests/detectors/test_emergency_detector.py index 0d4a7850..0b2bd09c 100644 --- a/tests/detectors/test_emergency_detector.py +++ b/tests/detectors/test_emergency_detector.py @@ -6,17 +6,22 @@ import pandas as pd import pytest +from melusine.base import MelusineRegexFullMatch, MelusineRegexUnitMatch from melusine.detectors import EmergencyDetector @pytest.fixture def emergency_detector_df(): - m0_debug_expected = { - "match_result": True, - "negative_match_data": {}, - "neutral_match_data": {}, - "positive_match_data": {"DEFAULT": [{"match_text": "urgent", "start": 10, "stop": 16}]}, - } + m0_debug_expected = MelusineRegexFullMatch( + **{ + "match_result": True, + "negative": {}, + "neutral": {}, + "positive": { + "DEFAULT": [MelusineRegexUnitMatch(**{"match_text": "urgent", "start": 10, "stop": 16})], + }, + } + ) df = pd.DataFrame( { diff --git a/tests/detectors/test_reply_detector.py b/tests/detectors/test_reply_detector.py index c4fe6159..6aa37af4 100644 --- a/tests/detectors/test_reply_detector.py +++ b/tests/detectors/test_reply_detector.py @@ -6,6 +6,7 @@ import pytest from pandas import DataFrame +from melusine.base import MelusineRegexFullMatch, MelusineRegexUnitMatch from melusine.detectors import ReplyDetector from melusine.pipeline import MelusinePipeline @@ -131,12 +132,16 @@ def test_transform(df_emails, expected_result): ), True, { - "ReplyRegex": { - "match_result": True, - "negative_match_data": {}, - "neutral_match_data": {}, - "positive_match_data": {"DEFAULT": [{"match_text": "re:", "start": 0, "stop": 3}]}, - }, + "ReplyRegex": MelusineRegexFullMatch( + **{ + "match_result": True, + "negative": {}, + "neutral": {}, + "positive": { + "DEFAULT": [MelusineRegexUnitMatch(**{"match_text": "re:", "start": 0, "stop": 3})] + }, + } + ), "reply_text": "re: suivi de dossier", }, ), diff --git a/tests/detectors/test_thanks_detector.py b/tests/detectors/test_thanks_detector.py index c17bbcd4..d4965bef 100644 --- a/tests/detectors/test_thanks_detector.py +++ b/tests/detectors/test_thanks_detector.py @@ -3,12 +3,10 @@ """ -from tempfile import TemporaryDirectory - import pandas as pd import pytest -from melusine.base import MissingFieldError +from melusine.base import MelusineRegexFullMatch, MelusineRegexUnitMatch, MissingFieldError from melusine.detectors import ThanksDetector from melusine.message import Message @@ -22,12 +20,14 @@ def thanks_detector_df(): ] m0_messages = [m0] m0_expected = True - m0_debug_expected = { - "match_result": True, - "negative_match_data": {}, - "neutral_match_data": {}, - "positive_match_data": {"DEFAULT": [{"match_text": "Merci", "start": 0, "stop": 5}]}, - } + m0_debug_expected = MelusineRegexFullMatch( + **{ + "match_result": True, + "negative": {}, + "neutral": {}, + "positive": {"DEFAULT": [MelusineRegexUnitMatch(**{"match_text": "Merci", "start": 0, "stop": 5})]}, + } + ) m1 = Message("") m1.tags = [ @@ -36,12 +36,16 @@ def thanks_detector_df(): ] m1_messages = [m1] m1_expected = False - m1_debug_expected = { - "match_result": False, - "negative_match_data": {"FORBIDDEN_WORDS": [{"match_text": "attend", "start": 9, "stop": 15}]}, - "neutral_match_data": {}, - "positive_match_data": {"DEFAULT": [{"match_text": "Merci", "start": 0, "stop": 5}]}, - } + m1_debug_expected = MelusineRegexFullMatch( + **{ + "match_result": False, + "negative": { + "FORBIDDEN_WORDS": [MelusineRegexUnitMatch(**{"match_text": "attend", "start": 9, "stop": 15})] + }, + "neutral": {}, + "positive": {"DEFAULT": [MelusineRegexUnitMatch(**{"match_text": "Merci", "start": 0, "stop": 5})]}, + } + ) df = pd.DataFrame( { diff --git a/tests/detectors/test_transfer_detector.py b/tests/detectors/test_transfer_detector.py index 9a86907f..ae50c519 100644 --- a/tests/detectors/test_transfer_detector.py +++ b/tests/detectors/test_transfer_detector.py @@ -6,6 +6,7 @@ import pytest from pandas import DataFrame +from melusine.base import MelusineRegexFullMatch, MelusineRegexUnitMatch from melusine.detectors import TransferDetector from melusine.message import Message from melusine.pipeline import MelusinePipeline @@ -205,12 +206,16 @@ def test_transform(df_emails, expected_result): { "reply_text": "tr: suivi de dossier", "messages[0].meta": "", - "TransferRegex": { - "match_result": True, - "negative_match_data": {}, - "neutral_match_data": {}, - "positive_match_data": {"DEFAULT": [{"match_text": "tr:", "start": 0, "stop": 3}]}, - }, + "TransferRegex": MelusineRegexFullMatch( + **{ + "match_result": True, + "negative": {}, + "neutral": {}, + "positive": { + "DEFAULT": [MelusineRegexUnitMatch(**{"match_text": "tr:", "start": 0, "stop": 3})] + }, + } + ), }, ), ], diff --git a/tests/detectors/test_vacation_reply_detector.py b/tests/detectors/test_vacation_reply_detector.py index d606ebeb..f913091f 100644 --- a/tests/detectors/test_vacation_reply_detector.py +++ b/tests/detectors/test_vacation_reply_detector.py @@ -6,6 +6,7 @@ import pytest from pandas import DataFrame +from melusine.base import MelusineRegexFullMatch, MelusineRegexUnitMatch from melusine.detectors import VacationReplyDetector from melusine.message import Message from melusine.pipeline import MelusinePipeline @@ -113,17 +114,25 @@ def test_transform(df, good_result): True, { "text": "Actuellement en conge je prendrai connaissance de votre message ulterieurement.", - "VacationReplyRegex": { - "match_result": True, - "negative_match_data": {}, - "neutral_match_data": {}, - "positive_match_data": { - "VAC_REP_HOLIDAYS": [{"match_text": "Actuellement " "en " "conge", "start": 0, "stop": 21}], - "VAC_REP_OUT_OF_OFFICE": [ - {"match_text": "je " "prendrai " "connaissance", "start": 22, "stop": 46} - ], - }, - }, + "VacationReplyRegex": MelusineRegexFullMatch( + **{ + "match_result": True, + "negative": {}, + "neutral": {}, + "positive": { + "VAC_REP_HOLIDAYS": [ + MelusineRegexUnitMatch( + **{"match_text": "Actuellement en conge", "start": 0, "stop": 21} + ) + ], + "VAC_REP_OUT_OF_OFFICE": [ + MelusineRegexUnitMatch( + **{"match_text": "je prendrai connaissance", "start": 22, "stop": 46} + ) + ], + }, + } + ), }, ), ],