Exemple #1
0
    def _convertDocumentsToVector(self):
        self.vectors, self.masks, self.docIds = [], [], []
        dimensions = TwoWayMap()
        for docId, document in self.documents:
            for w in document.split(): 
                if not dimensions.contains(Clustering.PHRASE_TO_DIMENSION, w): dimensions.set(Clustering.PHRASE_TO_DIMENSION, w, len(dimensions))
        for docId, document in self.documents:
            vector = zeros(len(dimensions))
            for w in document.split(): vector[dimensions.get(Clustering.PHRASE_TO_DIMENSION, w)]+=1 
            self.vectors.append(vector)
            self.masks.append(ones(len(dimensions)))
            self.docIds.append(docId)
#        self.vectors = whiten(self.vectors)
        self.dimensions = dimensions
Exemple #2
0
    def _convertDocumentsToVector(self):
        self.vectors, self.masks, self.docIds = [], [], []
        dimensions = TwoWayMap()
        for docId, document in self.documents:
            for w in document.split():
                if not dimensions.contains(Clustering.PHRASE_TO_DIMENSION, w):
                    dimensions.set(Clustering.PHRASE_TO_DIMENSION, w,
                                   len(dimensions))
        for docId, document in self.documents:
            vector = zeros(len(dimensions))
            for w in document.split():
                vector[dimensions.get(Clustering.PHRASE_TO_DIMENSION, w)] += 1
            self.vectors.append(vector)
            self.masks.append(ones(len(dimensions)))
            self.docIds.append(docId)


#        self.vectors = whiten(self.vectors)
        self.dimensions = dimensions
Exemple #3
0
 def setUp(self):
     self.twoWayMap = TwoWayMap()
     self.assertRaises(TypeError, self.twoWayMap.set, (5, 1, 2))
     self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'A')
     self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'B', 'b')
Exemple #4
0
class TwoWayDictTests(unittest.TestCase):
    def setUp(self):
        self.twoWayMap = TwoWayMap()
        self.assertRaises(TypeError, self.twoWayMap.set, (5, 1, 2))
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'A')
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'B', 'b')
    def __isValidObject(self): return self.twoWayMap.getMap(TwoWayMap.MAP_REVERSE)==GeneralMethods.reverseDict(self.twoWayMap.getMap(TwoWayMap.MAP_FORWARD))
    def test_basicOperation(self):
        self.assertEqual('A', self.twoWayMap.get(TwoWayMap.MAP_FORWARD, 'a'))
        self.assertEqual('a', self.twoWayMap.get(TwoWayMap.MAP_REVERSE, 'A'))
        self.assertEqual('B', self.twoWayMap.get(TwoWayMap.MAP_FORWARD, 'b'))
        self.assertEqual('b', self.twoWayMap.get(TwoWayMap.MAP_REVERSE, 'B'))
        self.assertEqual({'b': 'B', 'a': 'A'}, self.twoWayMap.getMap(self.twoWayMap.MAP_FORWARD))
        self.assertEqual({'B': 'b', 'A': 'a'}, self.twoWayMap.getMap(self.twoWayMap.MAP_REVERSE))
        self.assertTrue(self.__isValidObject())
    def test_set(self):
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'c', 'A'), self.assertTrue(self.__isValidObject())
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'C', 'b'), self.assertTrue(self.__isValidObject())
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'D'), self.assertTrue(self.__isValidObject())
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'B', 'd'), self.assertTrue(self.__isValidObject())
    def test_delete(self):
        self.twoWayMap.remove(TwoWayMap.MAP_FORWARD, 'a')
        self.assertEqual({'b': 'B'}, self.twoWayMap.getMap(self.twoWayMap.MAP_FORWARD))
        self.assertEqual({'B': 'b'}, self.twoWayMap.getMap(self.twoWayMap.MAP_REVERSE))
        self.twoWayMap.remove(TwoWayMap.MAP_FORWARD, 'a')
        self.assertTrue(self.__isValidObject())
    def test_length(self):
        self.assertEqual(2, len(self.twoWayMap))
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'C', 'c')
        self.assertEqual(3, len(self.twoWayMap))
    def test_contains(self):
        self.assertTrue(self.twoWayMap.contains(TwoWayMap.MAP_FORWARD, 'a'))
        self.assertFalse(self.twoWayMap.contains(TwoWayMap.MAP_FORWARD, 'A'))
        self.assertTrue(self.twoWayMap.contains(TwoWayMap.MAP_REVERSE, 'B'))
    def test_resetKey(self):
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'C')
        self.assertTrue(self.__isValidObject())
 def setUp(self):
     self.twoWayMap = TwoWayMap()
     self.assertRaises(TypeError, self.twoWayMap.set, (5, 1, 2))
     self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'A')
     self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'B', 'b')
class TwoWayDictTests(unittest.TestCase):
    def setUp(self):
        self.twoWayMap = TwoWayMap()
        self.assertRaises(TypeError, self.twoWayMap.set, (5, 1, 2))
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'A')
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'B', 'b')

    def __isValidObject(self):
        return self.twoWayMap.getMap(
            TwoWayMap.MAP_REVERSE) == GeneralMethods.reverseDict(
                self.twoWayMap.getMap(TwoWayMap.MAP_FORWARD))

    def test_basicOperation(self):
        self.assertEqual('A', self.twoWayMap.get(TwoWayMap.MAP_FORWARD, 'a'))
        self.assertEqual('a', self.twoWayMap.get(TwoWayMap.MAP_REVERSE, 'A'))
        self.assertEqual('B', self.twoWayMap.get(TwoWayMap.MAP_FORWARD, 'b'))
        self.assertEqual('b', self.twoWayMap.get(TwoWayMap.MAP_REVERSE, 'B'))
        self.assertEqual({
            'b': 'B',
            'a': 'A'
        }, self.twoWayMap.getMap(self.twoWayMap.MAP_FORWARD))
        self.assertEqual({
            'B': 'b',
            'A': 'a'
        }, self.twoWayMap.getMap(self.twoWayMap.MAP_REVERSE))
        self.assertTrue(self.__isValidObject())

    def test_set(self):
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'c',
                           'A'), self.assertTrue(self.__isValidObject())
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'C',
                           'b'), self.assertTrue(self.__isValidObject())
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a',
                           'D'), self.assertTrue(self.__isValidObject())
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'B',
                           'd'), self.assertTrue(self.__isValidObject())

    def test_delete(self):
        self.twoWayMap.remove(TwoWayMap.MAP_FORWARD, 'a')
        self.assertEqual({'b': 'B'},
                         self.twoWayMap.getMap(self.twoWayMap.MAP_FORWARD))
        self.assertEqual({'B': 'b'},
                         self.twoWayMap.getMap(self.twoWayMap.MAP_REVERSE))
        self.twoWayMap.remove(TwoWayMap.MAP_FORWARD, 'a')
        self.assertTrue(self.__isValidObject())

    def test_length(self):
        self.assertEqual(2, len(self.twoWayMap))
        self.twoWayMap.set(TwoWayMap.MAP_REVERSE, 'C', 'c')
        self.assertEqual(3, len(self.twoWayMap))

    def test_contains(self):
        self.assertTrue(self.twoWayMap.contains(TwoWayMap.MAP_FORWARD, 'a'))
        self.assertFalse(self.twoWayMap.contains(TwoWayMap.MAP_FORWARD, 'A'))
        self.assertTrue(self.twoWayMap.contains(TwoWayMap.MAP_REVERSE, 'B'))

    def test_resetKey(self):
        self.twoWayMap.set(TwoWayMap.MAP_FORWARD, 'a', 'C')
        self.assertTrue(self.__isValidObject())