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.experiments;
018
019import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
020import com.oracle.labs.mlrg.olcut.config.Options;
021import com.oracle.labs.mlrg.olcut.config.UsageException;
022import org.tribuo.Trainer;
023import org.tribuo.classification.Label;
024import org.tribuo.classification.TrainTestHelper;
025import org.tribuo.data.DataOptions;
026
027import java.io.IOException;
028import java.util.logging.Logger;
029
030/**
031 * Build and run a classifier for a standard dataset.
032 */
033public class TrainTest {
034
035    private static final Logger logger = Logger.getLogger(TrainTest.class.getName());
036
037    /**
038     * Command line options.
039     */
040    public static class AllClassificationOptions implements Options {
041        @Override
042        public String getOptionsDescription() {
043            return "Trains and tests the specified classifier on the supplied datasets.";
044        }
045
046        /**
047         * All the classification trainer options.
048         */
049        public AllTrainerOptions trainerOptions;
050
051        /**
052         * Options for loading in data.
053         */
054        public DataOptions general;
055    }
056
057    /**
058     * Runs a TrainTest CLI.
059     * @param args the command line arguments
060     * @throws IOException if there is any error reading the examples.
061     */
062    public static void main(String[] args) throws IOException {
063        AllClassificationOptions o = new AllClassificationOptions();
064        try (ConfigurationManager cm = new ConfigurationManager(args,o)){
065            Trainer<Label> trainer = o.trainerOptions.getTrainer();
066            TrainTestHelper.run(cm, o.general, trainer);
067        } catch (UsageException e) {
068            logger.info(e.getMessage());
069            return;
070        }
071    }
072}