001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.classification.liblinear;
018
019import com.oracle.labs.mlrg.olcut.config.ArgumentException;
020import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
021import com.oracle.labs.mlrg.olcut.config.Options;
022import com.oracle.labs.mlrg.olcut.config.UsageException;
023import org.tribuo.Trainer;
024import org.tribuo.classification.Label;
025import org.tribuo.classification.TrainTestHelper;
026import org.tribuo.classification.ensemble.ClassificationEnsembleOptions;
027import org.tribuo.data.DataOptions;
028
029import java.io.IOException;
030
031/**
032 * Build and run a liblinear-java classifier for a standard dataset.
033 */
034public class TrainTest {
035
036    /**
037     * Command line options.
038     */
039    public static class TrainTestOptions implements Options {
040
041        @Override
042        public String getOptionsDescription() {
043            return "Trains and tests a LibLinear model on the specified datasets.";
044        }
045
046        /**
047         * The data loading options.
048         */
049        public DataOptions general;
050        /**
051         * The liblinear options.
052         */
053        public LibLinearOptions libLinearOptions;
054        /**
055         * The ensemble options.
056         */
057        public ClassificationEnsembleOptions ensembleOptions;
058    }
059
060    /**
061     * Runs a TrainTest CLI.
062     * @param args the command line arguments
063     * @throws IOException if there is any error reading the examples.
064     */
065    public static void main(String[] args) throws IOException {
066        TrainTestOptions o = new TrainTestOptions();
067        try (ConfigurationManager cm = new ConfigurationManager(args, o)){
068            Trainer<Label> trainer = o.libLinearOptions.getTrainer();
069            trainer = o.ensembleOptions.wrapTrainer(trainer);
070            TrainTestHelper.run(cm, o.general, trainer);
071        } catch (UsageException e) {
072            System.out.println(e.getUsage());
073        } catch (ArgumentException e) {
074            System.out.println("Invalid argument: " + e.getMessage());
075        }
076    }
077}