A tagger that analyzes natural language text.


class NLTagger : NSObject


NLTagger supports many different languages and scripts. Use it to segment natural language text into paragraph, sentence, or word units and to tag each unit with information like part of speech, lexical class, lemma, script, and language.

When you create a linguistic tagger, you specify what kind of information you're interested in by passing one or more NLTagScheme values. Set the string property to the natural language text you want to analyze, and the linguistic tagger processes it according to the specified tag schemes. You can then enumerate over the tags in a specified range, using the methods described in Enumerating Linguistic Tags, to get the information requested for a given scheme and unit.


Creating a Tagger

init(tagSchemes: [NLTagScheme])

Creates a linguistic tagger instance using the specified tag schemes and options.

var string: String?

The string being analyzed by the linguistic tagger.

Getting the Tag Schemes

class func availableTagSchemes(for: NLTokenUnit, language: NLLanguage) -> [NLTagScheme]

Retrieves the tag schemes available for a particular unit (like word or sentence) and language on the current device.

class func requestAssets(for: NLLanguage, tagScheme: NLTagScheme, completionHandler: (NLTagger.AssetsResult, Error?) -> Void)

Asks the Natural Language framework to load any missing assets for a tag scheme onto the device for the given language.

enum NLTagger.AssetsResult

The response to an asset request.

var tagSchemes: [NLTagScheme]

The tag schemes configured for this linguistic tagger.

struct NLTagScheme

Constants for the tag schemes specified when initializing a linguistic tagger.

Determining the Dominant Language and Orthography

var dominantLanguage: NLLanguage?

The dominant language of the string set for the linguistic tagger.

func setLanguage(NLLanguage, range: Range<String.Index>)

Sets the language for a range of text within the tagger's string.

func setOrthography(NSOrthography, range: Range<String.Index>)

Sets the orthography for the specified range.

Enumerating Linguistic Tags

struct NLTagger.Options

Constants for linguistic tagger enumeration specifying which tokens to omit and whether to join names.

struct NLTag

A token type, lexical class, name, lemma, language, or script returned by a linguistic tagger for natural language text.

Getting Linguistic Tags

func tag(at: String.Index, unit: NLTokenUnit, scheme: NLTagScheme) -> (NLTag?, Range<String.Index>)

Finds a tag for a given linguistic unit, for a single scheme, at the specified character position.

func tags(in: Range<String.Index>, unit: NLTokenUnit, scheme: NLTagScheme, options: NLTagger.Options) -> [(NLTag?, Range<String.Index>)]

Finds an array of linguistic tags and token ranges for a given string range and linguistic unit.

Deteriming the Range of a Unit Token

func tokenRange(at: String.Index, unit: NLTokenUnit) -> Range<String.Index>

Returns the range of the linguistic unit containing the specified character index.

enum NLTokenUnit

Constants representing linguistic units.

Using Models with a Tagger

func models(forTagScheme: NLTagScheme) -> [NLModel]

Returns the models that apply to the given tag scheme.

Using Gazetteers with a Tagger

func setGazetteers([NLGazetteer], for: NLTagScheme)

Attaches gazetteers to a tag scheme, typically one gazetteer per language or one language-independent gazetteer.

func gazetteers(for: NLTagScheme) -> [NLGazetteer]

Retrieves the gazetteers attached to a tag scheme.

class NLGazetteer

A collection of terms and their labels, which take precedence over a word tagger.


Inherits From

Conforms To

See Also

Linguistic Tags

Identifying Parts of Speech

Classify nouns, verbs, adjectives, and other parts of speech in a string.

Identifying People, Places, and Organizations

Use a linguistic tagger to perform named entity recognition on a string.