public class WordVectorSerializer extends Object
Modifier and Type | Class and Description |
---|---|
protected static class |
WordVectorSerializer.BinaryReader |
protected static class |
WordVectorSerializer.CSVReader |
protected static interface |
WordVectorSerializer.Reader |
Modifier and Type | Method and Description |
---|---|
static String |
decodeB64(String word) |
static String |
encodeB64(String word) |
static Word2Vec |
fromPair(org.nd4j.linalg.primitives.Pair<InMemoryLookupTable,VocabCache> pair)
Load word vectors from the given pair
|
static WordVectors |
fromTableAndVocab(WeightLookupTable table,
VocabCache vocab)
Load word vectors for the given vocab and table
|
static float |
getFloat(byte[] b)
Read a string from a data input stream Credit to:
https://github.com/NLPchina/Word2VEC_java/blob/master/src/com/ansj/vec/Word2VEC.java
|
protected static TokenizerFactory |
getTokenizerFactory(VectorsConfiguration configuration) |
static Word2Vec |
loadFullModel(String path)
Deprecated.
Use readWord2VecModel() or loadStaticModel() method instead
|
static WordVectors |
loadStaticModel(File file) |
static WordVectors |
loadStaticModel(InputStream inputStream)
This method restores previously saved w2v model.
|
static org.nd4j.linalg.primitives.Pair<InMemoryLookupTable,VocabCache> |
loadTxt(File vectorsFile)
Loads an in memory cache from the given path (sets syn0 and the vocab)
|
static WordVectors |
loadTxtVectors(File vectorsFile)
Deprecated.
|
static WordVectors |
loadTxtVectors(InputStream stream,
boolean skipFirstLine)
Deprecated.
Use readWord2VecModel() or loadStaticModel() method instead
|
static void |
printOutProjectedMemoryUse(long numWords,
int vectorLength,
int numTables) |
static Word2Vec |
readAsBinary(File file) |
static Word2Vec |
readAsBinaryNoLineBreaks(File file) |
static Word2Vec |
readAsCsv(File file) |
static Word2Vec |
readBinaryModel(File modelFile,
boolean linebreaks,
boolean normalize)
Read a binary word2vec file.
|
static float |
readFloat(InputStream is)
Read a float from a data input stream Credit to:
https://github.com/NLPchina/Word2VEC_java/blob/master/src/com/ansj/vec/Word2VEC.java
|
static <T extends SequenceElement> |
readLookupTable(File file) |
static <T extends SequenceElement> |
readLookupTable(InputStream stream) |
static ParagraphVectors |
readParagraphVectors(File file)
This method restores ParagraphVectors model previously saved with writeParagraphVectors()
|
static ParagraphVectors |
readParagraphVectors(InputStream stream)
This method restores ParagraphVectors model previously saved with writeParagraphVectors()
|
static ParagraphVectors |
readParagraphVectors(String path)
This method restores ParagraphVectors model previously saved with writeParagraphVectors()
|
static ParagraphVectors |
readParagraphVectorsFromText(File file)
Deprecated.
Use readParagraphVectors() method instead
|
static ParagraphVectors |
readParagraphVectorsFromText(InputStream stream)
Deprecated.
Use readParagraphVectors() method instead
|
static ParagraphVectors |
readParagraphVectorsFromText(String path)
Deprecated.
Use readParagraphVectors() method instead
|
static <T extends SequenceElement> |
readSequenceVectors(File file,
boolean readExtendedTables) |
static <T extends SequenceElement> |
readSequenceVectors(InputStream stream,
boolean readExtendedTables) |
static <T extends SequenceElement> |
readSequenceVectors(SequenceElementFactory<T> factory,
File file)
This method loads previously saved SequenceVectors model from File
|
static <T extends SequenceElement> |
readSequenceVectors(SequenceElementFactory<T> factory,
InputStream stream)
This method loads previously saved SequenceVectors model from InputStream
|
static <T extends SequenceElement> |
readSequenceVectors(String path,
boolean readExtendedTables) |
static String |
readString(DataInputStream dis)
Read a string from a data input stream Credit to:
https://github.com/NLPchina/Word2VEC_java/blob/master/src/com/ansj/vec/Word2VEC.java
|
static VocabCache<VocabWord> |
readVocabCache(File file)
This method reads vocab cache from provided file.
|
static VocabCache<VocabWord> |
readVocabCache(InputStream stream)
This method reads vocab cache from provided InputStream.
|
static Word2Vec |
readWord2Vec(File file)
Deprecated.
|
static Word2Vec |
readWord2Vec(File file,
boolean readExtendedTables) |
static Word2Vec |
readWord2Vec(InputStream stream,
boolean readExtendedTable) |
static Word2Vec |
readWord2Vec(String path,
boolean readExtendedTables) |
static Word2Vec |
readWord2VecFromText(File vectors,
File hs,
File h_codes,
File h_points,
VectorsConfiguration configuration)
This method allows you to read ParagraphVectors from externally originated vectors and syn1.
|
static Word2Vec |
readWord2VecModel(File file)
This method
1) Binary model, either compressed or not.
|
static Word2Vec |
readWord2VecModel(File file,
boolean extendedModel)
This method
1) Binary model, either compressed or not.
|
static Word2Vec |
readWord2VecModel(String path)
This method
1) Binary model, either compressed or not.
|
static Word2Vec |
readWord2VecModel(String path,
boolean extendedModel)
This method
1) Binary model, either compressed or not.
|
static void |
writeFullModel(Word2Vec vec,
String path)
Deprecated.
Use writeWord2VecModel() method instead
|
static <T extends SequenceElement> |
writeLookupTable(WeightLookupTable<T> weightLookupTable,
File file) |
static void |
writeParagraphVectors(ParagraphVectors vectors,
File file)
This method saves ParagraphVectors model into compressed zip file
|
static void |
writeParagraphVectors(ParagraphVectors vectors,
OutputStream stream)
This method saves ParagraphVectors model into compressed zip file and sends it to output stream
|
static void |
writeParagraphVectors(ParagraphVectors vectors,
String path)
This method saves ParagraphVectors model into compressed zip file located at path
|
static <T extends SequenceElement> |
writeSequenceVectors(SequenceVectors<T> vectors,
OutputStream stream) |
static <T extends SequenceElement> |
writeSequenceVectors(SequenceVectors<T> vectors,
SequenceElementFactory<T> factory,
File file)
This method saves specified SequenceVectors model to target file
|
static <T extends SequenceElement> |
writeSequenceVectors(SequenceVectors<T> vectors,
SequenceElementFactory<T> factory,
OutputStream stream)
This method saves specified SequenceVectors model to target OutputStream
|
static <T extends SequenceElement> |
writeSequenceVectors(SequenceVectors<T> vectors,
SequenceElementFactory<T> factory,
String path)
This method saves specified SequenceVectors model to target file path
|
static void |
writeTsneFormat(Glove vec,
org.nd4j.linalg.api.ndarray.INDArray tsne,
File csv)
Write the tsne format
|
static void |
writeTsneFormat(Word2Vec vec,
org.nd4j.linalg.api.ndarray.INDArray tsne,
File csv)
Write the tsne format
|
static void |
writeVocabCache(VocabCache<VocabWord> vocabCache,
File file)
This method saves vocab cache to provided File.
|
static void |
writeVocabCache(VocabCache<VocabWord> vocabCache,
OutputStream stream)
This method saves vocab cache to provided OutputStream.
|
static void |
writeWord2Vec(Word2Vec word2Vec,
OutputStream stream) |
static void |
writeWord2VecModel(Word2Vec vectors,
File file)
This method saves Word2Vec model into compressed zip file and sends it to output stream
PLEASE NOTE: This method saves FULL model, including syn0 AND syn1
|
static void |
writeWord2VecModel(Word2Vec vectors,
OutputStream stream)
This method saves Word2Vec model into compressed zip file and sends it to output stream
PLEASE NOTE: This method saves FULL model, including syn0 AND syn1
|
static void |
writeWord2VecModel(Word2Vec vectors,
String path)
This method saves Word2Vec model into compressed zip file and sends it to output stream
PLEASE NOTE: This method saves FULL model, including syn0 AND syn1
|
static void |
writeWordVectors(Glove vectors,
File file)
This method saves GloVe model to the given output stream.
|
static void |
writeWordVectors(Glove vectors,
OutputStream stream)
This method saves GloVe model to the given OutputStream
|
static void |
writeWordVectors(Glove vectors,
String path)
This method saves GloVe model to the given output stream.
|
static void |
writeWordVectors(InMemoryLookupTable lookupTable,
InMemoryLookupCache cache,
String path)
Deprecated.
Use
writeWord2VecModel(Word2Vec, File) instead |
static void |
writeWordVectors(ParagraphVectors vectors,
File path)
Deprecated.
|
static void |
writeWordVectors(ParagraphVectors vectors,
OutputStream stream)
Deprecated.
|
static void |
writeWordVectors(ParagraphVectors vectors,
String path)
Deprecated.
|
static <T extends SequenceElement> |
writeWordVectors(WeightLookupTable<T> lookupTable,
File file)
This method writes word vectors to the given file.
|
static <T extends SequenceElement> |
writeWordVectors(WeightLookupTable<T> lookupTable,
OutputStream stream)
This method writes word vectors to the given OutputStream.
|
static <T extends SequenceElement> |
writeWordVectors(WeightLookupTable<T> lookupTable,
String path)
This method writes word vectors to the given path.
|
static void |
writeWordVectors(Word2Vec vec,
BufferedWriter writer)
Deprecated.
|
static void |
writeWordVectors(Word2Vec vec,
File file)
Deprecated.
|
static void |
writeWordVectors(Word2Vec vec,
OutputStream outputStream)
Deprecated.
|
static void |
writeWordVectors(Word2Vec vec,
String path)
Deprecated.
|
public static Word2Vec readBinaryModel(File modelFile, boolean linebreaks, boolean normalize) throws NumberFormatException, IOException
modelFile
- the File to readlinebreaks
- if true, the reader expects each word/vector to be in a separate line, terminated
by a line breakmodel
NumberFormatException
IOException
FileNotFoundException
public static float readFloat(InputStream is) throws IOException
is
- IOException
public static float getFloat(byte[] b)
b
- IOException
public static String readString(DataInputStream dis) throws IOException
dis
- IOException
public static <T extends SequenceElement> void writeWordVectors(WeightLookupTable<T> lookupTable, String path) throws IOException
T
- lookupTable
- path
- IOException
public static <T extends SequenceElement> void writeWordVectors(WeightLookupTable<T> lookupTable, File file) throws IOException
T
- lookupTable
- file
- IOException
public static <T extends SequenceElement> void writeWordVectors(WeightLookupTable<T> lookupTable, OutputStream stream) throws IOException
T
- lookupTable
- stream
- IOException
@Deprecated public static void writeWordVectors(@NonNull ParagraphVectors vectors, @NonNull File path)
vectors
- path
- @Deprecated public static void writeWordVectors(@NonNull ParagraphVectors vectors, @NonNull String path)
vectors
- path
- public static void writeParagraphVectors(ParagraphVectors vectors, File file)
file
- public static void writeParagraphVectors(ParagraphVectors vectors, String path)
path
- public static void writeWord2VecModel(Word2Vec vectors, File file)
public static void writeWord2VecModel(Word2Vec vectors, String path)
public static void writeWord2VecModel(Word2Vec vectors, OutputStream stream) throws IOException
IOException
public static void writeParagraphVectors(ParagraphVectors vectors, OutputStream stream) throws IOException
IOException
public static ParagraphVectors readParagraphVectors(String path) throws IOException
IOException
public static ParagraphVectors readParagraphVectors(File file) throws IOException
IOException
@Deprecated public static Word2Vec readWord2Vec(File file) throws IOException
PLEASE NOTE: This method loads FULL model, so don't use it if you're only going to use weights.
file
- IOException
public static ParagraphVectors readParagraphVectors(InputStream stream) throws IOException
IOException
public static Word2Vec readWord2VecFromText(@NonNull File vectors, @NonNull File hs, @NonNull File h_codes, @NonNull File h_points, @NonNull VectorsConfiguration configuration) throws IOException
vectors
- text file with words and their weights, aka Syn0hs
- text file HS layers, aka Syn1h_codes
- text file with Huffman tree codesh_points
- text file with Huffman tree pointsIOException
@Deprecated public static ParagraphVectors readParagraphVectorsFromText(@NonNull String path)
Deprecation note: Please, consider using readParagraphVectors() method instead
path
- Path to file that contains previously serialized model@Deprecated public static ParagraphVectors readParagraphVectorsFromText(@NonNull File file)
Deprecation note: Please, consider using readParagraphVectors() method instead
file
- File that contains previously serialized model@Deprecated public static ParagraphVectors readParagraphVectorsFromText(@NonNull InputStream stream)
Deprecation note: Please, consider using readParagraphVectors() method instead
stream
- InputStream that contains previously serialized modelpublic static void writeWordVectors(@NonNull Glove vectors, @NonNull File file)
vectors
- GloVe model to be savedfile
- path where model should be saved topublic static void writeWordVectors(@NonNull Glove vectors, @NonNull String path)
vectors
- GloVe model to be savedpath
- path where model should be saved topublic static void writeWordVectors(@NonNull Glove vectors, @NonNull OutputStream stream)
vectors
- GloVe model to be savedstream
- OutputStream where model should be saved to@Deprecated public static void writeWordVectors(ParagraphVectors vectors, OutputStream stream)
vectors
- stream
- @Deprecated public static void writeWordVectors(InMemoryLookupTable lookupTable, InMemoryLookupCache cache, String path) throws IOException
writeWord2VecModel(Word2Vec, File)
insteadlookupTable
- cache
- path
- the path to writeIOException
@Deprecated public static void writeFullModel(@NonNull Word2Vec vec, @NonNull String path)
Deprecation note: Please, consider using writeWord2VecModel() method instead
vec
- - The Word2Vec instance to be savedpath
- - the path for json to be saved@Deprecated public static Word2Vec loadFullModel(@NonNull String path) throws FileNotFoundException
Deprecation note: Please, consider using readWord2VecModel() or loadStaticModel() method instead
path
- - path to previously stored w2v json modelFileNotFoundException
@Deprecated public static void writeWordVectors(@NonNull Word2Vec vec, @NonNull String path) throws IOException
vec
- the word2vec to writepath
- the path to writeIOException
@Deprecated public static void writeWordVectors(@NonNull Word2Vec vec, @NonNull File file) throws IOException
vec
- the word2vec to writefile
- the file to writeIOException
@Deprecated public static void writeWordVectors(@NonNull Word2Vec vec, @NonNull OutputStream outputStream) throws IOException
vec
- the word2vec to writeoutputStream
- - OutputStream, where all data should be sent to
the path to writeIOException
@Deprecated public static void writeWordVectors(@NonNull Word2Vec vec, @NonNull BufferedWriter writer) throws IOException
vec
- the word2vec to writewriter
- - BufferedWriter, where all data should be written to
the path to writeIOException
public static WordVectors fromTableAndVocab(WeightLookupTable table, VocabCache vocab)
table
- the weights to usevocab
- the vocab to usepublic static Word2Vec fromPair(org.nd4j.linalg.primitives.Pair<InMemoryLookupTable,VocabCache> pair)
pair
- the given pair@Deprecated public static WordVectors loadTxtVectors(File vectorsFile) throws IOException
Deprecation note: Please, consider using readWord2VecModel() or loadStaticModel() method instead
vectorsFile
- the path of the file to load\FileNotFoundException
- if the file does not existIOException
public static org.nd4j.linalg.primitives.Pair<InMemoryLookupTable,VocabCache> loadTxt(File vectorsFile) throws IOException, UnsupportedEncodingException
vectorsFile
- the path of the file to loadFileNotFoundException
- if the input file does not existIOException
UnsupportedEncodingException
@Deprecated public static WordVectors loadTxtVectors(@NonNull InputStream stream, boolean skipFirstLine) throws IOException
Deprecation note: Please, consider using readWord2VecModel() or loadStaticModel() method instead
stream
- InputStream that contains previously serialized modelskipFirstLine
- Set this TRUE if first line contains csv header, FALSE otherwiseIOException
public static void writeTsneFormat(Glove vec, org.nd4j.linalg.api.ndarray.INDArray tsne, File csv) throws Exception
vec
- the word vectors to use for labelingtsne
- the tsne array to writecsv
- the file to useException
public static void writeTsneFormat(Word2Vec vec, org.nd4j.linalg.api.ndarray.INDArray tsne, File csv) throws Exception
vec
- the word vectors to use for labelingtsne
- the tsne array to writecsv
- the file to useException
public static <T extends SequenceElement> void writeSequenceVectors(@NonNull SequenceVectors<T> vectors, @NonNull SequenceElementFactory<T> factory, @NonNull String path) throws IOException
T
- vectors
- SequenceVectors modelfactory
- SequenceElementFactory implementation for your objectspath
- Target output file pathIOException
public static <T extends SequenceElement> void writeSequenceVectors(@NonNull SequenceVectors<T> vectors, @NonNull SequenceElementFactory<T> factory, @NonNull File file) throws IOException
T
- vectors
- SequenceVectors modelfactory
- SequenceElementFactory implementation for your objectsfile
- Target output fileIOException
public static <T extends SequenceElement> void writeSequenceVectors(@NonNull SequenceVectors<T> vectors, @NonNull SequenceElementFactory<T> factory, @NonNull OutputStream stream) throws IOException
T
- vectors
- SequenceVectors modelfactory
- SequenceElementFactory implementation for your objectsstream
- Target output streamIOException
public static <T extends SequenceElement> void writeSequenceVectors(@NonNull SequenceVectors<T> vectors, @NonNull OutputStream stream) throws IOException
IOException
public static <T extends SequenceElement> SequenceVectors<T> readSequenceVectors(@NonNull String path, boolean readExtendedTables) throws IOException
IOException
public static <T extends SequenceElement> SequenceVectors<T> readSequenceVectors(@NonNull File file, boolean readExtendedTables) throws IOException
IOException
public static <T extends SequenceElement> SequenceVectors<T> readSequenceVectors(@NonNull InputStream stream, boolean readExtendedTables) throws IOException
IOException
public static <T extends SequenceElement> SequenceVectors<T> readSequenceVectors(@NonNull SequenceElementFactory<T> factory, @NonNull File file) throws IOException
T
- factory
- file
- IOException
public static <T extends SequenceElement> SequenceVectors<T> readSequenceVectors(@NonNull SequenceElementFactory<T> factory, @NonNull InputStream stream) throws IOException
T
- factory
- stream
- IOException
public static void writeVocabCache(@NonNull VocabCache<VocabWord> vocabCache, @NonNull File file) throws IOException
vocabCache
- file
- UnsupportedEncodingException
IOException
public static void writeVocabCache(@NonNull VocabCache<VocabWord> vocabCache, @NonNull OutputStream stream) throws IOException
vocabCache
- stream
- UnsupportedEncodingException
IOException
public static VocabCache<VocabWord> readVocabCache(@NonNull File file) throws IOException
file
- IOException
public static VocabCache<VocabWord> readVocabCache(@NonNull InputStream stream) throws IOException
stream
- IOException
public static Word2Vec readWord2VecModel(@NonNull File file)
Please note: Only weights will be loaded by this method.
file
- public static Word2Vec readWord2VecModel(String path)
Please note: Only weights will be loaded by this method.
path
- public static Word2Vec readWord2VecModel(String path, boolean extendedModel)
Please note: if extended data isn't available, only weights will be loaded instead.
path
- extendedModel
- if TRUE, we'll try to load HS states & Huffman tree info, if FALSE, only weights will be loadedpublic static Word2Vec readWord2VecModel(@NonNull File file, boolean extendedModel)
Please note: if extended data isn't available, only weights will be loaded instead.
file
- extendedModel
- if TRUE, we'll try to load HS states & Huffman tree info, if FALSE, only weights will be loadedprotected static TokenizerFactory getTokenizerFactory(VectorsConfiguration configuration)
public static WordVectors loadStaticModel(InputStream inputStream) throws IOException
file
- File should point to previously saved w2v modelIOException
public static WordVectors loadStaticModel(@NonNull File file)
public static void writeWord2Vec(@NonNull Word2Vec word2Vec, @NonNull OutputStream stream) throws IOException
IOException
public static Word2Vec readWord2Vec(@NonNull String path, boolean readExtendedTables) throws IOException
IOException
public static <T extends SequenceElement> void writeLookupTable(WeightLookupTable<T> weightLookupTable, @NonNull File file) throws IOException
IOException
public static <T extends SequenceElement> WeightLookupTable<T> readLookupTable(File file) throws IOException
IOException
public static <T extends SequenceElement> WeightLookupTable<T> readLookupTable(InputStream stream) throws IOException
IOException
public static Word2Vec readWord2Vec(@NonNull File file, boolean readExtendedTables) throws IOException
IOException
public static Word2Vec readWord2Vec(@NonNull InputStream stream, boolean readExtendedTable) throws IOException
IOException
public static void printOutProjectedMemoryUse(long numWords, int vectorLength, int numTables)
Copyright © 2019. All rights reserved.