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
Branches
3 merge requests!10Anonimizer v2,!8Add txt output format,!7Better coverage
from dataclasses import dataclass
@dataclass
class Annotation:
def __hash__(self) -> int:
return (type(self), *(self.__dict__.values())).__hash__()
......
from dataclasses import dataclass
from typing import Optional
@dataclass
class Detection:
TYPE_NAME = "detection"
......
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
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
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.
......
......@@ -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)
......
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
......@@ -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
......
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]]:
......
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