簡體   English   中英

使用NSLinguisticTagger識別人名

[英]Identifying person names with NSLinguisticTagger

我正在修補NSLinguisticTagger

識別名詞,動詞,介詞等基本單詞類型非常有效。

然而,人名NSLinguisticTagPersonalName的識別幾乎不適用於我的測試(iOS8)。 地方NSLinguisticTagPlaceName似乎也運作良好,但大多數時候人名也被歸類為地方。

這是我的基本設置(使用NSLinguisticTagSchemeNameTypeOrLexicalClass)

    var tagger:NSLinguisticTagger = NSLinguisticTagger(tagSchemes: NSLinguisticTagger.availableTagSchemesForLanguage("en") , options: 3)
    tagger.string = entryString
    tagger.enumerateTagsInRange(NSMakeRange(0, entryString.length), scheme: NSLinguisticTagSchemeNameTypeOrLexicalClass, options: (NSLinguisticTaggerOptions.OmitWhitespace | NSLinguisticTaggerOptions.JoinNames), usingBlock: {
        tag,tokenRange,sentenceRange,_ in
        let token = entryString.substringWithRange(tokenRange)
        println("[\(tag)] \(token) \(tokenRange)")

例1

 "Meeting with John in Paris"

  Evaluation

 [Verb] Meeting
 [Preposition] with
 [Noun] John
 [Preposition] in
 [PlaceName] Paris

例2

 "Meeting with John"

  Evaluation

 [Verb] Meeting (0,7)
 [Preposition] with (8,4)
 [PlaceName] John (13,4)

知道如何改善人名的匹配嗎?

此外,我有興趣知道名稱需要如何被識別。 (我假設例如像“with”這樣的介詞將是一個很好的指標......顯然這還不夠)。 我很感激任何想法或其他見解。 這是一個令人興奮的領域。

顯然,正確答案是:“等待幾年蘋果改善Swift 4中的 NSLinguisticTagger

這是在Xcode 9(beta)中編寫和執行的Swift 4代碼:

let entryString = "Meeting with John"

let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
let options: NSLinguisticTagger.Options = [
    .omitWhitespace, .omitPunctuation, .joinNames
]

let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
tagger.string = entryString

let rangeOfEntireEntryString = NSRange(location: 0, length: entryString.utf16.count)

tagger.enumerateTags(
    in: rangeOfEntireEntryString,
    scheme: .nameTypeOrLexicalClass,
    options: options)
{ (tag, tokenRange, sentenceRange, _) in
    guard let tag = tag?.rawValue else { return }
    let token = (entryString as NSString).substring(with: tokenRange)
    print("[\(tag)] \(token) \(tokenRange)")
}

這是第一個示例字符串的結果:

let entryString = "Meeting with John in Paris"

[Noun] Meeting {0, 7}
[Preposition] with {8, 4}
[PersonalName] John {13, 4}
[Preposition] in {18, 2}
[PlaceName] Paris {21, 5}

和你的第二個例子字符串:

let entryString = "Meeting with John"

[Noun] Meeting {0, 7}
[Preposition] with {8, 4}
[PersonalName] John {13, 4}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM