Main Content

writeWordEmbedding

Write word embedding file

Description

example

writeWordEmbedding(emb,filename)writes the word embeddingembto the filefilename. The function writes the vocabulary in UTF-8 in word2vec text format.

Examples

collapse all

Train a word embedding and write it to a text file.

Load the example data. The filesonnetsPreprocessed.txtcontains preprocessed versions of Shakespeare's sonnets. The file contains one sonnet per line, with words separated by a space. Extract the text fromsonnetsPreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename ="sonnetsPreprocessed.txt"; str = extractFileText(filename); textData = split(str,newline); documents = tokenizedDocument(textData);

Train a word embedding usingtrainWordEmbedding.

emb = trainWordEmbedding(documents)
Training: 100% Loss: 2.70886 Remaining time: 0 hours 0 minutes.
emb = wordEmbedding with properties: Dimension: 100 Vocabulary: ["thy" "thou" "love" "thee" "doth" ... ]

Write the word embedding to a text file.

filename ="exampleSonnetsEmbedding.vec"; writeWordEmbedding(emb,filename)

Read the word embedding file usingreadWordEmbedding.

emb = readWordEmbedding(filename)
emb = wordEmbedding with properties: Dimension: 100 Vocabulary: ["thy" "thou" "love" "thee" "doth" ... ]

Input Arguments

collapse all

Input word embedding, specified as awordEmbeddingobject.

Name of the file, specified as a string scalar or character vector.

Data Types:string|char

Version History

Introduced in R2017b