NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to find similar words for a search term

Swift version: 5.2

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 NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out 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 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

Link copied to your pasteboard.