Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package edu.polyu.comp5412.word2vec; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer; import org.deeplearning4j.models.embeddings.wordvectors.WordVectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author mungchau */ public class Word2VecTestChi { private static Logger log = LoggerFactory.getLogger(Word2VecTestChi.class); public static void main(String[] args) throws Exception { WordVectors vec = WordVectorSerializer.loadTxtVectors(new File("poem-vec.txt")); Collection<String> words = vec.wordsNearest(Arrays.asList("", ""), Arrays.asList(""), 5); System.out.println(words); words = vec.wordsNearest(Arrays.asList("", ""), Arrays.asList(""), 5); System.out.println(words); String[] testwords = new String[] { "", "", "", "", "" }; for (String s : testwords) { Collection<String> lst = vec.wordsNearest(s, 5); List<SimWord> simwords = new ArrayList(); for (String w : lst) { SimWord sw = new SimWord(); sw.word = w; sw.similarlity = vec.similarity(s, w); simwords.add(sw); } System.out.println(s); System.out.println("====================="); for (SimWord sw : simwords) { System.out.println(sw.word + "\t" + sw.similarlity); } } } static class SimWord { String word; double similarlity; } }