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.sequence.viterbi;
018
019import com.oracle.labs.mlrg.olcut.config.Option;
020import com.oracle.labs.mlrg.olcut.config.Options;
021import org.tribuo.Trainer;
022import org.tribuo.classification.Label;
023
024/**
025 * Options for building a viterbi trainer.
026 */
027public class ViterbiTrainerOptions implements Options {
028
029    /**
030     * Type of label features to include.
031     */
032    public enum ViterbiLabelFeatures {
033        /**
034         * The default label features.
035         */
036        DEFAULT,
037        /**
038         * No label features.
039         */
040        NONE
041    }
042
043    @Option(longName = "viterbi-score-aggregation", usage = "Aggregation operation, choices are {ADD, MULTIPLY}.")
044    private ViterbiModel.ScoreAggregation viterbiScoreAggregation = ViterbiModel.ScoreAggregation.ADD;
045
046    @Option(longName = "viterbi-label-features", usage = "Add label features to the inner training, choices are {DEFAULT, NONE}.")
047    private ViterbiLabelFeatures viterbiLabelFeatures = ViterbiLabelFeatures.DEFAULT;
048
049    @Option(longName = "viterbi-stack-size", usage = "-1 for no limit on the stack size")
050    private int viterbiStackSize = -1;
051
052    /**
053     * Creates a viterbi trainer wrapping the supplied label trainer.
054     * @param innerTrainer The trainer to wrap.
055     * @return A ViterbiTrainer.
056     */
057    public ViterbiTrainer getSequenceTrainer(Trainer<Label> innerTrainer) {
058        LabelFeatureExtractor lfe = new NoopFeatureExtractor();
059        if (viterbiLabelFeatures == ViterbiLabelFeatures.DEFAULT) {
060            lfe = new DefaultFeatureExtractor();
061        }
062        return new ViterbiTrainer(innerTrainer, lfe, viterbiStackSize, viterbiScoreAggregation);
063    }
064}