<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "NaturalLanguage",
  "identifier" : "/documentation/NaturalLanguage/NLEmbedding",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Natural Language"
    ],
    "preciseIdentifier" : "c:objc(cs)NLEmbedding"
  },
  "title" : "NLEmbedding"
}
-->

# NLEmbedding

A map of strings to vectors, which locates neighboring, similar strings.

```
class NLEmbedding
```

## Overview

Use an [`NLEmbedding`](/documentation/NaturalLanguage/NLEmbedding) to find similar strings based on the proximity of their vectors. The *vocabulary* is the entire set of strings in an embedding. Each string in the vocabulary has a vector, which is an array of doubles, and each double corresponds to a dimension in the embedding. An [`NLEmbedding`](/documentation/NaturalLanguage/NLEmbedding) uses these vectors to determine the distance between two strings, or to find the nearest neighbors of a string in the vocabulary. The higher the similarity of any two strings, the smaller the distance is between them.

[`Natural Language`](/documentation/NaturalLanguage) provides built-in word embeddings that you can retrieve by using the [`wordEmbedding(for:)`](/documentation/NaturalLanguage/NLEmbedding/wordEmbedding(for:)) method. You can also compile your own custom embedding into an efficient, searchable, on-disk representation. Typically, you compile an embedding by using Create ML’s <doc://com.apple.documentation/documentation/CreateML/MLWordEmbedding> and save it as a file for your Xcode project at development time. Alternatively, you can compile an embedding at runtime by using Natural Language’s [`writeEmbeddingForDictionary:language:revision:toURL:error:`](/documentation/NaturalLanguage/NLEmbedding/writeEmbeddingForDictionary:language:revision:toURL:error:) method.

Your custom embedding can use any kind of string that’s useful to your app, such as phrases, brand names, serial numbers, and so on. For example, you could make an embedding of movie titles. Each movie title could have a vector that places similar movies close together in the embedding.

## Topics

### Creating a word embedding

[`wordEmbedding(for:)`](/documentation/NaturalLanguage/NLEmbedding/wordEmbedding(for:))

Retrieves a word embedding for a given language.

[`wordEmbedding(for:revision:)`](/documentation/NaturalLanguage/NLEmbedding/wordEmbedding(for:revision:))

Retrieves a word embedding for a given language and revision.

[`init(contentsOf:)`](/documentation/NaturalLanguage/NLEmbedding/init(contentsOf:))

Creates a word embedding from a model file.

### Creating a sentence embedding

[`sentenceEmbedding(for:)`](/documentation/NaturalLanguage/NLEmbedding/sentenceEmbedding(for:))

Retrieves a sentence embedding for a given language.

[`sentenceEmbedding(for:revision:)`](/documentation/NaturalLanguage/NLEmbedding/sentenceEmbedding(for:revision:))

Retrieves a sentence embedding for a given language and revision.

### Finding strings and their distances in an embedding

[`neighbors(for:maximumCount:distanceType:)`](/documentation/NaturalLanguage/NLEmbedding/neighbors(for:maximumCount:distanceType:)-8f1jc)

Retrieves a limited number of strings near a string in the vocabulary.

[`neighborsForString:maximumCount:distanceType:`](/documentation/NaturalLanguage/NLEmbedding/neighborsForString:maximumCount:distanceType:)

Retrieves a limited number of strings near a string in the vocabulary.

[`neighborsForString:maximumCount:maximumDistance:distanceType:`](/documentation/NaturalLanguage/NLEmbedding/neighborsForString:maximumCount:maximumDistance:distanceType:)

Retrieves a limited number of strings, within a radius of a string, in the vocabulary.

[`neighbors(for:maximumCount:distanceType:)`](/documentation/NaturalLanguage/NLEmbedding/neighbors(for:maximumCount:distanceType:)-8lp4z)

Retrieves a limited number of strings near a location in the vocabulary space.

[`neighborsForVector:maximumCount:distanceType:`](/documentation/NaturalLanguage/NLEmbedding/neighborsForVector:maximumCount:distanceType:)

Retrieves a limited number of strings near a location in the vocabulary space.

[`neighborsForVector:maximumCount:maximumDistance:distanceType:`](/documentation/NaturalLanguage/NLEmbedding/neighborsForVector:maximumCount:maximumDistance:distanceType:)

Retrieves a limited number of strings within a radius of a location in the vocabulary space.

[`enumerateNeighbors(for:maximumCount:distanceType:using:)`](/documentation/NaturalLanguage/NLEmbedding/enumerateNeighbors(for:maximumCount:distanceType:using:)-72jda)

Passes the nearest strings of a string in the vocabulary to a closure.

[`enumerateNeighborsForString:maximumCount:distanceType:usingBlock:`](/documentation/NaturalLanguage/NLEmbedding/enumerateNeighborsForString:maximumCount:distanceType:usingBlock:)

Passes the nearest strings of a string in the vocabulary to a block.

[`enumerateNeighborsForString:maximumCount:maximumDistance:distanceType:usingBlock:`](/documentation/NaturalLanguage/NLEmbedding/enumerateNeighborsForString:maximumCount:maximumDistance:distanceType:usingBlock:)

Passes the nearest strings, within a radius of a string in the vocabulary, to a block.

[`enumerateNeighbors(for:maximumCount:distanceType:using:)`](/documentation/NaturalLanguage/NLEmbedding/enumerateNeighbors(for:maximumCount:distanceType:using:)-6dy4x)

Passes the nearest strings of a location in the vocabulary space to a closure.

[`enumerateNeighborsForVector:maximumCount:distanceType:usingBlock:`](/documentation/NaturalLanguage/NLEmbedding/enumerateNeighborsForVector:maximumCount:distanceType:usingBlock:)

Passes the nearest strings of a location in the vocabulary space to a closure.

[`enumerateNeighborsForVector:maximumCount:maximumDistance:distanceType:usingBlock:`](/documentation/NaturalLanguage/NLEmbedding/enumerateNeighborsForVector:maximumCount:maximumDistance:distanceType:usingBlock:)

Passes the nearest strings, within a radius of a location in the vocabulary space, to a block.

[`distance(between:and:distanceType:)`](/documentation/NaturalLanguage/NLEmbedding/distance(between:and:distanceType:))

Calculates the distance between two strings in the vocabulary space.

[`distanceBetweenString:andString:distanceType:`](/documentation/NaturalLanguage/NLEmbedding/distanceBetweenString:andString:distanceType:)

Calculates the distance between two strings in the vocabulary space.

[`NLDistance`](/documentation/NaturalLanguage/NLDistance)

The distance between two strings in a text embedding.

### Inspecting the vocabulary of an embedding

[`dimension`](/documentation/NaturalLanguage/NLEmbedding/dimension)

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

[`vocabularySize`](/documentation/NaturalLanguage/NLEmbedding/vocabularySize)

The number of words in the vocabulary.

[`language`](/documentation/NaturalLanguage/NLEmbedding/language)

The language of the text in the word embedding.

[`contains(_:)`](/documentation/NaturalLanguage/NLEmbedding/contains(_:))

Requests a Boolean value that indicates whether the term is in the vocabulary.

[`vector(for:)`](/documentation/NaturalLanguage/NLEmbedding/vector(for:))

Requests the vector for the given term.

[`vectorForString:`](/documentation/NaturalLanguage/NLEmbedding/vectorForString:)

Requests the vector for the given term.

[`getVector:forString:`](/documentation/NaturalLanguage/NLEmbedding/getVector:forString:)

Copies a vector into the given a pointer to a float array.

[`revision`](/documentation/NaturalLanguage/NLEmbedding/revision)

The revision of the word embedding.

### Saving an embedding

[`write(_:language:revision:to:)`](/documentation/NaturalLanguage/NLEmbedding/write(_:language:revision:to:))

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

[`writeEmbeddingForDictionary:language:revision:toURL:error:`](/documentation/NaturalLanguage/NLEmbedding/writeEmbeddingForDictionary:language:revision:toURL:error:)

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

### Checking for Natural Language support

[`currentRevision(for:)`](/documentation/NaturalLanguage/NLEmbedding/currentRevision(for:))

Retrieves the current version of a word embedding for the given language.

[`supportedRevisions(for:)`](/documentation/NaturalLanguage/NLEmbedding/supportedRevisions(for:))

Retrieves all version numbers of a word embedding for the given language.

[`currentSentenceEmbeddingRevision(for:)`](/documentation/NaturalLanguage/NLEmbedding/currentSentenceEmbeddingRevision(for:))

Retrieves the current version of a sentence embedding for the given language.

[`supportedSentenceEmbeddingRevisions(for:)`](/documentation/NaturalLanguage/NLEmbedding/supportedSentenceEmbeddingRevisions(for:))

Retrieves all version numbers of a sentence embedding for the given language.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)