de.fu_berlin.ties.classify
Class ExternalClassifier

java.lang.Object
  extended by de.fu_berlin.ties.classify.TrainableClassifier
      extended by de.fu_berlin.ties.classify.ExternalClassifier
All Implemented Interfaces:
Classifier, XMLStorable

public class ExternalClassifier
extends TrainableClassifier

A proxy that provides a trainable classifier by communicating with an external (non-Java) program. Program name and command line options of the external classifier can be configured.

Instances of this class are thread-safe if and only if several instances of the external classifier can reliably run in parallel.

Version:
$Revision: 1.35 $, $Date: 2006/10/21 16:03:54 $, $Author: siefkes $
Author:
Christian Siefkes

Field Summary
static String CONFIG_DIR
          Configuration key: the directory to run the classifier in (optional, defaults to current working directory).
 
Fields inherited from class de.fu_berlin.ties.classify.TrainableClassifier
ATTRIB_CLASSES, ATTRIB_TRAIN_ALL, ELEMENT_MAIN, META_CLASSIFIER, MULTI_CLASSIFIER, OAR_CLASSIFIER, TIE_CLASSIFIER
 
Fields inherited from interface de.fu_berlin.ties.classify.Classifier
CONFIG_CLASSIFIER
 
Constructor Summary
ExternalClassifier(Set<String> allValidClasses)
          Creates a new instance based on the standard configuration.
ExternalClassifier(Set<String> allValidClasses, FeatureTransformer trans, File runDirectory, TiesConfiguration config)
          Creates a new instance based on the provided arguments.
ExternalClassifier(Set<String> allValidClasses, TiesConfiguration config)
          Creates a new instance based on the provided configuration.
 
Method Summary
protected  PredictionDistribution doClassify(FeatureVector features, Set candidateClasses, ContextMap context)
          Classifies an item that is represented by a feature vector by choosing the most probable class among a set of candidate classes.
protected  void doTrain(FeatureVector features, String targetClass, ContextMap context)
          Incorporates an item that is represented by a feature vector into the classification model.
 void reset()
          Resets the classifer, completely deleting the prediction model.
protected  boolean shouldTrain(String targetClass, PredictionDistribution predDist, ContextMap context)
          This implementation uses reinforcement training, if a thick threshold is configured.
 ObjectElement toElement()
          Stores all relevant fields of this object in an XML element for serialization. An equivalent object can be created by calling ObjectElement.createObject(org.dom4j.Element, Class) on the created element. Subclasses of TrainableClassifier should extend this method and the corresponding constructor from Element to ensure (de)serialization works as expected. Currently, this classifier does not support XML serialization, throwing an UnsupportedOperationException instead.
 String toString()
          Returns a string representation of this object.
 
Methods inherited from class de.fu_berlin.ties.classify.TrainableClassifier
classify, createClassifier, createClassifier, createClassifier, createClassifier, createClassifier, destroy, doTrainOnError, getAllClasses, getConfig, train, trainOnError, trainOnErrorHook
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONFIG_DIR

public static final String CONFIG_DIR
Configuration key: the directory to run the classifier in (optional, defaults to current working directory).

See Also:
Constant Field Values
Constructor Detail

ExternalClassifier

public ExternalClassifier(Set<String> allValidClasses)
                   throws ProcessingException
Creates a new instance based on the standard configuration.

Parameters:
allValidClasses - the set of all valid classes
Throws:
ProcessingException - if an I/O error occurs during initialization

ExternalClassifier

public ExternalClassifier(Set<String> allValidClasses,
                          TiesConfiguration config)
                   throws ProcessingException
Creates a new instance based on the provided configuration.

Parameters:
allValidClasses - the set of all valid classes
config - contains configuration properties
Throws:
ProcessingException - if an I/O error occurs during initialization

ExternalClassifier

public ExternalClassifier(Set<String> allValidClasses,
                          FeatureTransformer trans,
                          File runDirectory,
                          TiesConfiguration config)
                   throws ProcessingException
