当前位置: 首页>>代码示例>>Python>>正文


Python Corpus.check_consistency方法代码示例

本文整理汇总了Python中corpus.Corpus.check_consistency方法的典型用法代码示例。如果您正苦于以下问题:Python Corpus.check_consistency方法的具体用法?Python Corpus.check_consistency怎么用?Python Corpus.check_consistency使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在corpus.Corpus的用法示例。


在下文中一共展示了Corpus.check_consistency方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestCorpus

# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import check_consistency [as 别名]
class TestCorpus(unittest.TestCase):

    def setUp(self):
        self.co = Corpus()
        self.co.instances = csr_matrix([[1, 2, 3], [4, 5, 6]])
        self.co.full_targets = [[1], [2,3]]
        self.co.representations = ['representation1', 'representation2']
        self.co.calculate_primary_targets()
        self.co.add_extra_info('extra_info1')

    def tearDown(self):
        self.assertTrue(self.co.check_consistency())

    def test_load_and_save(self):
        """Load and save functions must be inverses."""
        filename = 'testing_file'
        self.co.save_to_file(filename)
        new_co = Corpus()
        new_co.load_from_file(filename)
        self.assertTrue(_eq_crs_matrix(new_co.instances, self.co.instances))
        for index in range(len(self.co)):
            self.assertEqual(self.co.full_targets[index],
                             new_co.full_targets[index])
            self.assertEqual(self.co.representations[index],
                             new_co.representations[index])
        self.assertIsNotNone(new_co.primary_targets)

    def test_add_instance(self):
        self.co.add_instance([2, 3, 4], [2], 'representation3')
        self.assertEqual(len(self.co), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([2, 3, 4]),
                                       self.co.instances[-1]))

    def test_add_extra_info(self):
        self.assertEqual(len(self.co.extra_info), 1)
        self.assertIn('extra_info1', self.co.extra_info)

    def test_add_extra_info_twice(self):
        self.co.add_extra_info('extra_info1', values=[1, 1])
        self.assertEqual(len(self.co.extra_info), 1)
        self.assertIn('extra_info1', self.co.extra_info)
        self.assertEqual(self.co.extra_info['extra_info1'], [1, 1])

    def test_add_extra_info_second(self):
        self.co.add_extra_info('extra_info2', values=[1, 1])
        self.assertEqual(len(self.co.extra_info), 2)
        self.assertIn('extra_info2', self.co.extra_info)
        self.assertEqual(self.co.extra_info['extra_info2'], [1, 1])

    def test_pop_first_instance(self):
        result = self.co.pop_instance(0)
        # check result
        self.assertEqual(len(result), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([1, 2, 3]), result[0]))
        self.assertEqual(result[1], [1])
        self.assertEqual(result[2], 'representation1')
        #check corpus
        self.assertEqual(len(self.co), 1)
        self.assertTrue(_eq_crs_matrix(csr_matrix([4, 5, 6]),
                                       self.co.instances[0]))
        self.assertEqual(self.co.full_targets[0], [2, 3])
        self.assertEqual(self.co.representations[0], 'representation2')

    def test_pop_last_instance(self):
        result = self.co.pop_instance(1)
        # check result
        self.assertEqual(len(result), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([4, 5, 6]), result[0]))
        self.assertEqual(result[1], [2, 3])
        self.assertEqual(result[2], 'representation2')
        #check corpus
        self.assertEqual(len(self.co), 1)
        self.assertTrue(_eq_crs_matrix(csr_matrix([1, 2, 3]),
                                       self.co.instances[0]))
        self.assertEqual(self.co.full_targets[0], [1])
        self.assertEqual(self.co.representations[0], 'representation1')

    def test_pop_middle_instace(self):
        self.co.add_instance([2, 3, 4], [2], 'representation3')
        result = self.co.pop_instance(1)

        self.assertEqual(len(self.co), 2)
        self.assertEqual(len(result), 3)
        self.assertTrue(_eq_crs_matrix(csr_matrix([4, 5, 6]), result[0]))
        self.assertEqual(result[1], [2, 3])
        self.assertEqual(result[2], 'representation2')

    def test_pop_last_instance(self):
        self.co.pop_instance(1)
        self.co.pop_instance(0)

        self.assertEqual(len(self.co), 0)

    def test_concatenate_corpus(self):
        new_corpus = Corpus()
        new_corpus.add_instance([2, 3, 4], [2], 'representation3')
        new_corpus.add_instance([10, 4, 4], [1, 1, 2], 'representation3')
        new_corpus.calculate_primary_targets()
        self.assertTrue(new_corpus.add_extra_info('extra_info1'))

#.........这里部分代码省略.........
开发者ID:lucianosilvi,项目名称:mit0110_tesis,代码行数:103,代码来源:test_corpus.py


注:本文中的corpus.Corpus.check_consistency方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。