Example usage for org.deeplearning4j.models.embeddings.wordvectors WordVectors wordsNearest

List of usage examples for org.deeplearning4j.models.embeddings.wordvectors WordVectors wordsNearest

Introduction

In this page you can find the example usage for org.deeplearning4j.models.embeddings.wordvectors WordVectors wordsNearest.

Prototype

Collection<String> wordsNearest(String word, int n);

Source Link

Document

Get the top n words most similar to the given word

Usage

From source file:DL4J.java

License:Open Source License

public void runGoogleNews() throws IOException {
    File gFile = new File(getClass().getClassLoader().getResource("dataset").getFile()
            + "/GoogleNews-vectors-negative300.bin");
    WordVectors vec = WordVectorSerializer.loadGoogleModel(gFile, true);

    log.info("evaluate model...");
    double sim = vec.similarity("people", "money");
    log.info("Similarity between peeple and money: " + sim);
    Collection<String> similar = vec.wordsNearest("human", 10);
    log.info("Similarity words to 'human' : " + similar);
}

From source file:edu.polyu.comp5412.word2vec.Word2VecTestEng.java

public static void main(String[] args) throws Exception {
    WordVectors vec = WordVectorSerializer.loadTxtVectors(new File("gov-annc-vec.txt"));
    String[] testwords = new String[] { "police", "mtr", "typhoon", "economy" };
    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;//from ww  w .  j a  va  2 s.  c  om
            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);
        }
    }

    double[] wordVector = vec.getWordVector("typhoon");
    for (double d : wordVector) {
        System.out.print(d + ", ");
    }
    System.out.println();

    System.out.println(vec.wordsNearest(Arrays.asList("apple", "blue"), Arrays.asList("red"), 5));
    System.out.println(vec.wordsNearest(Arrays.asList("doctor", "school"), Arrays.asList("hospital"), 5));

}