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-03 01:10:00 +00:00
|
|
|
def train(self, training_set: t.Iterable[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.
|
|
|
|
"""
|
|
|
|
|
2023-02-03 16:50:40 +00:00
|
|
|
|
2023-02-02 01:56:37 +00:00
|
|
|
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",
|
|
|
|
)
|