Structure

MLWordEmbedding

Creates and saves a map of strings to vectors, enabling your app to find neighboring, similar strings.

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

init(dictionary: [String : [Double]], parameters: MLWordEmbedding.ModelParameters)

Creates a word embedding from a set of strings and their vectors provided with the given dictionary.

struct MLWordEmbedding.ModelParameters

Parameters that configure a word embedding.

let modelParameters: MLWordEmbedding.ModelParameters

The configuration parameters that the word embedding used during initialization.

Testing a Word Embedding

func distance(between: String, and: String, distanceType: NLDistanceType) -> Double

Requests the distance between two strings given a distance calculation method.

enum NLDistanceType

The means of calculating a distance between two locations in a text embedding.

func contains(String) -> Bool

Queries whether the given string is in the vocabulary.

func vector(for: String) -> [Double]?

Retrieves the vector of the string.

Saving a Word Embedding

func write(to: URL, metadata: MLModelMetadata?)

Exports the word embedding contained within a Core ML model file at the given URL.

func write(toFile: String, metadata: MLModelMetadata?)

Exports the word embedding contained within a Core ML model file at the given file path.

Describing a Word Embedding

let dimension: Int

The number of dimensions in the vocabulary’s vector space.

let vocabularySize: Int

The number of strings in the vocabulary.

var model: MLModel

The word embedding contained within a Core ML model file stored in memory.

var description: String

A text representation of the word embedding.

var debugDescription: String

A text representation of the word embedding that’s suitable for output during debugging.

var playgroundDescription: Any

A description of the word embedding shown in a playground.

See Also

Natural Language Processing

Creating a Text Classifier Model

Train a machine learning model to classify natural language text.

struct MLTextClassifier

A model you train to classify natural language text.

struct MLWordTagger

A word tagging model you train to classify natural language text at the word level.

struct MLGazetteer

A collection of terms and their labels, which augments a tagger that analyzes natural language text.