1
1
2
+ from abc import ABC , abstractmethod
3
+
2
4
import numpy as np
3
5
4
6
from shorttext .utils import textpreprocessing as textpreprocess , gensim_corpora as gc , classification_exceptions as e
5
7
from shorttext .utils .textpreprocessing import tokenize
6
8
7
9
# abstract class
8
- class LatentTopicModeler :
10
+ class LatentTopicModeler ( ABC ) :
9
11
"""
10
12
Abstract class for various topic modeler.
11
13
"""
@@ -33,7 +35,7 @@ def generate_corpus(self, classdict):
33
35
"""
34
36
self .dictionary , self .corpus , self .classlabels = gc .generate_gensim_corpora (classdict ,
35
37
preprocess_and_tokenize = lambda sent : tokenize (self .preprocessor (sent )))
36
-
38
+ @ abstractmethod
37
39
def train (self , classdict , nb_topics , * args , ** kwargs ):
38
40
""" Train the modeler.
39
41
@@ -79,6 +81,7 @@ def retrieve_bow_vector(self, shorttext, normalize=True):
79
81
vec /= np .linalg .norm (vec )
80
82
return vec
81
83
84
+ @abstractmethod
82
85
def retrieve_topicvec (self , shorttext ):
83
86
""" Calculate the topic vector representation of the short text.
84
87
@@ -92,6 +95,7 @@ def retrieve_topicvec(self, shorttext):
92
95
"""
93
96
raise e .NotImplementedException ()
94
97
98
+ @abstractmethod
95
99
def get_batch_cos_similarities (self , shorttext ):
96
100
""" Calculate the cosine similarities of the given short text and all the class labels.
97
101
@@ -113,6 +117,7 @@ def __contains__(self, shorttext):
113
117
raise e .ModelNotTrainedException ()
114
118
return True
115
119
120
+ @abstractmethod
116
121
def loadmodel (self , nameprefix ):
117
122
""" Load the model from files.
118
123
@@ -125,6 +130,7 @@ def loadmodel(self, nameprefix):
125
130
"""
126
131
raise e .NotImplementedException ()
127
132
133
+ @abstractmethod
128
134
def savemodel (self , nameprefix ):
129
135
""" Save the model to files.
130
136
0 commit comments