Home | History | Annotate | Download | only in tests
      1 # encoding: utf-8
      2 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 # ==============================================================================
     16 """Categorical vocabulary tests."""
     17 
     18 from __future__ import absolute_import
     19 from __future__ import division
     20 from __future__ import print_function
     21 
     22 from tensorflow.contrib.learn.python.learn.preprocessing import categorical_vocabulary
     23 from tensorflow.python.platform import test
     24 
     25 
     26 class CategoricalVocabularyTest(test.TestCase):
     27   """Categorical vocabulary tests."""
     28 
     29   def testIntVocabulary(self):
     30     vocab = categorical_vocabulary.CategoricalVocabulary()
     31     self.assertEqual(vocab.get(1), 1)
     32     self.assertEqual(vocab.get(3), 2)
     33     self.assertEqual(vocab.get(2), 3)
     34     self.assertEqual(vocab.get(3), 2)
     35     # This vocab doesn't handle nan specially.
     36     self.assertEqual(vocab.get(float('nan')), 4)
     37     self.assertEqual(len(vocab), 5)
     38 
     39   def testWordVocabulary(self):
     40     vocab = categorical_vocabulary.CategoricalVocabulary()
     41     self.assertEqual(vocab.get('a'), 1)
     42     self.assertEqual(vocab.get('b'), 2)
     43     self.assertEqual(vocab.get('a'), 1)
     44     self.assertEqual(vocab.get('b'), 2)
     45 
     46   def testCountsTrim(self):
     47     vocab = categorical_vocabulary.CategoricalVocabulary()
     48     vocab.get('c')
     49     vocab.add('c', 5)
     50     vocab.get('a')
     51     vocab.add('a', 10)
     52     # not in vocab yet, skips.
     53     vocab.add('b', 5)
     54     vocab.add('d', 12)
     55     vocab.trim(7, 11)
     56     vocab.freeze()
     57     self.assertEqual(vocab.get('b'), 0)
     58     self.assertEqual(vocab.get('c'), 0)
     59     self.assertEqual(len(vocab), 2)
     60     self.assertEqual(vocab.get('a'), 1)
     61 
     62 
     63 if __name__ == '__main__':
     64   test.main()
     65