All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.deeplearning4j.text.documentiterator.SimpleLabelAwareIterator Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2015-2018 Skymind, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Apache License, Version 2.0 which is available at
 * https://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.
 *
 * SPDX-License-Identifier: Apache-2.0
 ******************************************************************************/

package org.deeplearning4j.text.documentiterator;

import lombok.NonNull;

import java.util.Iterator;

/**
 * This class provide option to build LabelAwareIterator from Iterable or Iterator objects
 *
 * PLEASE NOTE: This iterator is meant to be used with externally-originated data via Java Iterable/Iterator interface.
 * It IS possible to use Collection/List object here, but it's NOT recommended, since huge List with data might cause significant
 * performance penalty due to JVM Garbage Collection mechanics.
 *
 * @author [email protected]
 */
public class SimpleLabelAwareIterator implements LabelAwareIterator {
    protected transient Iterable underlyingIterable;
    protected transient Iterator currentIterator;
    protected LabelsSource labels = new LabelsSource();

    /**
     * Builds LabelAwareIterator instance using Iterable object
     * @param iterable
     */
    public SimpleLabelAwareIterator(@NonNull Iterable iterable) {
        this.underlyingIterable = iterable;
        this.currentIterator = underlyingIterable.iterator();
    }

    /**
     * Builds LabelAwareIterator instance using Iterator object
     * PLEASE NOTE: If instance is built using Iterator object, reset() method becomes unavailable
     *
     * @param iterator
     */
    public SimpleLabelAwareIterator(@NonNull Iterator iterator) {
        this.currentIterator = iterator;
    }

    /**
     * This method checks, if there's more LabelledDocuments in underlying iterator
     * @return
     */
    @Override
    public boolean hasNextDocument() {
        return currentIterator.hasNext();
    }

    /**
     * This method returns next LabelledDocument from underlying iterator
     * @return
     */
    @Override
    public LabelledDocument nextDocument() {
        LabelledDocument document = currentIterator.next();
        for (String label : document.getLabels()) {
            labels.storeLabel(label);
        }

        return document;
    }

    @Override
    public boolean hasNext() {
        return hasNextDocument();
    }

    @Override
    public LabelledDocument next() {
        return nextDocument();
    }

    @Override
    public void remove() {
        // no-op
    }

    @Override
    public void shutdown() {
        // no-op
    }

    /**
     * This methods resets LabelAwareIterator by creating new Iterator from Iterable internally
     */
    @Override
    public void reset() {
        if (underlyingIterable != null)
            this.currentIterator = this.underlyingIterable.iterator();
        else
            throw new UnsupportedOperationException(
                            "You can't use reset() method for Iterator<> based instance, please provide Iterable<> instead, or avoid reset()");
    }

    /**
     * This method returns LabelsSource instance, containing all labels derived from this iterator
     * @return
     */
    @Override
    public LabelsSource getLabelsSource() {
        return labels;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy