iOS开发 - Natural Language Processing(NLP)其四

使用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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容