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

Java example source code file (Word2VecTestsSmall.java)

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

adam, before, classpathresource, collection, exception, string, test, util, word2vectestssmall, wordvectors

The Word2VecTestsSmall.java Java example source code

package org.deeplearning4j.models.word2vec;

import static org.junit.Assert.assertEquals;

import java.util.Collection;

import org.canova.api.util.ClassPathResource;
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
import org.junit.Before;
import org.junit.Test;


public class Word2VecTestsSmall
{
    WordVectors word2vec;

    @Before
    public void setUp()
        throws Exception
    {
        word2vec = WordVectorSerializer.loadGoogleModel(
                new ClassPathResource("vec.bin").getFile(), true,true);
    }

    @Test
    public void testWordsNearest2VecTxt()
    {
        String word = "Adam";
        String expectedNeighbour = "is";
        int neighbours = 1;

        Collection<String> nearestWords = word2vec.wordsNearest(word, neighbours);
        System.out.println(nearestWords);
        assertEquals(expectedNeighbour, nearestWords.iterator().next());
    }

    @Test
    public void testWordsNearest2NNeighbours()
    {
        String word = "Adam";
        int neighbours = 2;

        Collection<String> nearestWords = word2vec.wordsNearest(word, neighbours);
        System.out.println(nearestWords);
        assertEquals(neighbours, nearestWords.size());

    }

    @Test
    public void testPlot()
    {
        //word2vec.lookupTable().plotVocab();
    }
}

Other Java examples (source code examples)

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