Search in sources :

Example 1 with SynchronizedSentenceIterator

use of org.deeplearning4j.text.sentenceiterator.SynchronizedSentenceIterator in project deeplearning4j by deeplearning4j.

the class AbstractCoOccurrences method iterator.

/**
     *
     *  This method returns iterator with elements pairs and their weights. Resulting iterator is safe to use in multi-threaded environment.
     *
     * Developer's note: thread safety on received iterator is delegated to PrefetchedSentenceIterator
     * @return
     */
public Iterator<Pair<Pair<T, T>, Double>> iterator() {
    final SentenceIterator iterator;
    try {
        iterator = new SynchronizedSentenceIterator(new PrefetchingSentenceIterator.Builder(new BasicLineIterator(targetFile)).setFetchSize(500000).build());
    } catch (Exception e) {
        logger.error("Target file was not found on last stage!");
        throw new RuntimeException(e);
    }
    return new Iterator<Pair<Pair<T, T>, Double>>() {

        /*
                    iterator should be built on top of current text file with all pairs
             */
        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public Pair<Pair<T, T>, Double> next() {
            String line = iterator.nextSentence();
            String[] strings = line.split(" ");
            T element1 = vocabCache.elementAtIndex(Integer.valueOf(strings[0]));
            T element2 = vocabCache.elementAtIndex(Integer.valueOf(strings[1]));
            Double weight = Double.valueOf(strings[2]);
            return new Pair<>(new Pair<>(element1, element2), weight);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove() method can't be supported on read-only interface");
        }
    };
}
Also used : BasicLineIterator(org.deeplearning4j.text.sentenceiterator.BasicLineIterator) SynchronizedSentenceIterator(org.deeplearning4j.text.sentenceiterator.SynchronizedSentenceIterator) PrefetchingSentenceIterator(org.deeplearning4j.text.sentenceiterator.PrefetchingSentenceIterator) SentenceIterator(org.deeplearning4j.text.sentenceiterator.SentenceIterator) SynchronizedSentenceIterator(org.deeplearning4j.text.sentenceiterator.SynchronizedSentenceIterator) PrefetchingSentenceIterator(org.deeplearning4j.text.sentenceiterator.PrefetchingSentenceIterator) Iterator(java.util.Iterator) FilteredSequenceIterator(org.deeplearning4j.models.sequencevectors.iterators.FilteredSequenceIterator) SentenceIterator(org.deeplearning4j.text.sentenceiterator.SentenceIterator) SynchronizedSequenceIterator(org.deeplearning4j.models.sequencevectors.iterators.SynchronizedSequenceIterator) SynchronizedSentenceIterator(org.deeplearning4j.text.sentenceiterator.SynchronizedSentenceIterator) BasicLineIterator(org.deeplearning4j.text.sentenceiterator.BasicLineIterator) SequenceIterator(org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator) Pair(org.deeplearning4j.berkeley.Pair)

Aggregations

Iterator (java.util.Iterator)1 Pair (org.deeplearning4j.berkeley.Pair)1 SequenceIterator (org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator)1 FilteredSequenceIterator (org.deeplearning4j.models.sequencevectors.iterators.FilteredSequenceIterator)1 SynchronizedSequenceIterator (org.deeplearning4j.models.sequencevectors.iterators.SynchronizedSequenceIterator)1 BasicLineIterator (org.deeplearning4j.text.sentenceiterator.BasicLineIterator)1 PrefetchingSentenceIterator (org.deeplearning4j.text.sentenceiterator.PrefetchingSentenceIterator)1 SentenceIterator (org.deeplearning4j.text.sentenceiterator.SentenceIterator)1 SynchronizedSentenceIterator (org.deeplearning4j.text.sentenceiterator.SynchronizedSentenceIterator)1