-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
99 lines (76 loc) · 3.72 KB
/
tests.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import unittest
import logging
import tomodapi as models
TEST_SENTENCE = 'In the time since the industrial revolution the climate has increasingly been affected by human ' \
'activities that are causing global warming and climate change.'
TEST_CORPUS = './data/test.txt'
TEST_LABELS = './data/test_labels.txt'
# logging.basicConfig(level=logging.DEBUG)
class MainTest(unittest.TestCase):
def test_train(self):
for model in models.__all__:
m = model()
res = m.train(data=TEST_CORPUS)
self.assertEqual(res, 'success', '[%s] Problems in training.' % model)
m.save()
def test_predict(self):
for model in models.__all__:
m = model()
res = m.predict(TEST_SENTENCE, topn=3)
if 'message' in res:
self.assertEqual(res['message'], 'not implemented for this model',
'[%s] Unexpected output for the prediction')
else:
self.assertIsInstance(res, list, '[%s] Predict output should be a list.' % model)
self.assertEqual(len(res), min(3, len(m.topics)), '[%s] Predict output should match topn.' % model)
self.assertIsInstance(res[0], tuple,
'[%s] Predictions should be represented as tuple.' % model)
def test_topics(self):
for model in models.__all__:
m = model()
res = m.topics
print(res)
self.assertIsInstance(res, list, '[%s] Topics output should be a list.' % model)
self.assertIn('words', res[0], '[%s] Topics output should be like {words: [], weights: [] }.' % model)
def test_given_topic(self):
for model in models.__all__:
m = model()
res = m.topic(0)
print(res)
self.assertIn('words', res, '[%s] Topics output should be like {words: [], weights: [] }.' % model)
def test_coherence(self):
for model in models.__all__:
m = model()
m.load()
res = m.coherence(datapath=TEST_CORPUS)
print(res)
self.assertIsInstance(res, dict, '[%s] Coherence output should be a dict.' % model)
self.assertIn('c_v', res,
'[%s] Coherence output should be like {c_v_per_topics: [], c_v: 0.01, c_v_std: 0.01 }.' % model)
self.assertIsInstance(res['c_v'], float, '[%s] Coherence output should be a floating point.' % model)
def test_corpus_predictions(self):
for model in models.__all__:
m = model()
m.load()
print(model)
res = m.get_corpus_predictions()
# print(res)
self.assertIsInstance(res, list, '[%s] Corpus prediction output should be a list of lists.' % model)
self.assertIsInstance(res[0], list, '[%s] Corpus prediction output should be a list of lists.' % model)
self.assertIsInstance(res[0][0], tuple,
'[%s] Corpus prediction topics should be represented as tuple.' % model)
def test_evaluate(self):
with open(TEST_LABELS, 'r') as f:
labels = [x.strip() for x in f.readlines()]
for model in models.__all__:
m = model()
m.load()
print(model)
res = m.get_corpus_predictions(topn=1)
v = m.evaluate(res, labels, metric='nmi')
v2 = m.evaluate(res, labels, metric='purity')
print(v)
self.assertIsInstance(v, float, '[%s] Evaluate NMI should return a float.' % model)
self.assertIsInstance(v2, float, '[%s] Evaluate Purity should return a float.' % model)
if __name__ == '__main__':
unittest.main()