001/** 
002 * Copyright (c) 2007-2011, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.ml.jar;
025
026import java.io.File;
027
028import org.cleartk.ml.DataWriter;
029import org.cleartk.ml.SequenceDataWriter;
030
031/**
032 * Command line tool for training a classifier from an output directory that has been filled by a
033 * {@link DirectoryDataWriter}.
034 * 
035 * Usage: <code>java org.cleartk.ml.jar.Train model-dir ...</code>
036 * 
037 * Some classifiers may accept additional arguments to train, see the documentation for the various
038 * {@link JarClassifierBuilder} subclasses for details.
039 * 
040 * <br>
041 * Copyright (c) 2007-2011, Regents of the University of Colorado <br>
042 * All rights reserved.
043 */
044
045public class Train {
046
047  /**
048   * Trains a classifier in the given directory.
049   * 
050   * The directory should already contain training data as written by a {@link DataWriter} or
051   * {@link SequenceDataWriter}.
052   * 
053   * @param directory
054   *          The directory containing the training data.
055   * @param trainingArguments
056   *          Additional command-line arguments that should be passed to the classifier.
057   */
058  public static void main(File directory, String... trainingArguments) throws Exception {
059    JarClassifierBuilder.trainAndPackage(directory, trainingArguments);
060  }
061
062  public static void main(String... args) throws Exception {
063    String programName = Train.class.getName();
064    String usage = String.format("usage: java %s DIR\n\n"
065        + "The directory DIR should contain the training-data.xxx file as\n"
066        + "created by a classifier DataWriter\n", programName);
067
068    // usage message for wrong number of arguments
069    if (args.length < 1) {
070      System.err.format("error: wrong number of arguments\n%s", usage);
071      System.exit(1);
072    }
073
074    // parse out the training directory from the arguments
075    File dir = new File(args[0]);
076    String[] remainingArgs = new String[args.length - 1];
077    System.arraycopy(args, 1, remainingArgs, 0, remainingArgs.length);
078
079    // train and package the classifier
080    Train.main(dir, remainingArgs);
081  }
082
083}