Implement Natural Language Processing in 3 Minutes using Swift!

Implement Natural Language Processing in 3 Minutes using Swift!

There’s more content coming, so be sure to subscribe to my Youtube channel.

Here’s the transcript of the video.

Today we’re gonna take a look at the Natural Language framework.
Apple introduced this cross-platform framework this June at the developer conference.

Note that I’ll be using Xcode 10 beta so things may change until the official release.
All right, let’s start with a playground. I’m going to call it an NLP-demo.

First, I get rid of the boilerplate code. Next, we need to import the Natural Language framework.
Again, I’m using Xcode 10 beta. so you’ll get a compiler error whenever you try to import this framework on a prior version of Xcode.

Now, if I try to use any class from the natural language framework Xcode gives me a warning. Let’s give it a try!

So, I’m gonna instantiate an NLTagger instance. I need to initialize the tagger with an array of tag schemes.
The tagger tokenize is the input string and classifies the tags according to the provided schemes.

Now, let’s try to execute this playground. Yeah, indeed.
So here we get an error we need to make sure that we’re on iOS 12. So, I need to add this condition: if available iOS 12.

And now it should work. All right. So, we have our tagger. I used the lexical class tag scheme, which classifies the text according to lexical classes.
That is, it finds out whether a word is a noun, an objective, a pronoun, and so on.

Next, I create a string with one of my favorite quotes and assign it to the tagger string property.
Next I’m going to use the tagger to enumerate over the entire range of the string.

We start with the text’s start index up to its end index. The token unit should be a word and the scheme is again a lexical class.

We aren’t interested in punctuation and white spaces, so I set the options to omit the punctuation and omit white space… and omit whitespace.
In the closure, we get the tag and the range as we iterate through the tags. We’ll need a string to collect the results.
Let’s call it result. Equals an empty string.
And here, first, we check whether we have a valid tag then we build a string using the detected tokens and their lexical class.
And we add the word found at that range and the lexical class and finally let’s return true.
Here, we simply print the result to the console.

All right, now let’s run the demo I’m going to slightly adjust the text so that you can see better the output by adding some parentheses.
Let’s see.

Now, we can see the original text was tokenized and then each and every word has received its lexical class.
“Silence” is a noun, “is” – verb, “a” – determiner, “source” – noun and “of” – preposition, “great” – objective.
Cool, is it?

The new natural language framework has many other awesome features. I plan to talk about them in upcoming videos, so stay tuned!

Thanks for watching!

Are you interested in NLP and machine learning? Check out my upcoming book and video course “Machine Learning with CoreML and Swift.”
I plan to publish the video course and the book by September.
The book is available for pre-order on Amazon.

About the Author:

Leave A Comment