Classify nouns, verbs, adjectives, and other parts of speech in a string.
Identifying the parts of speech for words in natural language text can help your program understand the meaning of sentences. For example, given the transcription of a request spoken by the user, you might determine general intent by looking at only the nouns and verbs.
The example below shows how to use
NSLinguistic to enumerate over natural language text and identify the part of speech for each word.
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, and omitting any punctuation or whitespace. In the enumeration block, the part of speech 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 word and its part of speech on a new line, as shown below: