Identifying Parts of Speech on iOS using Natural Language framework

Identifying Parts of Speech on iOS using Natural Language framework

In this tutorial, we will walk you through an example of how to identify parts of speech on iOS using Apple’s Natural Language Processing framework (also referred to as NLP). With a WrappingButtonView, some UIButtons and the Natural Language Framework we were able to create a simple and highly addictive game to test a user’s knowledge of the different parts of speech (Wiki). This could be a game for 4th or 5th graders… or just for anyone wanting a refresher on language constructs. The name of the game? Grammanizer.

IOS
NATURAL LANGUAGE PROCESSING

Introduction

In this tutorial, we will walk you through an example of how to identify parts of speech on iOS using Apple’s Natural Language Processing framework (also referred to as NLP).

With a WrappingButtonView, some UIButtons and the Natural Language Framework we were able to create a simple and highly addictive game to test a user’s knowledge of the different parts of speech (Wiki). This could be a game for 4th or 5th graders… or just for anyone wanting a refresher on language constructs. The name of the game? Grammanizer.

NLTagger

The meat of the gameplay utilizes the NLTagger class to disect sentences into its constituent parts of speech, namely:

func enumerateTags(in range: Range, unit: NLTokenUnit, scheme: NLTagScheme, options: NLTagger.Options = default, using block: (NLTag?, Range) -> Bool)

For example, analyzing a sentence with .word as the NLTokenUnit and .lexicalClass as the NLTagScheme we get the following:

Pretty good. With this we wrapped it in a simple UI containing an array of buttons and some instructions. And of course a winning 🍔!

The tagger performs well with basic sentences so we tried a sentence with more subtleties; “close” acting as a noun, verb, and adjective:

Close (ugghh)! We expect the second “close” to be a noun. But, still, pretty dang close.

We also added a sentence in Italian (because, you know, 🍝?). The same NLTagger instance switches between languages without updating any parameters which is pretty cool. And this could easily be used as a game or utility for someone learning a foreign language.

Conclusion

This is a simple but practical and fun use of the natural language framework to find parts of speech on iOS. For more advanced cases (e.g training your own NLP model) we recommend checking out the WWDC video Introducing Natural Language Framework

The full source code is available here for your pleasure. Requires Xcode 10. Enjoy!

About SwiftKick Mobile

SwiftKick Mobile is a mobile application design and development agency in Austin, TX. Need help with your next app? Reach out to hello@swiftkickmobile.com

Everything you need to grow your business

Crafting exceptional mobile experiences so you stand out in a crowded digital landscape. Trusted by Experian, Vrbo, & Expedia.

Let's do it together