---
title: "#12: NSLinguisticTagger 🎤"
url: https://littlebitesofcocoa.com/12-nslinguistictagger
author: Jake Marsh
published: 2015-06-02
topics: ["Foundation"]
description: "NSLinguisticTagger is one of the hidden gems of Foundation. It can be used to process natural-language text (i.e."
---

# #12: NSLinguisticTagger 🎤

By Jake Marsh, June 2, 2015. HTML: https://littlebitesofcocoa.com/12-nslinguistictagger

**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](http://products.wolframalpha.com/api/explorer.html) 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:

``` swift
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:

``` swift
if let query = properNouns.first {
  API.search(query) { (thingDescription, error) in
    self.outputTextView.text = thingDescription
  }
}
```

Here's what it looks like:

<img src="https://media.littlebitesofcocoa.com/bite-assets/012-nslinguistictagger-screenshot.png" style="max-width: 375px;" />

[Download a working demo project here](http://j.mp/bite012)

## Read More About NSLinguisticTagger

- [Apple's Docs](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSLinguisticTagger_Class/)
- [NSHipster](http://nshipster.com/nslinguistictagger/)
- [objc.io](http://www.objc.io/issue-7/linguistic-tagging.html)
