Natural Language Processing  in the iOS SDK

With NaturalLanguage kit that was released along with IOS 11, we have a very powerful framework that can a be time-saving tool. In this article, I will help you to figure out almost useful functions. Before we start let’s get used to some of the basic terms that will be used a lot

  1. Token Type: A property which classifies each character as either a word, punctuation, or whitespace.
  2. Language: Determines the language of the token
  3. Lexical Class: A property which classifies each token according to its class. For example, it’ll determine the part of speech for a word, the type of punctuation for a punctuation, or the type of whitespace for a whitespace.
  4. Name Type: This property looks for tokens which are part of a named entity. It’ll look for a personal name, an organizational name, and a place name.
  5. Lemma: This basically returns the stem of a word token. I’ll be going into more detail about this later on.


Language Identification

func recogizerLanguage(text:String) -> NLLanguage? {
        let tagger = NLTagger(tagSchemes: [.language])
        tagger.string = text
       return tagger.dominantLanguage 

Note: The dominant language is the most frequently occurring language in the string. If you had a sentence which had a mix of words from English, French, and Spanish, it would choose the most common language.


func tokenizeText(for text: String) {
    tagger.string = text
    let range = NSRange(location: 0, length: text.utf16.count)
    tagger.enumerateTags(in: range, unit: .word, scheme: .tokenType, options: options) { tag, tokenRange, stop in
        let word = (text as NSString).substring(with: tokenRange)

A nature, universe, science, music, love lover

Leave a Reply

Your email address will not be published. Required fields are marked *