Example usage for org.deeplearning4j.models.word2vec.wordstore.inmemory InMemoryLookupCache wordAtIndex

List of usage examples for org.deeplearning4j.models.word2vec.wordstore.inmemory InMemoryLookupCache wordAtIndex

Introduction

In this page you can find the example usage for org.deeplearning4j.models.word2vec.wordstore.inmemory InMemoryLookupCache wordAtIndex.

Prototype

@Override
public synchronized String wordAtIndex(int index) 

Source Link

Document

Returns the word contained at the given index or null

Usage

From source file:de.mpii.docsimilarity.mr.utils.io.WordVectorSerializer.java

License:Apache License

/**
 * Writes the word vectors to the given path. Note that this assumes an in memory cache
 *
 * @param lookupTable/*from  w  w  w .  j a va2  s  .c  om*/
 * @param cache
 *
 * @param path
 *            the path to write
 * @throws IOException
 */
public static void writeWordVectors(InMemoryLookupTable lookupTable, InMemoryLookupCache cache, String path)
        throws IOException {
    BufferedWriter write = new BufferedWriter(new FileWriter(new File(path), false));
    for (int i = 0; i < lookupTable.getSyn0().rows(); i++) {
        String word = cache.wordAtIndex(i);
        if (word == null) {
            continue;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(word.replaceAll(" ", "_"));
        sb.append(" ");
        INDArray wordVector = lookupTable.vector(word);
        for (int j = 0; j < wordVector.length(); j++) {
            sb.append(wordVector.getDouble(j));
            if (j < wordVector.length() - 1) {
                sb.append(" ");
            }
        }
        sb.append("\n");
        write.write(sb.toString());

    }

    write.flush();
    write.close();

}