UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to find similar words for a search term

Swift version: 5.6

Paul Hudson    @twostraws   

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.

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 13.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.