# functions to handle classifiers

package classify;

require util;


$next_classify_num = "0";

# load_classifier will load one classifier. $classinfo is an
# array containing information about the classifier to load. The
# first element of the array is the name of the classifier, the
# rest of the elements are classifier specific.
sub load_classifier {
    my ($classinfo) = @_;

    # get the classifier name
    my $classname = shift(@$classinfo);
    return undef unless defined $classname;
    
    # find the classifier
    my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify", 
					   "${classname}.pm");
    my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify", 
					    "${classname}.pm");
    if (-e $colclassname) { require $colclassname; }
    elsif (-e $mainclassname) { require $mainclassname; }
    else { die "ERROR - couldn't find classifier \"$classname\"\n"; }

    # create the classify object
    my ($classobj);
    map { $_ = "\"$_\""; } @$classinfo;
    my $options = join (",", @$classinfo);
    eval ("\$classobj = new $classname($options)");
    die "$@" if $@;

    return $classobj;
}

# init_classifiers resets all the classifiers and readys them to process
# the documents. They are each given a unique classification OID
sub init_classifiers {
    my ($classifiers) = @_;
    
    foreach $classobj (@$classifiers) {
	$classobj->init();
	$classobj->set_OID ("CL$next_classify_num");
	$next_classify_num++;
    }
}

# classify_doc lets each of the classifiers classify a document
sub classify_doc {
    my ($classifiers, $doc_obj) = @_;
    
    foreach $classobj (@$classifiers) {
	$classobj->classify($doc_obj);
    }
}

# output_classify_info outputs all the info needed for the classification
# to the gdbm
sub output_classify_info {
    my ($classifiers, $handle) = @_;
#    $handle = "main::STDOUT";

    my $contains_str = "";
    my $first = 1;
    
    # output each of the classifications
    foreach $classobj (@$classifiers) {
	$classobj->output_classify_info($handle);
	$contains_str .= ";" unless $first;
	$first = 0;
	$contains_str .= $classobj->get_OID();
    }

    # output information about each of the classifications
    print $handle "[browse]\n";
    print $handle "<doctype>classify\n";
    print $handle "<hastxt>0\n";
    print $handle "<contains>$contains_str\n" 
	unless $contains_str eq "";
    print $handle '-' x 70, "\n";
}


1;
