本文整理汇总了Python中corpus.Corpus.add_instance方法的典型用法代码示例。如果您正苦于以下问题:Python Corpus.add_instance方法的具体用法?Python Corpus.add_instance怎么用?Python Corpus.add_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类corpus.Corpus
的用法示例。
在下文中一共展示了Corpus.add_instance方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_concatenate_corpus_no_extra_info
# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_instance [as 别名]
def test_concatenate_corpus_no_extra_info(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.co.concetenate_corpus(new_corpus)
self.assertEqual(len(self.co), 4)
示例2: ActivePipeline
# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_instance [as 别名]
#.........这里部分代码省略.........
while ((not max_iterations or it < max_iterations) and
len(self.unlabeled_corpus)):
it += 1
new_index = self.get_next_instance()
try:
new_instance = self.unlabeled_corpus.instances[new_index]
except IndexError:
import ipdb; ipdb.set_trace()
representation = self.unlabeled_corpus.representations[new_index]
if (self.emulate and
self.unlabeled_corpus.primary_targets[new_index]):
prediction = self.unlabeled_corpus.primary_targets[new_index]
message = "Emulation: Adding instance {}, {}".format(
representation, prediction
)
print colored(message, "red")
if (not self.emulate or
not self.unlabeled_corpus.primary_targets[new_index]):
classes = self._most_probable_classes(new_instance)
prediction = get_labeled_instance(representation, classes)
if prediction == 'stop':
break
if prediction == 'train':
self._train()
self._expectation_maximization()
continue
# if prediction == 'other':
# self.unlabeled_corpus.pop_instance(new_index)
# continue
self.new_instances += 1
result += 1
instance, targets, r = self.unlabeled_corpus.pop_instance(new_index)
self.user_corpus.add_instance(
instance, [prediction] + targets, r
)
return result
def feature_bootstrap(self, get_class, get_labeled_features,
max_iterations=None):
"""Presents a class and possible features until the prediction is stop.
Args:
get_class: A function that receives a list of classes and returns
one of them. Can return None in case of error.
get_labeled_features: A function that receives a class and a list
of features. It must return a list of features associated with the
class. Can return None in case of error.
max_iterations: Optional. An interger. The cicle will execute at
most max_iterations times if the user does not enter stop before.
Returns:
The number of features the user has labeled.
"""
result = 0
while not max_iterations or result < max_iterations:
class_name = get_class(self.get_class_options())
if not class_name:
continue
if class_name == 'stop':
break
if class_name == 'train':
self._train()
self._expectation_maximization()
continue
示例3: TestCorpus
# 需要导入模块: from corpus import Corpus [as 别名]
# 或者: from corpus.Corpus import add_instance [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'))
#.........这里部分代码省略.........