From 4f663e3cb06d974cf7dec0ce2297543b2c363e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pogoda?= <mipo57@e-science.pl> Date: Tue, 21 Feb 2023 11:35:29 +0100 Subject: [PATCH] Make interfaces use abstract class api --- src/annotations/annotations.py | 4 ---- src/detections/detection.py | 2 -- src/detectors/interface.py | 16 +++++++++++++--- src/dictionaries/morphosyntactic/interface.py | 11 ++++++----- src/input_parsers/interface.py | 4 +++- src/pipeline/default.py | 4 ++-- src/pipeline/interface.py | 17 ++++++++++++++--- src/pipeline/sequential_jsonl.py | 4 ++-- src/suppressors/interface.py | 4 +++- 9 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/annotations/annotations.py b/src/annotations/annotations.py index c72270c..4ba6feb 100644 --- a/src/annotations/annotations.py +++ b/src/annotations/annotations.py @@ -1,7 +1,3 @@ -from dataclasses import dataclass - - -@dataclass class Annotation: def __hash__(self) -> int: return (type(self), *(self.__dict__.values())).__hash__() diff --git a/src/detections/detection.py b/src/detections/detection.py index 149eaf8..fad8a6d 100644 --- a/src/detections/detection.py +++ b/src/detections/detection.py @@ -1,8 +1,6 @@ -from dataclasses import dataclass from typing import Optional -@dataclass class Detection: TYPE_NAME = "detection" diff --git a/src/detectors/interface.py b/src/detectors/interface.py index 325d6d6..fe8981e 100644 --- a/src/detectors/interface.py +++ b/src/detectors/interface.py @@ -1,9 +1,19 @@ from typing import List, Dict, Any, Tuple from src.detections import Detection +from abc import ABC, abstractmethod - -class Detector: +class Detector(ABC): + @abstractmethod def detect( self, text: str, annotations: Dict[str, List[Tuple[int, int, Any]]] ) -> List[Tuple[int, int, Detection]]: - raise NotImplementedError + """Detects entities in text + + Args: + text (str): Text to be processed. + annotations (Dict[str, List[Tuple[int, int, Any]]]): Annotations. + + Returns: + List[Tuple[int, int, Detection]]: List of detections. + """ + pass \ No newline at end of file diff --git a/src/dictionaries/morphosyntactic/interface.py b/src/dictionaries/morphosyntactic/interface.py index 2718189..13d36e9 100644 --- a/src/dictionaries/morphosyntactic/interface.py +++ b/src/dictionaries/morphosyntactic/interface.py @@ -1,16 +1,17 @@ from src.detections import Detection from typing import Optional, List, Type +from abc import ABC, abstractmethod - -class MorphosyntacticDictionary: +class MorphosyntacticDictionary(ABC): + @abstractmethod def get_supported_detection_classes(self) -> List[Type[Detection]]: """ - Returns a list of supported detection classes + Returns a list of supported detection classess """ - raise NotImplementedError() + pass def get_random_replacement(self, original_entry: Detection) -> Optional[str]: """ Returns a random replacement for the original entry """ - raise NotImplementedError() + pass diff --git a/src/input_parsers/interface.py b/src/input_parsers/interface.py index 192fa2f..6309f91 100644 --- a/src/input_parsers/interface.py +++ b/src/input_parsers/interface.py @@ -1,7 +1,9 @@ from typing import Dict, List, Tuple, Any +from abc import ABC, abstractmethod -class InputParser: +class InputParser(ABC): + @abstractmethod def parse(self, content: str) -> Tuple[str, List[Tuple[int, int, Any]]]: """Parse input string into text and annotations. diff --git a/src/pipeline/default.py b/src/pipeline/default.py index 8850a28..cdf2db3 100644 --- a/src/pipeline/default.py +++ b/src/pipeline/default.py @@ -19,8 +19,8 @@ class DefaultPipeline(Pipeline): self._suppressor = suppressor self._replacers = replacers - def run(self, input) -> str: - with open(input, "r") as f: + def run(self, input_path) -> str: + with open(input_path, "r") as f: content = f.read() parsed_input = self._input_parser.parse(content) diff --git a/src/pipeline/interface.py b/src/pipeline/interface.py index ed87fd6..3574d84 100644 --- a/src/pipeline/interface.py +++ b/src/pipeline/interface.py @@ -1,3 +1,14 @@ -class Pipeline: - def run(self, input) -> str: - raise NotImplementedError +from abc import ABC, abstractmethod + +class Pipeline(ABC): + @abstractmethod + def run(self, input_path) -> str: + """Run the whole anonymization pipeline on the input file and output the result. + + Args: + input_path (_type_): Path to the input file. + + Returns: + str: Anonymized text. + """ + pass diff --git a/src/pipeline/sequential_jsonl.py b/src/pipeline/sequential_jsonl.py index 2bc796a..5f922ec 100644 --- a/src/pipeline/sequential_jsonl.py +++ b/src/pipeline/sequential_jsonl.py @@ -20,9 +20,9 @@ class SequentialJSONLPipeline(Pipeline): self._suppressor = suppressor self._replacers = replacers - def run(self, input) -> str: + def run(self, input_path) -> str: result = [] - with open(input, "r") as f: + with open(input_path, "r") as f: for line in f.readlines(): if line.strip() == "": continue diff --git a/src/suppressors/interface.py b/src/suppressors/interface.py index 565c6ac..f6a3420 100644 --- a/src/suppressors/interface.py +++ b/src/suppressors/interface.py @@ -1,7 +1,9 @@ from typing import List, Tuple, Any +from abc import ABC, abstractmethod -class Suppressor: +class Suppressor(ABC): + @abstractmethod def suppress( self, annotations: List[Tuple[int, int, Any]] ) -> List[Tuple[int, int, Any]]: -- GitLab