ios – 使用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}
点赞