NEW: Join my free 100 Days of SwiftUI challenge today! >>

How to find similar words for a search term

Swift version: 5.1

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.

LEARN SWIFTUI FOR FREE I have a massive, free SwiftUI video collection on YouTube teaching you how to build complete apps with SwiftUI – check it out!

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 OUR BOOKS
Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5