Algorithm-AM

 view release on metacpan or  search on metacpan

t/07-Batch_hooks.t  view on Meta::CPAN

		'test class');
	if($test_item->class eq 'e'){
		like(
			$test_item->comment,
			qr/second test item$/,
			'test comment'
		);
		is_deeply($test_item->features, [3,1,3], 'test item features')
			or note explain $test_item->features;
	}else{
		like(
			$test_item->comment,
			qr/test item comment$/,
			'test comment'
		);
		is_deeply($test_item->features, [3,1,2], 'test item features')
			or note explain $test_item->features;
	}
	return;
}

# Test variables available for each iteration
sub test_iter_vars {
	my ($batch, $test_item, $iteration) = @_;
	ok(
		$iteration == 1 || $iteration == 2,
		'only do 2 iteration of classification');
	return;
}

sub test_training_item_hook_vars {
	my ($batch, $test_item, $iteration, $train_item) = @_;
	isa_ok($train_item, 'Algorithm::AM::DataSet::Item');
	ok($train_item->comment =~ /my.*CommentHere/,
		'item is from training set');
}

# Test variables provided after an iteration is finished
sub test_end_iter_vars {
	my ($batch, $test_item, $iteration, $excluded_items, $result) = @_;

	if($test_item->class eq 'e'){
		is_deeply($result->scores, {e => '4', r => '4'},
			'class scores');
	}else{
		is_deeply($result->scores, {e => '4', r => '9'},
			'classes scores');
	}
	is_deeply($excluded_items, [], 'no items excluded');
	return;
}

sub test_end_test_vars {
	my ($self, $test_item, @item_results) = @_;
	isa_ok($item_results[0], 'Algorithm::AM::Result');
	is(scalar @item_results, 2, '1 result for each iteration');
	is($item_results[0]->test_item, $item_results[0]->test_item,
		'results have the same test item');
}

# Test variables provided after all iterations are finished
sub test_end_vars {
	my ($batch, @results) = @_;

	is_deeply($results[0]->scores, {e => '4', r => '9'},
		'scores for first result');
	is_deeply($results[1]->scores, {e => '4', r => '9'},
		'scores for second result');
	is_deeply($results[2]->scores, {e => '4', r => '4'},
		'scores for third result');
	is_deeply($results[3]->scores, {e => '4', r => '4'},
		'scores for fourth result');
	return;
}

sub test_defaults {
	my ($batch) = @_;
	is($batch->test_set, undef, 'test_set is undef outside of hooks');
	return;
}

# test that training_item_hook excludes items via false return value
sub test_training_item_hook {
	my $batch = Algorithm::AM::Batch->new(
		training_set => chapter_3_train(),
		training_item_hook 	=> sub {
			# false return value indicates that item should be excluded
			return 0;
		},
		end_repeat_hook => sub {
			my $excluded_items = $_[3];
			is(scalar @$excluded_items, 5,
				'training_item_hook excluded all items');
			isa_ok($excluded_items->[0],
				'Algorithm::AM::DataSet::Item');
		},
	);
	$batch->classify_all(chapter_3_test());
	return;
}



( run in 0.759 second using v1.01-cache-2.11-cpan-71847e10f99 )