Creates and saves a map of strings to vectors, enabling your app to find neighboring, similar strings.
SDKs
- macOS 10.15+
- Xcode 11.0+
Framework
- Create ML
Declaration
struct MLWordEmbedding
Overview
Use an MLWord
to configure and save a word embedding to a file, which you then add to your project in Xcode. Your project uses that word embedding file at runtime to create an NLEmbedding
instance, which finds similar strings based on the proximity of their vectors.
You configure a word embedding with a dictionary, keyed by strings which make up the vocabulary of the word embedding. The value for each string is an array of doubles, which represents a vector. The length of the arrays is arbitrary but all arrays in a word embedding must be the same length. The length of the arrays determine the number of dimensions in the vector space. For example, the following listing creates a word embedding with four dimensions and a vocabulary of two strings.
let wordEmbedding = try! MLWordEmbedding(dictionary: [
"Hello" : [0.0, 1.2, 5.0, 0.0],
"Goodbye" : [0.0, 1.3, -6.2, 0.1]
])
Once you’ve configured an MLWord
, save it to an .mlmodel
file to include in your app.
try wordEmbedding.write(toFile: "~/Desktop/WordEmbedding.mlmodel")
A word embedding file can efficiently store many strings and their vectors.