Swift version: 5.10
iOS gives us the ability to search for similar words for a term by using word embeddings, which are maps of strings created using machine learning that describe how similar various words are in terms of their meaning. This kind of thing is useful when handling user searches: you might have tagged a photo with “hat”, but your user searched for “sombrero” – word embeddings let us find similar words, and we can then use those variations for data searches.
To get started, first add import NaturalLanguage
, then create a word embedding for the language you want to target:
let embedding = NLEmbedding.wordEmbedding(for: .english)
That returns an optional NLEmbedding
, because the language you requested might not be supported. For example, right now .english
, .french
, and some others work, but .german
does not.
Once you have your embedding, you can request all similar words for a given string by calling its neighbors(for:maximumCount:)
method, like this:
let similarWords = embedding?.neighbors(for: "rain", maximumCount: 10)
That will set similarWords
to be an array of tuples, where each tuple contains two values: a word that is similar, and a distance from your original word. This array is sorted by distance, so closest words come first.
We asked for “rain”, so we’ll get back “downpour” with a distance of 0.614, “rainstorm” with a distance of 0.661, “torrential” with a distance of 0.701, and more – drenching, rainfall, flooding, storm, flood, monsoon, and more.
Here’s a full example so you can try it easily:
if let embedding = NLEmbedding.wordEmbedding(for: .english) {
let similarWords = embedding.neighbors(for: "rain", maximumCount: 10)
for word in similarWords {
print("\(word.0) has a distance of \(word.1)")
}
}
Before you dive into word embeddings, I want to add an important note of caution: the concept of distance isn’t just “words that mean the same thing.”
Instead, word embeddings also include words that are used in similar contexts: if you search for “cat” you’ll get back “feline”, “kitten”, “tabby”, and “kitty”, but you’ll also get back “meow” because that’s the sound cat makes. You’ll also get back “pet”, because cats are pets, and even more you’ll get back “dog”, “canine”, and “puppy” because they are also pets.
Apple uses these word embeddings as search suggestions, giving users the chance to change their search. For example, if you search Photos for “meow” you’ll see a suggestion saying “meow -> Feline” as a suggested search.
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 13.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.