Creates a new instance based on the provided arguments.

Parameters:
allValidClasses - the set of all valid classes
trans - the last transformer in the transformer chain to use, or null if no feature transformers should be used
runDirectory - the directory to run the classifier in; used instead of the configured directory if not null
config - contains configuration properties
Throws:
ProcessingException - if an I/O error occurs during initialization
Method Detail

doClassify

protected PredictionDistribution doClassify(FeatureVector features,
                                            Set candidateClasses,
                                            ContextMap context)
                                     throws ProcessingException
Classifies an item that is represented by a feature vector by choosing the most probable class among a set of candidate classes.

Specified by:
doClassify in class TrainableClassifier
Parameters:
features - the feature vector to consider
candidateClasses - an array of the classes that are allowed for this item
context - ignored by this implementation
Returns:
the result of the classification; you can call PredictionDistribution.best() to get the most probably class; this classifier returns only the best prediction, so PredictionDistribution.size() will be 1
Throws:
ProcessingException - if an I/O error occurs during communication with the external program

doTrain

protected void doTrain(FeatureVector features,
                       String targetClass,
                       ContextMap context)
                throws ProcessingException
Incorporates an item that is represented by a feature vector into the classification model.

Specified by:
doTrain in class TrainableClassifier
Parameters:
features - the feature vector to consider
targetClass - the class of this feature vector
context - can be used to transport implementation-specific contextual information between the TrainableClassifier.doClassify(FeatureVector, Set, ContextMap), TrainableClassifier.doTrain(FeatureVector, String, ContextMap), and TrainableClassifier.trainOnErrorHook(PredictionDistribution, FeatureVector, String, Set, ContextMap) methods
Throws:
ProcessingException - if an error occurs during training

reset

public void reset()
           throws ProcessingException
Resets the classifer, completely deleting the prediction model.

Specified by:
reset in class TrainableClassifier
Throws:
ProcessingException - if an error occurs during reset

shouldTrain

protected boolean shouldTrain(String targetClass,
                              PredictionDistribution predDist,
                              ContextMap context)
This implementation uses reinforcement training, if a thick threshold is configured. Invoked by TrainableClassifier.trainOnError(FeatureVector, String, Set) to decide whether to train an instance. The default behavior is to train if the best prediction was wrong or didn't yield a positive probability ("train only errors"). Subclasses can override this method to add their own behavior, e.g. reinforcement training (thick threshold heuristic).

Overrides:
shouldTrain in class TrainableClassifier
Parameters:
targetClass - the expected class of this feature vector; must be contained in the set of candidateClasses
predDist - the prediction distribution returned by TrainableClassifier.doClassify(FeatureVector, Set, ContextMap)
context - can be used to transport implementation-specific contextual information between the TrainableClassifier.doClassify(FeatureVector, Set, ContextMap), TrainableClassifier.doTrain(FeatureVector, String, ContextMap), and TrainableClassifier.trainOnErrorHook(PredictionDistribution, FeatureVector, String, Set, ContextMap) methods
Returns:
whether to train this instance

toElement

public ObjectElement toElement()
                        throws UnsupportedOperationException
Stores all relevant fields of this object in an XML element for serialization. An equivalent object can be created by calling ObjectElement.createObject(org.dom4j.Element, Class) on the created element. Subclasses of TrainableClassifier should extend this method and the corresponding constructor from Element to ensure (de)serialization works as expected. Currently, this classifier does not support XML serialization, throwing an UnsupportedOperationException instead.

Specified by:
toElement in interface XMLStorable
Overrides:
toElement in class TrainableClassifier
Returns:
the created XML element
Throws:
UnsupportedOperationException - always thrown by this implementation

toString

public String toString()
Returns a string representation of this object.

Overrides:
toString in class TrainableClassifier
Returns:
a textual representation


Copyright © 2003-2007 Christian Siefkes. All Rights Reserved.