๐Ÿ†• Kicking Off a New Series on Apple's Machine Learning Tools ๐Ÿค–

Get started with Machine Learning
Natural Language
July 7, 2025
Sponsored

Forget about Ruby and Fastlane installation issues!

Discover Codemagic CLI Tools - the free, open-source Fastlane alternative for automating iOS builds, code signing and publishing.

This message is brought to you by a sponsor who helps keep this content free for everyone. If you have a moment, check them out - your support means a lot!

Welcome to issue #52 of the iOS Coffee Break Newsletter ๐Ÿ“ฌ.

Apple has recently released a set of new tutorials focused on Machine Learning, and I have been diving into them over the past few days.

As I went through the material, I noticed that a significant portion of my time was actually spent on SwiftUI, rather than the core ML content ๐Ÿ‘€ ...

That inspired me to start a new series in the newsletter called "Get started with Machine Learning". In this series, I'll be focusing specifically on the Machine Learning aspects of the tutorials, offering a high-level overview of the ML features and APIs Apple provides.

Although I'll be covering many of Apple's ML capabilities, I strongly encourage you to go through the official tutorials yourself to get the full experience ๐Ÿ‘.

Series Plan

In this series, here is what you can expect to learn:

Analyzing text

Let's kick off the series by exploring how to analyze text to identify its sentiment using the Natural Language API.

To start, here is how it works:

import NaturalLanguage
 
class Scorer {
    let tagger = NLTagger(tagSchemes: [.sentimentScore])
 
    func score(_ text: String) -> Double {
        var sentimentScore = 0.0
        tagger.string = text
        tagger.enumerateTags(
            in: text.startIndex..<text.endIndex,
            unit: .paragraph,
            scheme: .sentimentScore,
            options: []
        ) { sentimentTag, _ in
            if let sentimentString = sentimentTag?.rawValue,
                let score = Double(sentimentString) {
                sentimentScore = score
                return true
            }
            return false
        }
        return sentimentScore
    }
}

The NLTagger class allows you to specify a tagging scheme that extracts various types of information from text - such as its grammatical role or the language it is written in. Among the available schemes, we are focusing on .sentimentScore for this example. This particular scheme provides a sentiment value ranging from 1.0 (the most positive ๐Ÿ˜ƒ) to -1.0 (the most negative ๐Ÿ˜ข).

Here is an example of how to implement it:

let text = "I enjoy hard hikes. When my heart is pumping and I'm being challenged, I feel great."
 
let scorer = Scorer()
let score = scorer.score(text)

In this sample, the score property gets a value of 1.0, indicating extremely positive sentiment. Great! ๐Ÿ’ช

Feel free to experiment on your own by entering various types of text to see how the API reacts!

๐Ÿค Wrapping Up

Machine learning can work across different input and output types. In this edition, we explored Apple's Natural Language framework along with sentiment analysis, demonstrating how we can input text and obtain a numerical sentiment score in return.

As a next step, we can enhance our approach by modeling sentiment characteristics, for example, defining an enum to ensure consistent representation of sentiment values throughout the app.

Have any feedback, suggestions, or ideas to share? Feel free to reach out to me on Twitter.

tiagohenriques avatar

Thank you for reading this issue!

I truly appreciate your support. If you have been enjoying the content and want to stay in touch, feel free to connect with me on your favorite social platform: