Grant Emerson

What’s in a Name

What’s in a Name — SwiftMoji Entry #8

Extracting a list of locations from a String, identifying the language of its contents, or sorting the contained words based on their parts of speech is just a fraction of the limitless capabilities of the Foundation framework’s NSLinguisticTagger class. In the following example, an NSLinguisticTagger is initialized with the nameType NSLinguisticTagScheme and an arbitrary options parameter “reserved for later use.” The tagging scheme specifies the type of text to be enumerated over. The String to be analyzed in this example is an outline of a family. The String can be passed into the tagger for evaluation via its string property. The enumerateTags(in:scheme:options:using:) method on the tagger is then used to extract the desired elements. The method is split into two processes: segmentation and tagging. First, the portion of the string property within the specified range is split up into segments based on the unit parameter. Then, the units that match the specified scheme are passed to a closure with a range value of their location. In the code example, after binding the optional tag it is verified to match the personalName type. If it does, the NSRange can be cast to a Swift Range to subscript the original string for the tagged name value.

import Foundation

var 👨‍👩‍👧‍👦 = """
    Family:
    Father's Name: Jayson
    Mother's Name: Kathrine
    Daughter's Name: Alannah
    Son's Name: Jacob
"""

let tagger = NSLinguisticTagger(tagSchemes: [.nameType], options: 0)
tagger.string = 👨‍👩‍👧‍👦

let range = NSRange(👨‍👩‍👧‍👦.startIndex..<👨‍👩‍👧‍👦.endIndex, in: 👨‍👩‍👧‍👦)

let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]

tagger.enumerateTags(in: range, unit: .word, scheme: .nameType, options: options) { (tag, tokenRange, _) in
    if let tag = tag, tag == .personalName {
        if let range = Range(tokenRange, in: 👨‍👩‍👧‍👦) {
            let name = 👨‍👩‍👧‍👦[range]
            print("\(name)") // Jayson Katherine Alannah Jacob
        }
    }
}
Tagged with: