iOS開發(fā) - Natural Language Processing(NLP)其四

使用NLTagger識別String分詞之后的人名,地名,組織機構(gòu)名等

參考自蘋果官方文檔

使用語言標記器對字符串執(zhí)行命名實體識別。

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容