使用NLTagger识别String分词之后的人名,地名,组织机构名等
参考自苹果官方文档
使用语言标记器对字符串执行命名实体识别。
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
}
输入内容为
["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