Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Last active August 28, 2019 21:12
Show Gist options
  • Select an option

  • Save ozgurshn/d45d7c0f5f0a6e1b366754afa734ee79 to your computer and use it in GitHub Desktop.

Select an option

Save ozgurshn/d45d7c0f5f0a6e1b366754afa734ee79 to your computer and use it in GitHub Desktop.
NLTagger
import NaturalLanguage
let text = "Prime Minister Boris Johnson has urged the EU to re-open the withdrawal deal reached with Theresa May, and to make key changes that would allow it to be passed by Parliament."
let tagger = NLTagger(tagSchemes: [.nameType ])
tagger.string = text
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
let tags: [NLTag] = [.personalName, .placeName, .organizationName, .adverb ,NLTag.pronoun, NLTag.determiner, NLTag.noun , NLTag.interjection ]
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: NLTagScheme.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