Contents

MLWordEmbedding

A map of strings in a vector space that enable your app to find similar strings by looking at a string’s neighbors.

Declaration

struct MLWordEmbedding

Overview

Use an MLWordEmbedding 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 MLWordEmbedding, 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.

Topics

Creating a word embedding

Testing a word embedding

Saving a word embedding

Describing a word embedding

Default Implementations

See Also

Text models