MaltParser 1.4.1

org.maltparser.ml.libsvm
Class Libsvm

java.lang.Object
  extended by org.maltparser.ml.libsvm.Libsvm
All Implemented Interfaces:
LearningMethod

public class Libsvm
extends Object
implements LearningMethod

Implements an interface to the LIBSVM learner (currently the LIBSVM 2.91 is used). More information about LIBSVM can be found at LIBSVM -- A Library for Support Vector Machines.

Since:
1.0
Author:
Johan Hall

Nested Class Summary
static class Libsvm.Verbostity
           
 
Field Summary
protected  boolean excludeNullValues
           
protected  int learnerMode
           
static String LIBSVM_VERSION
           
protected  String name
           
protected  int numberOfInstances
           
protected  InstanceModel owner
           
protected  String pathExternalSVMTrain
           
protected  boolean saveInstanceFiles
           
 
Fields inherited from interface org.maltparser.ml.LearningMethod
BATCH, CLASSIFY
 
Constructor Summary
Libsvm(InstanceModel owner, Integer learnerMode)
          Constructs a LIBSVM learner.
 
Method Summary
 void addInstance(SingleDecision decision, FeatureVector featureVector)
           
protected  void closeInstanceWriter()
           
 Map<Integer,Integer> createFeatureIdToCountMap(ArrayList<Integer> divideFeatureIndexVector)
           
 double crossValidate(FeatureVector featureVector, int nrOfSplits)
          This method does a cross validation of the training instances added and return the average score over the nrOfSplit divisions.
 void decreaseNumberOfInstances()
           
 void divideByFeatureSet(Set<Integer> featureIdsToCreateSeparateBranchesForSet, ArrayList<Integer> divideFeatureIndexVector, String otherId)
           
protected  void finalize()
           
 void finalizeSentence(DependencyStructure dependencyGraph)
           
protected  JarEntry getConfigFileEntry(String suffix)
           
 DependencyParserConfig getConfiguration()
          Returns the current configuration
protected  File getFile(String suffix)
           
protected  InputStreamReader getInstanceInputStreamReader(String suffix)
           
protected  InputStreamReader getInstanceInputStreamReaderFromConfigFile(String suffix)
           
protected  OutputStreamWriter getInstanceOutputStreamWriter(String suffix)
           
 BufferedWriter getInstanceWriter()
           
 int getLearnerMode()
           
 String getLearningMethodName()
           
 int getNumberOfInstances()
           
 InstanceModel getOwner()
           
 String getParamString()
          Returns the parameter string for used for configure LIBSVM
 String[] getSVMParamStringArray(libsvm.svm_parameter param)
           
 void increaseNumberOfInstances()
           
protected  void initParameters(libsvm.svm_parameter param)
          Assign a default value to all svm parameters
protected  void initSpecialParameters()
           
protected  void initSvmParam(String paramString)
          Initialize the LIBSVM according to the parameter string
