Skip to content
Snippets Groups Projects
Commit 4f663e3c authored by Michał Pogoda's avatar Michał Pogoda
Browse files

Make interfaces use abstract class api

parent 046c75ef
3 merge requests!10Anonimizer v2,!8Add txt output format,!7Better coverage
from dataclasses import dataclass
@dataclass
class Annotation: class Annotation:
def __hash__(self) -> int: def __hash__(self) -> int:
return (type(self), *(self.__dict__.values())).__hash__() return (type(self), *(self.__dict__.values())).__hash__()
......
from dataclasses import dataclass
from typing import Optional from typing import Optional
@dataclass
class Detection: class Detection:
TYPE_NAME = "detection" TYPE_NAME = "detection"
......
from typing import List, Dict, Any, Tuple from typing import List, Dict, Any, Tuple
from src.detections import Detection from src.detections import Detection
from abc import ABC, abstractmethod
class Detector(ABC):
class Detector: @abstractmethod
def detect( def detect(
self, text: str, annotations: Dict[str, List[Tuple[int, int, Any]]] self, text: str, annotations: Dict[str, List[Tuple[int, int, Any]]]
) -> List[Tuple[int, int, Detection]]: ) -> 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
from src.detections import Detection from src.detections import Detection
from typing import Optional, List, Type from typing import Optional, List, Type
from abc import ABC, abstractmethod
class MorphosyntacticDictionary(ABC):
class MorphosyntacticDictionary: @abstractmethod
def get_supported_detection_classes(self) -> List[Type[Detection]]: 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]: def get_random_replacement(self, original_entry: Detection) -> Optional[str]:
""" """
Returns a random replacement for the original entry Returns a random replacement for the original entry
""" """
raise NotImplementedError() pass
from typing import Dict, List, Tuple, Any 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]]]: def parse(self, content: str) -> Tuple[str, List[Tuple[int, int, Any]]]:
"""Parse input string into text and annotations. """Parse input string into text and annotations.
......
...@@ -19,8 +19,8 @@ class DefaultPipeline(Pipeline): ...@@ -19,8 +19,8 @@ class DefaultPipeline(Pipeline):
self._suppressor = suppressor self._suppressor = suppressor
self._replacers = replacers self._replacers = replacers
def run(self, input) -> str: def run(self, input_path) -> str:
with open(input, "r") as f: with open(input_path, "r") as f:
content = f.read() content = f.read()
parsed_input = self._input_parser.parse(content) parsed_input = self._input_parser.parse(content)
......
class Pipeline: from abc import ABC, abstractmethod
def run(self, input) -> str:
raise NotImplementedError 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
...@@ -20,9 +20,9 @@ class SequentialJSONLPipeline(Pipeline): ...@@ -20,9 +20,9 @@ class SequentialJSONLPipeline(Pipeline):
self._suppressor = suppressor self._suppressor = suppressor
self._replacers = replacers self._replacers = replacers
def run(self, input) -> str: def run(self, input_path) -> str:
result = [] result = []
with open(input, "r") as f: with open(input_path, "r") as f:
for line in f.readlines(): for line in f.readlines():
if line.strip() == "": if line.strip() == "":
continue continue
......
from typing import List, Tuple, Any from typing import List, Tuple, Any
from abc import ABC, abstractmethod
class Suppressor: class Suppressor(ABC):
@abstractmethod
def suppress( def suppress(
self, annotations: List[Tuple[int, int, Any]] self, annotations: List[Tuple[int, int, Any]]
) -> List[Tuple[int, int, Any]]: ) -> List[Tuple[int, int, Any]]:
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment