home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (VocabConstructorTest.java)

This example Java source code file (VocabConstructorTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractcache, abstractsequenceiterator, atomicboolean, basiclineiterator, classpathresource, exception, iterable, override, sentencetransformer, sequence, test, util, vocabcache, vocabconstructor, vocabword

The VocabConstructorTest.java Java example source code

package org.deeplearning4j.models.word2vec.wordstore;

import org.canova.api.util.ClassPathResource;
import org.deeplearning4j.models.sequencevectors.interfaces.SequenceIterator;
import org.deeplearning4j.models.sequencevectors.iterators.AbstractSequenceIterator;
import org.deeplearning4j.models.sequencevectors.sequence.Sequence;
import org.deeplearning4j.models.sequencevectors.transformers.impl.SentenceTransformer;
import org.deeplearning4j.models.word2vec.VocabWord;
import org.deeplearning4j.models.word2vec.wordstore.inmemory.AbstractCache;
import org.deeplearning4j.text.documentiterator.FileLabelAwareIterator;
import org.deeplearning4j.text.sentenceiterator.BasicLineIterator;
import org.deeplearning4j.text.sentenceiterator.SentenceIterator;
import org.deeplearning4j.text.tokenization.tokenizer.Tokenizer;
import org.deeplearning4j.text.tokenization.tokenizer.preprocessor.CommonPreprocessor;
import org.deeplearning4j.text.tokenization.tokenizerfactory.DefaultTokenizerFactory;
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.*;

/**
 * @author raver119@gmail.com
 */
public class VocabConstructorTest {

    protected static final Logger log = LoggerFactory.getLogger(VocabConstructorTest.class);

    TokenizerFactory t = new DefaultTokenizerFactory();


    @Before
    public void setUp() throws Exception {
        t.setTokenPreProcessor(new CommonPreprocessor());
    }

    @Test
    public void testVocab() throws Exception {
        File inputFile = new ClassPathResource("big/raw_sentences.txt").getFile();
        SentenceIterator iter = new BasicLineIterator(inputFile);

        Set<String> set = new HashSet<>();
        int lines = 0;
        int cnt = 0;
        while (iter.hasNext()) {
            Tokenizer tok = t.create(iter.nextSentence());
            for (String token: tok.getTokens()) {
                if (token == null || token.isEmpty() || token.trim().isEmpty()) continue;
                    cnt++;

                if (!set.contains(token))
                    set.add(token);
            }

            lines++;
        }

        log.info("Total number of tokens: [" + cnt + "], lines: [" + lines+"], set size: ["+ set.size() +"]");
        log.info("Set:\n" + set);
    }


    @Test
    public void testBuildJointVocabulary1() throws Exception {
        File inputFile = new ClassPathResource("big/raw_sentences.txt").getFile();
        SentenceIterator iter = new BasicLineIterator(inputFile);

        VocabCache<VocabWord> cache = new AbstractCache.Builder().build();

        SentenceTransformer transformer = new SentenceTransformer.Builder()
                .iterator(iter)
                .tokenizerFactory(t)
                .build();


        /*
            And we pack that transformer into AbstractSequenceIterator
         */
        AbstractSequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder(transformer)
                .build();

        VocabConstructor<VocabWord> constructor = new VocabConstructor.Builder()
                .addSource(sequenceIterator, 0)
                .useAdaGrad(false)
                .setTargetVocabCache(cache)
                .build();

        constructor.buildJointVocabulary(true, false);


        assertEquals(244, cache.numWords());

        assertEquals(0, cache.totalWordOccurrences());
    }


    @Test
    public void testBuildJointVocabulary2() throws Exception {
        File inputFile = new ClassPathResource("big/raw_sentences.txt").getFile();
        SentenceIterator iter = new BasicLineIterator(inputFile);

        VocabCache<VocabWord> cache = new AbstractCache.Builder().build();

        SentenceTransformer transformer = new SentenceTransformer.Builder()
                .iterator(iter)
                .tokenizerFactory(t)
                .build();


        AbstractSequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder(transformer)
                .build();

        VocabConstructor<VocabWord> constructor = new VocabConstructor.Builder()
                .addSource(sequenceIterator, 5)
                .useAdaGrad(false)
                .setTargetVocabCache(cache)
                .build();

        constructor.buildJointVocabulary(false, true);

//        assertFalse(cache.hasToken("including"));

        assertEquals(242, cache.numWords());


        assertEquals("i", cache.wordAtIndex(1));
        assertEquals("it", cache.wordAtIndex(0));

        assertEquals(634303, cache.totalWordOccurrences());
    }

    @Test
    public void testCounter1() throws Exception {
        VocabCache<VocabWord> vocabCache = new AbstractCache.Builder().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 iterable = new Iterable>() {
            @Override
            public Iterator<Sequence iterator() {

                return new Iterator<Sequence() {
                    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()
                .addSource(sequenceIterator, 0)
                .useAdaGrad(false)
                .setTargetVocabCache(vocabCache)
                .build();

        constructor.buildJointVocabulary(false, true);

        assertEquals(3, vocabCache.numWords());

        assertEquals(1, vocabCache.wordFrequency("test"));
    }

    @Test
    public void testCounter2() throws Exception {
        VocabCache<VocabWord> vocabCache = new AbstractCache.Builder().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 iterable = new Iterable>() {
            @Override
            public Iterator<Sequence iterator() {

                return new Iterator<Sequence() {
                    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()
                .addSource(sequenceIterator, 0)
                .useAdaGrad(false)
                .setTargetVocabCache(vocabCache)
                .build();

        constructor.buildJointVocabulary(false, true);

        assertEquals(3, vocabCache.numWords());

        assertEquals(1, vocabCache.wordFrequency("test"));
    }

    /**
     * Here we test basic vocab transfer, done WITHOUT labels
     * @throws Exception
     */
    @Test
    public void testMergedVocab1() throws Exception {
        AbstractCache<VocabWord> cacheSource = new AbstractCache.Builder()
                .build();

        AbstractCache<VocabWord> cacheTarget = new AbstractCache.Builder()
                .build();

        ClassPathResource resource = new ClassPathResource("big/raw_sentences.txt");

        BasicLineIterator underlyingIterator = new BasicLineIterator(resource.getFile());


        SentenceTransformer transformer = new SentenceTransformer.Builder()
                .iterator(underlyingIterator)
                .tokenizerFactory(t)
                .build();

        AbstractSequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder(transformer)
                .build();

        VocabConstructor<VocabWord> vocabConstructor = new VocabConstructor.Builder()
                .addSource(sequenceIterator, 1)
                .setTargetVocabCache(cacheSource)
                .build();

        vocabConstructor.buildJointVocabulary(false, true);

        int sourceSize = cacheSource.numWords();
        log.info("Source Vocab size: " + sourceSize);


        VocabConstructor<VocabWord> vocabTransfer = new VocabConstructor.Builder()
                .addSource(sequenceIterator, 1)
                .setTargetVocabCache(cacheTarget)
                .build();

        vocabTransfer.buildMergedVocabulary(cacheSource, false);

        assertEquals(sourceSize, cacheTarget.numWords());
    }

    @Test
    public void testMergedVocabWithLabels1() throws Exception {
        AbstractCache<VocabWord> cacheSource = new AbstractCache.Builder()
                .build();

        AbstractCache<VocabWord> cacheTarget = new AbstractCache.Builder()
                .build();

        ClassPathResource resource = new ClassPathResource("big/raw_sentences.txt");

        BasicLineIterator underlyingIterator = new BasicLineIterator(resource.getFile());


        SentenceTransformer transformer = new SentenceTransformer.Builder()
                .iterator(underlyingIterator)
                .tokenizerFactory(t)
                .build();

        AbstractSequenceIterator<VocabWord> sequenceIterator = new AbstractSequenceIterator.Builder(transformer)
                .build();

        VocabConstructor<VocabWord> vocabConstructor = new VocabConstructor.Builder()
                .addSource(sequenceIterator, 1)
                .setTargetVocabCache(cacheSource)
                .build();

        vocabConstructor.buildJointVocabulary(false, true);

        int sourceSize = cacheSource.numWords();
        log.info("Source Vocab size: " + sourceSize);

        FileLabelAwareIterator labelAwareIterator = new FileLabelAwareIterator.Builder()
                .addSourceFolder(new ClassPathResource("/paravec/labeled").getFile())
                .build();

        transformer = new SentenceTransformer.Builder()
                .iterator(labelAwareIterator)
                .tokenizerFactory(t)
                .build();

        sequenceIterator = new AbstractSequenceIterator.Builder<VocabWord>(transformer)
                .build();

        VocabConstructor<VocabWord> vocabTransfer = new VocabConstructor.Builder()
                .addSource(sequenceIterator, 1)
                .setTargetVocabCache(cacheTarget)
                .build();

        vocabTransfer.buildMergedVocabulary(cacheSource, true);

        // those +3 go for 3 additional entries in target VocabCache: labels
        assertEquals(sourceSize + 3, cacheTarget.numWords());

        // now we check index equality for transferred elements
        assertEquals(cacheSource.wordAtIndex(17), cacheTarget.wordAtIndex(17));
        assertEquals(cacheSource.wordAtIndex(45), cacheTarget.wordAtIndex(45));
        assertEquals(cacheSource.wordAtIndex(89), cacheTarget.wordAtIndex(89));

        // we check that newly added labels have indexes beyond the VocabCache index space
        // please note, we need >= since the indexes are zero-based, and sourceSize is not
        assertTrue(cacheTarget.indexOf("Zfinance") > sourceSize - 1);
        assertTrue(cacheTarget.indexOf("Zscience") > sourceSize - 1);
        assertTrue(cacheTarget.indexOf("Zhealth") > sourceSize - 1);
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java VocabConstructorTest.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.