static void maltSVMFormat2OriginalSVMFormat(InputStreamReader isr, OutputStreamWriter osw, int[] cardinalities)
          Converts the instance file (Malt's own SVM format) into the LIBSVM (SVMLight) format.
 void moveAllInstances(LearningMethod method, FeatureFunction divideFeature, ArrayList<Integer> divideFeatureIndexVector)
           
 void noMoreInstances()
           
 void parseParameters(String paramstring, libsvm.svm_parameter param)
          Parses the parameter string.
 boolean predict(FeatureVector featureVector, SingleDecision decision)
           
 libsvm.svm_problem readProblemMaltSVMFormat(InputStreamReader isr, int[] cardinalities, libsvm.svm_parameter param)
          Reads an instance file into a svm_problem object according to the Malt-SVM format, which is column fixed format (tab-separated).
 void setLearnerMode(int learnerMode)
           
protected  void setLearningMethodName(String name)
           
protected  void setNumberOfInstances(int numberOfInstances)
           
protected  void setOwner(InstanceModel owner)
           
 void svm_predict_with_kbestlist(libsvm.svm_model model, libsvm.svm_node[] x, KBestList kBestList)
           
 void terminate()
           
 String toString()
           
 String toStringParameters(libsvm.svm_parameter param)
          Returns a string containing all svm-parameters of interest
 void train(FeatureVector featureVector)
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LIBSVM_VERSION

public static final String LIBSVM_VERSION
See Also:
Constant Field Values

owner

protected InstanceModel owner

learnerMode

protected int learnerMode

name

protected String name

numberOfInstances

protected int numberOfInstances

saveInstanceFiles

protected boolean saveInstanceFiles

excludeNullValues

protected boolean excludeNullValues

pathExternalSVMTrain

protected String pathExternalSVMTrain
Constructor Detail

Libsvm

public Libsvm(InstanceModel owner,
              Integer learnerMode)
       throws MaltChainedException
Constructs a LIBSVM learner.

Parameters:
owner - the guide model owner
learnerMode - the mode of the learner TRAIN or CLASSIFY
Throws:
MaltChainedException
Method Detail

addInstance

public void addInstance(SingleDecision decision,
                        FeatureVector featureVector)
                 throws MaltChainedException
Specified by:
addInstance in interface LearningMethod
Throws:
MaltChainedException

finalizeSentence

public void finalizeSentence(DependencyStructure dependencyGraph)
                      throws MaltChainedException
Specified by:
finalizeSentence in interface LearningMethod
Throws:
MaltChainedException

noMoreInstances

public void noMoreInstances()
                     throws MaltChainedException
Specified by:
noMoreInstances in interface LearningMethod
Throws:
MaltChainedException

train

public void train(FeatureVector featureVector)
           throws MaltChainedException
Specified by:
train in interface LearningMethod
Throws:
MaltChainedException

crossValidate

public double crossValidate(FeatureVector featureVector,
                            int nrOfSplits)
                     throws MaltChainedException
Description copied from interface: LearningMethod
This method does a cross validation of the training instances added and return the average score over the nrOfSplit divisions. This method is used by the decision tree model when deciding which parts of the tree that shall be pruned.

Specified by:
crossValidate in interface LearningMethod
Returns:
a double
Throws:
MaltChainedException

moveAllInstances

public void moveAllInstances(LearningMethod method,
                             FeatureFunction divideFeature,
                             ArrayList<Integer> divideFeatureIndexVector)
                      throws MaltChainedException
Specified by:
moveAllInstances in interface LearningMethod
Throws:
MaltChainedException

predict

public boolean predict(FeatureVector featureVector,
                       SingleDecision decision)
                throws MaltChainedException
Specified by:
predict in interface LearningMethod
Throws:
MaltChainedException

terminate

public void terminate()
               throws MaltChainedException
Specified by:
terminate in interface LearningMethod
Throws:
MaltChainedException

getInstanceWriter

public BufferedWriter getInstanceWriter()
Specified by:
getInstanceWriter in interface LearningMethod

closeInstanceWriter

protected void closeInstanceWriter()
                            throws MaltChainedException
Throws:
MaltChainedException

initSvmParam

protected void initSvmParam(String paramString)
                     throws MaltChainedException
Initialize the LIBSVM according to the parameter string

Parameters:
paramString - the parameter string to configure the LIBSVM learner.
Throws:
MaltChainedException

getParamString

public String getParamString()
Returns the parameter string for used for configure LIBSVM

Returns:
the parameter string for used for configure LIBSVM

getOwner

public InstanceModel getOwner()

setOwner

protected void setOwner(InstanceModel owner)

getLearnerMode

public int getLearnerMode()

setLearnerMode

public void setLearnerMode(int learnerMode)
                    throws MaltChainedException
Throws:
MaltChainedException

getLearningMethodName

public String getLearningMethodName()

getConfiguration

public DependencyParserConfig getConfiguration()
                                        throws MaltChainedException
Returns the current configuration

Returns:
the current configuration
Throws:
MaltChainedException

getNumberOfInstances

public int getNumberOfInstances()
                         throws MaltChainedException
Throws:
MaltChainedException

increaseNumberOfInstances

public void increaseNumberOfInstances()
Specified by:
increaseNumberOfInstances in interface LearningMethod

decreaseNumberOfInstances

public void decreaseNumberOfInstances()
Specified by:
decreaseNumberOfInstances in interface LearningMethod

setNumberOfInstances

protected void setNumberOfInstances(int numberOfInstances)

setLearningMethodName

protected void setLearningMethodName(String name)

getInstanceOutputStreamWriter

protected OutputStreamWriter getInstanceOutputStreamWriter(String suffix)
                                                    throws MaltChainedException
Throws:
MaltChainedException

getInstanceInputStreamReader

protected InputStreamReader getInstanceInputStreamReader(String suffix)
                                                  throws MaltChainedException
Throws:
MaltChainedException

getInstanceInputStreamReaderFromConfigFile

protected InputStreamReader getInstanceInputStreamReaderFromConfigFile(String suffix)
                                                                throws MaltChainedException
Throws:
MaltChainedException

getFile

protected File getFile(String suffix)
                throws MaltChainedException
Throws:
MaltChainedException

getConfigFileEntry

protected JarEntry getConfigFileEntry(String suffix)
                               throws MaltChainedException
Throws:
MaltChainedException

readProblemMaltSVMFormat

public final libsvm.svm_problem readProblemMaltSVMFormat(InputStreamReader isr,
                                                         int[] cardinalities,
                                                         libsvm.svm_parameter param)
                                                  throws MaltChainedException
Reads an instance file into a svm_problem object according to the Malt-SVM format, which is column fixed format (tab-separated).

Parameters:
isr - the instance stream reader for the instance file
cardinalities - a array containing the number of distinct values for a particular column.
param - a svm_parameter object
Throws:
LibsvmException
MaltChainedException

initSpecialParameters

protected void initSpecialParameters()
                              throws MaltChainedException
Throws:
MaltChainedException

initParameters

protected void initParameters(libsvm.svm_parameter param)
                       throws MaltChainedException
Assign a default value to all svm parameters

Parameters:
param - a svm_parameter object
Throws:
MaltChainedException

toStringParameters

public String toStringParameters(libsvm.svm_parameter param)
Returns a string containing all svm-parameters of interest

Parameters:
param - a svm_parameter object
Returns:
a string containing all svm-parameters of interest

getSVMParamStringArray

public String[] getSVMParamStringArray(libsvm.svm_parameter param)

parseParameters

public void parseParameters(String paramstring,
                            libsvm.svm_parameter param)
                     throws MaltChainedException
Parses the parameter string. The parameter string must contain parameter and value pairs, which are separated by a blank or a underscore. The parameter begins with a character '-' followed by a one-character flag and the value must comply with the parameters data type. Some examples: -s 0 -t 1 -d 2 -g 0.4 -e 0.1 -s_0_-t_1_-d_2_-g_0.4_-e_0.1

Parameters:
paramstring - the parameter string
param - a svm_parameter object
Throws:
LibsvmException
MaltChainedException

svm_predict_with_kbestlist

public void svm_predict_with_kbestlist(libsvm.svm_model model,
                                       libsvm.svm_node[] x,
                                       KBestList kBestList)
                                throws MaltChainedException
Throws:
MaltChainedException

maltSVMFormat2OriginalSVMFormat

public static void maltSVMFormat2OriginalSVMFormat(InputStreamReader isr,
                                                   OutputStreamWriter osw,
                                                   int[] cardinalities)
                                            throws MaltChainedException
Converts the instance file (Malt's own SVM format) into the LIBSVM (SVMLight) format. The input instance file is removed (replaced) by the instance file in the LIBSVM (SVMLight) format. If a column contains -1, the value will be removed in destination file.

Parameters:
isr - the input stream reader for the source instance file
osw - the output stream writer for the destination instance file
cardinalities - a vector containing the number of distinct values for a particular column
Throws:
LibsvmException
MaltChainedException

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

toString

public String toString()
Overrides:
toString in class Object

divideByFeatureSet

public void divideByFeatureSet(Set<Integer> featureIdsToCreateSeparateBranchesForSet,
                               ArrayList<Integer> divideFeatureIndexVector,
                               String otherId)
                        throws MaltChainedException
Specified by:
divideByFeatureSet in interface LearningMethod
Throws:
MaltChainedException

createFeatureIdToCountMap

public Map<Integer,Integer> createFeatureIdToCountMap(ArrayList<Integer> divideFeatureIndexVector)
                                               throws MaltChainedException
Specified by:
createFeatureIdToCountMap in interface LearningMethod
Throws:
MaltChainedException

MaltParser 1.4.1

Copyright 2007-2010 Johan Hall, Jens Nilsson and Joakim Nivre.