-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseClassifier.py
63 lines (52 loc) · 2 KB
/
BaseClassifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import abc
import joblib
import os
from SmartAnno.utils.NoteBookLogger import logMsg
NotTrained = 0
InTraining = 1
ReadyTrained = 2
class BaseClassifier:
# indicate the status of classifier
status = NotTrained
instance = None
# add optional paramters with default values here (will be overwritten by ___init__'s **kwargs)
# These parameters will be shown in GUI ask for users' configuration
def __init__(self, task_name='default_task', pipeline=None, params=None, model_file=None, **kwargs):
self.task_name = task_name
for name, value in kwargs.items():
setattr(self, name, value)
if model_file is None:
model_file = 'models/saved/' + type(self).__name__ + '_' + task_name
self.model_file = model_file
self.model = None
if os.path.isfile(self.model_file):
self.model = self.loadModel()
BaseClassifier.status = ReadyTrained
else:
self.model = self.init_model()
BaseClassifier.status = NotTrained
# automatically set customized parameters to self object
BaseClassifier.instance = self
pass
@abc.abstractmethod
def init_model(self):
"""separate the definition, because at most of the time, you would want to automatically load previously trained
model instead. """
return None
@abc.abstractmethod
def classify(self, txt):
return 'Irrelevant'
@abc.abstractmethod
def train(self, x, y):
logMsg('error, abstract method called')
# [] to return Documents, dict() to return grouping information
pass
def saveModel(self):
"""will be automatically saved when user click complete"""
joblib.dump(self.model, self.model_file)
pass
def loadModel(self):
"""will be automatically load when initiate the classifier if self.model_file exists."""
model = joblib.load(self.model_file)
BaseClassifier.status = ReadyTrained
return model