source: git/machine_learning/ml_python/tests/common/test_keyword_vectors.py @ 7fb0101

spielwiese
Last change on this file since 7fb0101 was 7fb0101, checked in by Murray Heymann <heymann.murray@…>, 5 years ago
Begin writing c functions to interact with python3
  • Property mode set to 100644
File size: 4.7 KB
Line 
1import sys
2import unittest
3import numpy as np
4
5from common.keyword_vector import *
6
7class TestKeywordMethods(unittest.TestCase):
8
9    def test_read_dictionary(self):
10        """
11        Create test for read_dictionary function
12        """
13        print("Testing read_dictionary function:")
14
15        print("Non-existant file")
16        if sys.version_info[0] == 3:
17            self.assertRaises(FileNotFoundError,
18                              read_dictionary,
19                              "asdfasdf")
20        else:
21            self.assertRaises(IOError,
22                              read_dictionary,
23                              "asdfasdf")
24
25
26        print("Reading default file")
27        correct = True
28        read_dictionary()
29        print()
30
31    def test_count_occurances(self):
32        """
33        Create test for count_occurances function
34        """
35        print("Testing count_occurances function:")
36
37        dic = read_dictionary()
38        print("Test non-existant file")
39        if sys.version_info[0] == 3:
40            self.assertRaises(FileNotFoundError,
41                              count_occurances,
42                              "asdfasdf",
43                              dic)
44        else:
45            self.assertRaises(IOError,
46                              count_occurances,
47                              "asdfasdf",
48                              dic)
49
50        print("Count occurances with None dictionary:")
51        self.assertRaises(AssertionError, count_occurances,
52                          "../Singular/table.h",
53                          None)
54
55        print("Count occurances with empty dictionary:")
56        self.assertRaises(AssertionError, count_occurances,
57                          "../Singular/table.h",
58                          np.array([]))
59
60        print("vector of ../Singular/table.h")
61        vec = count_occurances("../Singular/table.h", dic)
62        print(vec)
63        print("Don't normalise")
64        vec = count_occurances("../Singular/table.h", dic, False)
65        self.assertGreater(np.linalg.norm(vec), 1)
66        print()
67       
68
69    def test_create_vector_dictionary(self):
70        """
71        Create test for create_vector_dictionary function
72        """
73        print("Testing create_vector_dictionary function:")
74
75        dictionary = read_dictionary()
76
77        print("Create Vector Dictionary with None as dictionary:")
78        self.assertRaises(AssertionError, create_vector_dictionary, None)
79
80        print("Create Vector Dictionary with empty dictionary:")
81        self.assertRaises(AssertionError,
82                          create_vector_dictionary,
83                          np.array([]))
84        vec_dic = create_vector_dictionary(dictionary)
85
86    def test_vector_distance(self):
87        """
88        Create test for vector_distance function
89        """
90        print("Testing vector_distance function:")
91
92        vector1 = np.array([3, 4])
93        vector1 = normalise_vector(vector1)
94        vector2 = np.array([4, 3])
95        vector2 = normalise_vector(vector2)
96
97        print("Distance of vectors of different dimensions:")
98        self.assertRaises(AssertionError,
99                          vector_distance,
100                          np.array([1, 2, 3]),
101                          vector1)
102
103        print("Distance same vector: " + str(vector_distance(vector1, vector1)))
104        self.assertEqual(vector_distance(vector1, vector1), 0)
105
106        print("Distance different vector: " + str(vector_distance(vector1, vector2)))
107        self.assertGreater(vector_distance(vector1, vector2), 0)
108        print()
109
110    def test_normalise_vector(self):
111        """
112        Create test for normalise_vector function
113        """
114        print("Testing normalise_vector function:")
115        testvector = np.array([3, 4])
116        testvector = normalise_vector(testvector)
117        self.assertEqual(np.linalg.norm(testvector), 1)
118
119        print("Attempt to normalise the zero vector")
120        vec = normalise_vector(np.array([0, 0, 0, 0, 0]))
121        self.assertEqual(np.linalg.norm(vec), 0)
122
123        print("Attempt to normalise list")
124        vec = normalise_vector([3, 4, 0, 0, 0])
125        self.assertEqual(np.linalg.norm(vec), 1)
126
127        print("Attempt to normalise empty vector")
128        vec = normalise_vector(np.array([]))
129        self.assertEqual(np.linalg.norm(vec), 0)
130
131        print("Attempt to normalise None")
132        vec = normalise_vector(None)
133        self.assertEqual(np.linalg.norm(vec), 0)
134        print()
135
136
137    def test_copy_dictionary(self):
138        dic = np.array(["hello", "bye"])
139        dic_copy = copy_dictionary(dic)
140        self.assertTrue((dic == dic_copy).all())
141
142
143    def test_copy_vector(self):
144        vec = np.array([1, 2, 3])
145        vec_copy = copy_vector(vec)
146        self.assertTrue((vec == vec_copy).all())
147
148
149if __name__ == '__main__':
150    unittest.main()
Note: See TracBrowser for help on using the repository browser.