All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.sandia.cognition.learning.function.categorization.CompositeCategorizer Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                CompositeCategorizer.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 * 
 * Copyright December 06, 2009, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive 
 * license for use of this work by or on behalf of the U.S. Government. Export 
 * of this program may require a license from the United States Government. 
 * See CopyrightHistory.txt for complete details.
 * 
 */

package gov.sandia.cognition.learning.function.categorization;

import gov.sandia.cognition.evaluator.Evaluator;
import gov.sandia.cognition.util.AbstractCloneableSerializable;
import gov.sandia.cognition.util.ObjectUtil;
import java.util.Set;

/**
 * Composes a preprocessor function with a categorizer. It takes an input
 * value and passes it to the preprocessor to get an intermediate value then it
 * passes the intermediate value to the categorizer to produce the output.
 *
 * @param   
 *      The type of input to the categorizer. It is the type of input to the
 *      preprocessor.
 * @param   
 *      The type of the intermediate values for the categorizer. It is the type
 *      of the output of the preprocessor and the input of the internal
 *      categorizer.
 * @param   
 *      The type of the output of the categorizer. It is the type of the output
 *      of the internal categorizer as well.
 * @author  Justin Basilico
 * @since   3.0
 */
public class CompositeCategorizer
    extends AbstractCloneableSerializable
    implements Categorizer
{

    /** The preprocessor for the input data. */
    protected Evaluator preprocessor;

    /** The categorizer. */
    protected Categorizer categorizer;

    /**
     * Creates a new CompositeCategorizer with the preprocessor and categorizer
     * set to null.
     */
    public CompositeCategorizer()
    {
        this(null, null);
    }

    /**
     * Creates a new {@code CompositeCategorizer}.
     *
     * @param   preprocessor
     *      The preprocessing evaluator that is run on the input.
     * @param   categorizer
     *      The categorizer that takes the output of the preprocessor and
     *      categorizes it.
     */
    public CompositeCategorizer(
        final Evaluator preprocessor,
        final Categorizer categorizer)
    {
        super();

        this.setPreprocessor(preprocessor);
        this.setCategorizer(categorizer);
    }

    @Override
    public CompositeCategorizer clone()
    {
        @SuppressWarnings("unchecked")
        final CompositeCategorizer
            clone = (CompositeCategorizer)
                super.clone();
        clone.preprocessor = ObjectUtil.cloneSmart(this.preprocessor);
        clone.categorizer = ObjectUtil.cloneSmart(this.categorizer);

        return clone;
    }

    public CategoryType evaluate(
        final InputType input)
    {
        // Run the preprocessor.
        final IntermediateType intermediate = this.preprocessor.evaluate(input);

        // Run the categorizer.
        return this.categorizer.evaluate(intermediate);
    }

    /**
     * Gets the set of categories, which is just the set returned by the
     * categorizer. If there is no categorizer, the set is null.
     *
     * @return
     *      The set of categories.
     */
    public Set getCategories()
    {
        if (this.categorizer == null)
        {
            return null;
        }
        else
        {
            return this.categorizer.getCategories();
        }
    }

    /**
     * Gets the preprocessor, which takes the input and produces an intermediate
     * value that is then passed to the categorizer.
     *
     * @return
     *      The preprocessor.
     */
    public Evaluator getPreprocessor()
    {
        return this.preprocessor;
    }

    /**
     * Sets the preprocessor, which takes the input and produces an intermediate
     * value that is then passed to the categorizer.
     *
     * @param   preprocessor
     *      The preprocessor.
     */
    public void setPreprocessor(
        final Evaluator preprocessor)
    {
        this.preprocessor = preprocessor;
    }

    /**
     * Gets the categorizer, which takes the output of the preprocessor and
     * categorizes it.
     *
     * @return
     *      The categorizer.
     */
    public Categorizer getCategorizer()
    {
        return this.categorizer;
    }

    /**
     * Sets the categorizer, which takes the output of the preprocessor and
     * categorizes it.
     *
     * @param   categorizer
     *      The categorizer.
     */
    public void setCategorizer(
        final Categorizer categorizer)
    {
        this.categorizer = categorizer;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy