NSLinguisticTagger problem

I've tried to use this piece of code taked by Apple Documentation and don't work


let text = "The ripe taste of cheese improves with age."
let tagger = NSLinguisticTagger(tagSchemes: [.lexicalClass], options: 0)
tagger.string = text
let range = NSRange(location: 0, length: text.utf16.count)
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace]
tagger.enumerateTags(in: range, unit: .word, scheme: .tokenType, options: options) { tag, tokenRange, _ in
      if let tag = tag {
          let word = (text as NSString).substring(with: tokenRange)
           print("\(word): \(tag)")
     }
}


this is the Apple's Doc

https://developer.apple.com/documentation/foundation/nslinguistictagger/identifying_parts_of_speech

Any solution?

Replies

What do you mean by "doesn't work"? Are you getting an error message?

No warning. no errors.


just jump the enumerateTags method and don't enter inside.

Change:


let tagger = NSLinguisticTagger(tagSchemes: [.lexicalClass], options: 0)


To:

let tagger = NSLinguisticTagger(tagSchemes: [.tokenType], options: 0)


https://developer.apple.com/documentation/foundation/nslinguistictagger/tokenizing_natural_language_text


When you initialize NSLinguisticTagger you need to include in tagSchemes the scheme that you will use in enumerateTags


let tagger = NSLinguisticTagger(tagSchemes: [.tokenType, .lexicalClass], options: 0)

What do you do if the scheme "does not exist"? https://stackoverflow.com/questions/48768919/device-vs-simulator-linguistic-schemes When testing on physical devices the console prints 3 schemes are available, none of which are the Lexical or Lemma scheme....