๐ Kicking Off a New Series on Apple's Machine Learning Tools ๐ค
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:
- How to use the Natural Language API to analyze the sentiment in text
- How to use the Vision API to identify text within an image
- How to train a machine learning model with Create ML
- How to use Core ML to integrate new models in your app
- How to use the new Foundation Models framework from iOS 26
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.