Example usage for org.deeplearning4j.models.embeddings.loader WordVectorSerializer readWord2VecModel

List of usage examples for org.deeplearning4j.models.embeddings.loader WordVectorSerializer readWord2VecModel

Introduction

In this page you can find the example usage for org.deeplearning4j.models.embeddings.loader WordVectorSerializer readWord2VecModel.

Prototype

public static Word2Vec readWord2VecModel(String path) 

Source Link

Document

This method 1) Binary model, either compressed or not.

Usage

From source file:net.liaocy.ml4j.nlp.word2vec.Predict.java

private void load(String modelName) throws IOException {
    MongoDatabase db = Mongo.getDB();//from w w  w. j a  va 2 s .c  om
    GridFSBucket gridFSBucket = GridFSBuckets.create(db, "word2vecmodels");
    File file = File.createTempFile(modelName, ".w2v");
    OutputStream os = new FileOutputStream(file);
    gridFSBucket.downloadToStreamByName(modelName, os);
    os.close();
    this.vec = WordVectorSerializer.readWord2VecModel(file);
    //        System.out.println(file.getAbsolutePath());
    if (!file.delete()) {
        file.deleteOnExit();
    }
}

From source file:org.knime.ext.textprocessing.dl4j.util.WordVectorPortObjectUtils.java

License:Open Source License

/**
 * Reads {@link WordVectors} from the specified {@link ZipInputStream}. The method expects the ZipInputStream to
 * contain an ZipEntry with name "word_vectors", which contains the WordVector model to load.
 *
 * @param in stream to read from//from w w w  .  j  a  v a2s  .c  o  m
 * @param mode the type of WordVector model to expect
 * @return {@link WordVectors} loaded from stream
 * @throws IOException
 */
public static WordVectors loadWordVectors(final ZipInputStream in, final WordVectorTrainingMode mode)
        throws IOException {
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
        if (entry.getName().matches("word_vectors")) {
            switch (mode) {
            case DOC2VEC:
                return WordVectorSerializer.readParagraphVectors(in);
            case WORD2VEC:
                /* Need to copy stream to temp file because API does not support Word2VecModel reading
                 * with InputStreams. */
                Word2Vec model = null;
                File tmp = null;
                try {
                    tmp = copyInputStreamToTmpFile(in);
                    model = WordVectorSerializer.readWord2VecModel(tmp);
                } catch (Exception e) {
                    throw e;
                } finally {
                    if (tmp != null && tmp.exists()) {
                        tmp.delete();
                    }
                }

                return model;
            default:
                throw new IllegalStateException(
                        "No deserialization method defined for WordVectors of type: " + mode);
            }
        }
    }
    throw new IllegalArgumentException(
            "WordVectors entry not found. ZipInputStream seems not to contain ZipEntry "
                    + "with name 'word_vectors'!");
}

From source file:org.knime.ext.textprocessing.dl4j.util.WordVectorPortObjectUtils.java

License:Open Source License

/**
 * Reads {@link WordVectors} from the specified {@link URL}.
 *
 * @param url the URL to read from//ww  w. java  2 s.com
 * @param mode the type of WordVector model to expect
 * @return {@link WordVectors} loaded from URL
 * @throws IOException
 * @throws URISyntaxException
 */
public static WordVectors loadWordVectors(final URL url, final WordVectorTrainingMode mode)
        throws IOException, URISyntaxException {
    switch (mode) {
    case DOC2VEC:
        return WordVectorSerializer.readParagraphVectors(url.openStream());
    case WORD2VEC:
        boolean isLocalFile = FileUtil.resolveToPath(url) != null;
        File wvFile = null;
        try {
            wvFile = isLocalFile ? FileUtil.getFileFromURL(url) : copyURLToTmpFile(url);
            return WordVectorSerializer.readWord2VecModel(wvFile);
        } catch (Exception e) {
            // error is handled outside
            throw e;
        } finally {
            // file has been temporarily downloaded
            if (!isLocalFile && wvFile != null) {
                wvFile.delete();
            }
        }

    default:
        throw new IllegalStateException("No deserialization method defined for WordVectors of type: " + mode);
    }
}