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

Java example source code file (GloveTest.java)

This example Java source code file (GloveTest.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

before, classpathresource, commonpreprocessor, defaulttokenizerfactory, exception, file, glove, glovetest, indarray, nearest, sentenceiterator, sentencepreprocessor, string, test, util

The GloveTest.java Java example source code

 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://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.

package org.deeplearning4j.models.glove;
import static org.junit.Assert.*;

import org.canova.api.util.ClassPathResource;
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
import org.deeplearning4j.models.word2vec.VocabWord;
import org.deeplearning4j.text.sentenceiterator.BasicLineIterator;
import org.deeplearning4j.text.sentenceiterator.LineSentenceIterator;
import org.deeplearning4j.text.sentenceiterator.SentenceIterator;
import org.deeplearning4j.text.sentenceiterator.SentencePreProcessor;
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.Ignore;
import org.junit.Test;

import org.nd4j.linalg.api.ndarray.INDArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Collection;

 * Created by agibsonccc on 12/3/14.
public class GloveTest {
    private static final Logger log = LoggerFactory.getLogger(GloveTest.class);
    private Glove glove;
    private SentenceIterator iter;

    public void before() throws Exception {

        ClassPathResource resource = new ClassPathResource("/raw_sentences.txt");
        File file = resource.getFile();
        iter = new LineSentenceIterator(file);
        iter.setPreProcessor(new SentencePreProcessor() {
            public String preProcess(String sentence) {
                return sentence.toLowerCase();


    public void testGlove() throws Exception {
        glove = new Glove.Builder().iterate(iter).symmetric(true).shuffle(true)

        Collection<String> words = glove.wordsNearest("day", 20);
        log.info("Nearest words to 'day': " + words);



    public void testGloVe1() throws Exception {
        File inputFile = new ClassPathResource("/big/raw_sentences.txt").getFile();

        SentenceIterator iter = new BasicLineIterator(inputFile.getAbsolutePath());
        // Split on white spaces in the line to get words
        TokenizerFactory t = new DefaultTokenizerFactory();
        t.setTokenPreProcessor(new CommonPreprocessor());

        Glove glove = new Glove.Builder()


        double simD = glove.similarity("day", "night");
        double simP = glove.similarity("best", "police");

        log.info("Day/night similarity: " + simD);
        log.info("Best/police similarity: " + simP);

        Collection<String> words = glove.wordsNearest("day", 10);
        log.info("Nearest words to 'day': " + words);

        assertTrue(simD > 0.7);

        // actually simP should be somewhere at 0
        assertTrue(simP < 0.5);


        File tempFile = File.createTempFile("glove", "temp");

        INDArray day1 = glove.getWordVectorMatrix("day").dup();

        WordVectorSerializer.writeWordVectors(glove, tempFile);

        WordVectors vectors = WordVectorSerializer.loadTxtVectors(tempFile);

        INDArray day2 = vectors.getWordVectorMatrix("day").dup();

        assertEquals(day1, day2);


Other Java examples (source code examples)

Here is a short list of links related to this Java GloveTest.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.