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

org.deeplearning4j.text.sentenceiterator.MutipleEpochsSentenceIterator Maven / Gradle / Ivy

There is a newer version: 1.0.0-M2.1
Show newest version
package org.deeplearning4j.text.sentenceiterator;

import lombok.NonNull;
import org.deeplearning4j.datasets.iterator.MultipleEpochsIterator;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * This SentenceIterator implemenation wraps existing sentence iterator, and resets it numEpochs times
 *
 * This class is usable for tests purposes mostly.
 *
 * @author [email protected]
 */
public class MutipleEpochsSentenceIterator implements SentenceIterator {
    private SentenceIterator iterator;
    private int numEpochs;
    private AtomicInteger counter = new AtomicInteger(0);

    public MutipleEpochsSentenceIterator(@NonNull SentenceIterator iterator, int numEpochs) {
        this.numEpochs = numEpochs;
        this.iterator = iterator;

        this.iterator.reset();
    }

    @Override
    public String nextSentence() {
        return iterator.nextSentence();
    }

    @Override
    public boolean hasNext() {
        if (!iterator.hasNext()) {
            if (counter.get() < numEpochs - 1) {
                counter.incrementAndGet();
                iterator.reset();
                return true;
            } else
                return false;
        }
        return true;
    }

    @Override
    public void reset() {
        this.counter.set(0);
        this.iterator.reset();
    }

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

    @Override
    public SentencePreProcessor getPreProcessor() {
        return this.iterator.getPreProcessor();
    }

    @Override
    public void setPreProcessor(SentencePreProcessor preProcessor) {
        this.iterator.setPreProcessor(preProcessor);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy