Does anyone have a NSLinguisticTagger.possibleTags example in swift

I have found a single example in Objective-C, but have been unable to convert to work in swift. I keep getting memory violation errors deep inside the routine, several stack layers deeper than my code.

Accepted Reply

A simple example.

let tagger = NSLinguisticTagger(tagSchemes: [.lexicalClass], options: 0)
tagger.string = "This is the sample sentence."
var score: NSArray? = nil
let result = tagger.possibleTags(at: 10, scheme: NSLinguisticTagScheme.lexicalClass.rawValue, tokenRange: nil, sentenceRange: nil, scores: &score)
print(result, score)

Replies

A simple example.

let tagger = NSLinguisticTagger(tagSchemes: [.lexicalClass], options: 0)
tagger.string = "This is the sample sentence."
var score: NSArray? = nil
let result = tagger.possibleTags(at: 10, scheme: NSLinguisticTagScheme.lexicalClass.rawValue, tokenRange: nil, sentenceRange: nil, scores: &score)
print(result, score)

Thank you. The code works.


The main difference I see from what I was trying is the tagSchemes on tagger creation. I was using NSLinguisticTagger.availableTagSchemes(forLanguage: "en"), which worked great when I am using the enumerateTags function, but seems to be a problem when using possibleTags.


enumerateTags is great at tokenization, but is fairly poor in tagging parts-of-speech in a sentence correctly, so I was looking to get the possible tags for each word to incorporate an older parser algorithm I created 10 years ago to parse the sentences using hints from enumerateTags to speed things up.


Thanks again.

The Natural Language framework supports tokenization - see Tokenizing Natural Language Text and Identifying Parts of Speech for details. You could also look into using Create ML's MLWordTagger to create a word tagging model.