1
Fork 0
mirror of https://github.com/Steffo99/unimore-bda-6.git synced 2024-11-23 00:14:19 +00:00
bda-6-steffo/unimore_bda_6/analysis/base.py

47 lines
929 B
Python
Raw Normal View History

2023-02-02 01:56:37 +00:00
import abc
2023-02-02 16:24:11 +00:00
import typing as t
Input = t.TypeVar("Input")
Category = t.TypeVar("Category")
2023-02-02 01:56:37 +00:00
class BaseSA(metaclass=abc.ABCMeta):
"""
Abstract base class for sentiment analyzers implemented in this project.
"""
@abc.abstractmethod
2023-02-02 16:24:11 +00:00
def train(self, training_set: list[tuple[Input, Category]]) -> None:
2023-02-02 01:56:37 +00:00
"""
Train the analyzer with the given training set.
"""
raise NotImplementedError()
@abc.abstractmethod
2023-02-02 16:24:11 +00:00
def use(self, text: Input) -> Category:
2023-02-02 01:56:37 +00:00
"""
Use the sentiment analyzer.
"""
raise NotImplementedError()
class AlreadyTrainedError(Exception):
"""
This model has already been trained and cannot be trained again.
"""
class NotTrainedError(Exception):
"""
This model has not been trained yet.
"""
__all__ = (
2023-02-02 16:24:11 +00:00
"Input",
"Category",
2023-02-02 01:56:37 +00:00
"BaseSA",
"AlreadyTrainedError",
"NotTrainedError",
)