Topics

#12: NSLinguisticTagger 🎀

Topics

NSLinguisticTagger is one of the hidden gems of Foundation.

It can be used to process natural-language text (i.e. words written by a human and suitable for consumption by other humans).

It’s an incredibly powerful API and we’re only going to scratch the surface of what’s possible with it here.

Let’s use NSLinguisticTagger to extract all the proper nouns from a UITextView.

Then we’ll use those nouns to perform searches in the Wolfram Alpha API to provide instant feedback about the important things the user has written in the text view.

The result will be a bare-bones context-aware writing app that can understand what you're writing about!

First we extract the proper nouns, like this:

func updateProperNouns() -> [String] {
  let options: NSLinguisticTaggerOptions = .OmitWhitespace | .OmitPunctuation | .JoinNames
  let schemes = NSLinguisticTagger.availableTagSchemesForLanguage("en")
  let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))

  tagger.string = inputTextView.text

  var results = ""
    let properNouns = []

  let range = NSMakeRange(0, (inputTextView.text as NSString).length)

  tagger.enumerateTagsInRange(range, scheme: NSLinguisticTagSchemeNameType, options: options) {
    (tag, tokenRange, sentenceRange, _) in
          let token = (self.inputTextView.text as NSString).substringWithRange(tokenRange)
          results += "\(token): \(tag)\n"

      let properNounTags = [
        NSLinguisticTagPersonalName,
        NSLinguisticTagPlaceName,
        NSLinguisticTagOrganizationName
      ]

      if contains(properNounTags, tag) {
      properNouns.append(token)   }

    return properNouns
  }
}

Then, we grab the first noun and search for it, and place the resulting description into our output text view:

if let query = properNouns.first {
  API.search(query) { (thingDescription, error) in
    self.outputTextView.text = thingDescription
  }
}

Here's what it looks like:

Download a working demo project here

Read More About NSLinguisticTagger