Use a linguistic tagger to perform named entity recognition on a string.
Identifying named entities in natural language text can help make your app more intelligent. For example, a messaging app might look for names of people and places in text in order to display related information like contact information or directions.
The example below shows how to use
NSLinguistic to enumerate over natural language text and identify any named person, place, or organization.
First, an instance of
NSLinguistic is created, specifying
NSLinguistic as the tag scheme to be used. Next, the
string property of the linguistic tagger is set to the natural language text. Finally, the linguistic tagger enumerates over the entire range of the string, specifying
NSLinguistic as the tag unit and
NSLinguistic as the tag scheme, omitting any punctuation or whitespace, and joining words that are part of a single name into the same token. In the enumeration block, the name type is provided by
tag, and each word is obtained by taking a substring of the original text at
When run, this code prints out each name and its type on a new line, as shown below: