Created
August 30, 2017 13:53
-
-
Save julien-c/e89d8e6ccd19221bc303c77b95df515d to your computer and use it in GitHub Desktop.
iOS 11 NLP demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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