use of org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator in project deeplearning4j by deeplearning4j.
the class VocabConstructorTest method testCounter1.
@Test
public void testCounter1() throws Exception {
VocabCache<VocabWord> vocabCache = new AbstractCache.Builder<VocabWord>().build();
final List<VocabWord> words = new ArrayList<>();
words.add(new VocabWord(1, "word"));
words.add(new VocabWord(2, "test"));
words.add(new VocabWord(1, "here"));
Iterable<Sequence<VocabWord>> iterable = new Iterable<Sequence<VocabWord>>() {
@Override
public Iterator<Sequence<VocabWord>> iterator() {
return new Iterator<Sequence<VocabWord>>() {
private AtomicBoolean switcher = new AtomicBoolean(true);
@Override
public boolean hasNext() {
return switcher.getAndSet(false);
}
@Override
public Sequence<VocabWord> next() {
Sequence<VocabWord> sequence = new Sequence<>(words);
return sequence;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
SequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder<>(iterable).build();
VocabConstructor<VocabWord> constructor = new VocabConstructor.Builder<VocabWord>().addSource(sequenceIterator, 0).useAdaGrad(false).setTargetVocabCache(vocabCache).build();
constructor.buildJointVocabulary(false, true);
assertEquals(3, vocabCache.numWords());
assertEquals(1, vocabCache.wordFrequency("test"));
}
use of org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator in project deeplearning4j by deeplearning4j.
the class VocabConstructorTest method testCounter2.
@Test
public void testCounter2() throws Exception {
VocabCache<VocabWord> vocabCache = new AbstractCache.Builder<VocabWord>().build();
final List<VocabWord> words = new ArrayList<>();
words.add(new VocabWord(1, "word"));
words.add(new VocabWord(0, "test"));
words.add(new VocabWord(1, "here"));
Iterable<Sequence<VocabWord>> iterable = new Iterable<Sequence<VocabWord>>() {
@Override
public Iterator<Sequence<VocabWord>> iterator() {
return new Iterator<Sequence<VocabWord>>() {
private AtomicBoolean switcher = new AtomicBoolean(true);
@Override
public boolean hasNext() {
return switcher.getAndSet(false);
}
@Override
public Sequence<VocabWord> next() {
Sequence<VocabWord> sequence = new Sequence<>(words);
return sequence;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
SequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder<>(iterable).build();
VocabConstructor<VocabWord> constructor = new VocabConstructor.Builder<VocabWord>().addSource(sequenceIterator, 0).useAdaGrad(false).setTargetVocabCache(vocabCache).build();
constructor.buildJointVocabulary(false, true);
assertEquals(3, vocabCache.numWords());
assertEquals(1, vocabCache.wordFrequency("test"));
}
Aggregations