NLP in iOS Development
By: Sai Durga Mahesh
Tags: NLP, Swift Programming, Ios Development, Natural Language, iOS App Development

Photo by Jazmin Quaynor on Unsplash

NLP is a field from Artificial Intelligence that helps to understand human language well. NLP is mainly used for Text Classification and Tagging.

Many NLP tasks has to start from tokenization, lemmatization and embedding. Apple provides ‘Natural Language’ framework to perform NLP tasks on user data.

Also Apple provides BERT-SQuAD CoreML model for question answering. This can be directly used in our app with very few steps.

But understanding Natural Language framework is required to perform NLP tasks such as Entity Recognition and Text Classification.

Let’s see some trivial NLP tasks we can perform with Natural Language framework.

Tokenization

Natural Human Language is stream of words and sentences. It is to be tokenized or enumerated to paragraphs, sentences or words for processing.

NLTokenizer class from ‘Natural Language’ is used to do this task.

let tokenizer = NLTokenizer(unit: .word)

Here unit can be word or sentence depending on the use case. Learn more about Tokenization here.

Language Recognition

Very common and basic task in NLP is to identify the language of given piece of text.

This can be done easily with ‘NLLanguageRecognizer’.

let recognizer = NLLanguageRecognizer()

recognizer.processString("English Language")

You can specify the list of possible languages like below

recognizer.languageConstraints = [.french, .english, .german, .italian, .spanish, .portuguese]

Learn more about language recognition here.

Parts Of Speech Recognition

Identifying parts of speech is sometimes necessary. Consider a situation where you need to identify nouns or verbs from a given text when providing a overview of paragraph.

This can be done with ‘NLTagger’

let tagger = NLTagger(tagSchemes: [.lexicalClass])

tagger.string = text

Tag Schemes can be lexicalClass , nameType or script. Use only necessary schemes for your use case.

For example lexcicalClass is when you are interested in Parts of Speech of text and if language of text makes a difference then include language. Learn more about schemes here.

You can further provide options such as omitPunctuation and omitWhiteSpaces depending on the use case.

Learn more about POS Recognition here

People, Place and Organizations Identification

Identifying people, place or organization sometimes necessary to have a quick look at text.

This is similar to POS recognition. Specify nametype as tagScheme and use tags array to filter available enumerated tags.

let tagger = NLTagger(tagSchemes: [.nameType])tagger.string = text let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]

let tags: [NLTag] = [.personalName, .placeName, .organizationName]

Learn more about this task here.

Embedding

Text Embedding is trivial NLP task. Text Embedding is used to find similar words and sentences. Also this is used as basic step of building Recommendation Engine.

‘NLEmbedding’ class of Natural Language framework can be used to do this task.

let embedding = NLEmbedding.wordEmbedding(for: .english)

with embedding we can obtain vector representation, know the similarity between words using distance or obtain neighbours of the word.

if let vector = embedding.vector(for: word) {

print(vector) }

let specificDistance = embedding.distance(between: word, and: “motorcycle”)

The above steps can be applied for sentences too. Learn more about this task here.

Conclusion

We have seen some basic NLP tasks that can be performed with ‘Natural Language’ framework. Also We can create custom word tagger or classifier using createML.

Reference

https://developer.apple.com/documentation/naturallanguage