1 | import os |
---|
2 | import unittest |
---|
3 | import numpy as np |
---|
4 | |
---|
5 | from model.predictor import * |
---|
6 | from common.constants import KEYWORDS_FILE |
---|
7 | |
---|
8 | class TestPredictionMethods(unittest.TestCase): |
---|
9 | |
---|
10 | def test_fit(self): |
---|
11 | predictor = HelpPagePredictor() |
---|
12 | |
---|
13 | self.assertRaises(AssertionError, |
---|
14 | predictor.fit, |
---|
15 | None, |
---|
16 | np.array([])) |
---|
17 | |
---|
18 | self.assertRaises(AssertionError, |
---|
19 | predictor.fit, |
---|
20 | np.array([]), |
---|
21 | None) |
---|
22 | predictor.fit(np.array([]),np.array([])) |
---|
23 | |
---|
24 | def test_predict(self): |
---|
25 | predictor = HelpPagePredictor() |
---|
26 | vector1 = normalise_vector(np.array([1, 4, 10])) |
---|
27 | vector2 = normalise_vector(np.array([2, 3, 1])) |
---|
28 | vector3 = normalise_vector(np.array([3, 9, 3])) |
---|
29 | |
---|
30 | vectors = np.array([vector1, vector2, vector3]) |
---|
31 | files = np.array(["file1", "file2", "file3"]) |
---|
32 | |
---|
33 | testvec = normalise_vector(np.array([1, 1, 1])) |
---|
34 | |
---|
35 | print("distance to 1") |
---|
36 | print(vector_distance(testvec, vector1)) |
---|
37 | print() |
---|
38 | print("distance to 2") |
---|
39 | print(vector_distance(testvec, vector2)) |
---|
40 | print() |
---|
41 | print("distance to 3") |
---|
42 | print(vector_distance(testvec, vector3)) |
---|
43 | print() |
---|
44 | |
---|
45 | predictor.fit(vectors, files) |
---|
46 | prediction = predictor.predict(np.array([testvec])) |
---|
47 | print("Prediction:") |
---|
48 | print(prediction) |
---|
49 | self.assertEqual(prediction[0], "file2") |
---|
50 | |
---|
51 | |
---|
52 | if __name__ == '__main__': |
---|
53 | unittest.main() |
---|