Skip to content

Instantly share code, notes, and snippets.

@mohdsanadzakirizvi
Last active November 26, 2019 10:05
Show Gist options
  • Save mohdsanadzakirizvi/c0a9aeca04a9d4079f743324b7191fc7 to your computer and use it in GitHub Desktop.
Save mohdsanadzakirizvi/c0a9aeca04a9d4079f743324b7191fc7 to your computer and use it in GitHub Desktop.
let text = "Hello world, I am a data scientist. I work with machine learning!"
// Initialize the tagger
let tagger = NLTagger(tagSchemes: [.lexicalClass])
// Ignore whitespace and punctuation marks
let options : NLTagger.Options = [.omitWhitespace, .omitPunctuation]
// Process the text for POS
tagger.string = text
// loop through all the tokens and print their POS tags
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .lexicalClass, options: options) { tag, tokenRange in
if let tag = tag{
print("\(text[tokenRange]): \(tag.rawValue)")
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment