com.davidbracewell.ml.sequence.Sequence.java Source code

Java tutorial

Introduction

Here is the source code for com.davidbracewell.ml.sequence.Sequence.java

Source

/*
 * (c) 2005 David B. Bracewell
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.davidbracewell.ml.sequence;

import com.davidbracewell.ml.FeatureSet;
import com.davidbracewell.ml.HasFeatureSet;
import com.davidbracewell.ml.Instance;
import com.davidbracewell.ml.sequence.extractor.ContextExtractorSet;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * The type Sequence.
 *
 * @author David B. Bracewell
 */
public class Sequence<V> implements Serializable, HasFeatureSet {

    private static final long serialVersionUID = -9005384848088907231L;

    private final ContextExtractorSet<V> extractors;
    private final List<V> data;
    private final double[] labels;

    /**
     * Instantiates a new Sequence.
     *
     * @param data       the data
     * @param extractors the extractors
     */
    public Sequence(Iterable<V> data, ContextExtractorSet<V> extractors) {
        this.extractors = extractors;
        this.data = Collections.unmodifiableList(Lists.newArrayList(data));
        this.labels = new double[Iterables.size(data)];
        Arrays.fill(labels, Instance.MISSING_VALUE);
    }

    /**
     * Instantiates a new Sequence.
     *
     * @param data       the data
     * @param labels     the labels
     * @param extractors the extractors
     */
    public Sequence(Iterable<V> data, double[] labels, ContextExtractorSet<V> extractors) {
        this.extractors = extractors;
        this.data = Collections.unmodifiableList(Lists.newArrayList(data));
        this.labels = new double[labels.length];
        System.arraycopy(labels, 0, this.labels, 0, labels.length);
    }

    public List<V> asList() {
        return Collections.unmodifiableList(data);
    }

    /**
     * Gets data.
     *
     * @param i the i
     * @return the data
     */
    public V getData(int i) {
        if (i < 0 || i >= data.size()) {
            return null;
        }
        return data.get(i);
    }

    /**
     * Gets label.
     *
     * @param i the i
     * @return the label
     */
    public double getLabel(int i) {
        return labels[i];
    }

    /**
     * Length int.
     *
     * @return the int
     */
    public int length() {
        return data.size();
    }

    @Override
    public FeatureSet getFeatures() {
        return extractors.getFeatures();
    }

    @Override
    public void setFeatures(FeatureSet features) {
        Preconditions.checkNotNull(features);
        if (!features.equals(extractors.getFeatures())) {
            //TODO: Figure out this
        }
    }

    /**
     * Generate instance.
     *
     * @param index        the index
     * @param previousTags the previous tags
     * @return the instance
     */
    public final Instance generateInstance(int index, double[] previousTags) {
        Instance instance = extractors.extract(index, this, previousTags);
        if (previousTags.length > index) {
            instance.setTargetValue(previousTags[index]);
        }
        instance.trimToSize();
        return instance;
    }

    /**
     * Get labels.
     *
     * @return the labels
     */
    public final double[] getLabels() {
        return labels;
    }

    /**
     * Sub sequence.
     *
     * @param start the start
     * @param end   the end
     * @return the list
     */
    public List<V> subSequence(int start, int end) {
        return data.subList(start, end);
    }

}//END OF Sequence