Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// Places, People, Organizations using NER
let text = "Apple is looking at buying U.K. startup for $1 billion."
// Initialize NLTagger with ".nameType" scheme for NER
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = text
// Ignore Punctuation and Whitespace
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
// Tags to extract
let tags: [NLTag] = [.personalName, .placeName, .organizationName]
// Loop over the tokens and print the NER of the tokens
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType, options: options) { tag, tokenRange in
if let tag = tag, tags.contains(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