Skip to content

Instantly share code, notes, and snippets.

@julien-c
Created August 30, 2017 13:53
Show Gist options
  • Save julien-c/e89d8e6ccd19221bc303c77b95df515d to your computer and use it in GitHub Desktop.
Save julien-c/e89d8e6ccd19221bc303c77b95df515d to your computer and use it in GitHub Desktop.
iOS 11 NLP demo
let tagger = NSLinguisticTagger(tagSchemes: [.nameType], options: 0)
let dataset: Dataset = .test
let url = Bundle.main.url(forResource: dataset.rawValue, withExtension: "txt")!
let content = try! String(contentsOf: url, encoding: .utf8).components(separatedBy: ["\n"])
var output: [String] = []
/// Map ios nlp tags to canonical tags.
extension NSLinguisticTag {
var canonical: String {
switch self {
case .personalName:
return "PER"
case .placeName:
return "LOC"
case .organizationName:
return "ORG"
default:
return "O"
}
}
}
func iob(_ descr: String, _ prevDescr: String) -> String {
if descr == "O" {
return "O"
}
if descr == prevDescr {
return "I-\(descr)"
} else {
return "B-\(descr)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment