使用NLTagger識別String分詞之后的人名焕檬,地名,組織機(jī)構(gòu)名等
參考自蘋果官方文檔
使用語言標(biāo)記器對字符串執(zhí)行命名實(shí)體識別非剃。
import NaturalLanguage
let text = "The American Red Cross was established in Washington, D.C., by Clara Barton."
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = text
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
let tags: [NLTag] = [.personalName, .placeName, .organizationName]
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType, options: options) { tag, tokenRange in
// Get the most likely tag, and print it if it's a named entity.
if let tag = tag, tags.contains(tag) {
print("\(text[tokenRange]): \(tag.rawValue)")
}
// Get multiple possible tags with their associated confidence scores.
let (hypotheses, _) = tagger.tagHypotheses(at: tokenRange.lowerBound, unit: .word, scheme: .nameType, maximumCount: 1)
print(hypotheses)
return true
}
輸入內(nèi)容為
["OtherWord": 0.9974877557628311]
American Red Cross: OrganizationName
["OrganizationName": 1.0]
["OtherWord": 0.9997951690191498]
["OtherWord": 0.9972322554508491]
["OtherWord": 0.9900385427580366]
Washington: PlaceName
["PlaceName": 1.0]
["OtherWord": 0.9998070074878757]
["OtherWord": 0.9995524348475762]
Clara Barton: PersonalName
["PersonalName": 1.0]
Program ended with exit code: 0