IOS / Objective-C:用于识别命名实体的NSLinguisticTagger

时间:2018-09-11 18:38:24

标签: ios objective-c nslinguistictagger

Apple提供了一种使用标记器在Swift中标识命名实体的方法,但没有为Objective-C提供标识。

这是他们提供的Swift示例:

let text = "The American Red Cross was established in Washington, D.C., by Clara Barton."
let tagger = NSLinguisticTagger(tagSchemes: [.nameType], options: 0)
tagger.string = text
let range = NSRange(location:0, length: text.utf16.count)
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
let tags: [NSLinguisticTag] = [.personalName, .placeName, .organizationName]
tagger.enumerateTags(in: range, unit: .word, scheme: .nameType, options: options) { tag, tokenRange, stop in
    if let tag = tag, tags.contains(tag) {
        let name = (text as NSString).substring(with: tokenRange)
        print("\(name): \(tag)")
    }
}

我已经完成了help from here的翻译工作,但我不知道如何使标签具体化,例如[.personalName,.placeName,.organizationName]:这只是您枚举的标记类型的数组吗?

NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc]
                              initWithTagSchemes:[NSArray arrayWithObjects:NSLinguisticTagSchemeNameType, nil]
                              options:(NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames)];
[tagger setString:text];

[tagger enumerateTagsInRange:NSMakeRange(0, [text length])
                      scheme:NSLinguisticTagSchemeNameType
                     options:(NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation| NSLinguisticTaggerJoinNames)
                  usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                      NSString *token = [text substringWithRange:tokenRange];
                      NSString *name =[tagger tagAtIndex:tokenRange.location scheme:NSLinguisticTagSchemeNameType tokenRange:NULL sentenceRange:NULL];

                      if (name == nil) {
                          name = token;
                      }

                      NSLog(@"tagger results:%@, %@", token, name);
                  }];

感谢有关在Objective-C中指定标签的建议。

1 个答案:

答案 0 :(得分:0)

原始Swift代码:

let text = "The American Red Cross was established in Washington, D.C., by Clara Barton."
let tagger = NSLinguisticTagger(tagSchemes: [.nameType], options: 0)
tagger.string = text
let range = NSRange(location:0, length: text.utf16.count)
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
let tags: [NSLinguisticTag] = [.personalName, .placeName, .organizationName]
tagger.enumerateTags(in: range, unit: .word, scheme: .nameType, options: options) { tag, tokenRange, stop in
    if let tag = tag, tags.contains(tag) {
        let name = (text as NSString).substring(with: tokenRange)
        print("\(name): \(tag)")
    }
}

输出:

American Red Cross: NSLinguisticTag(_rawValue: OrganizationName)
Washington: NSLinguisticTag(_rawValue: PlaceName)
Clara Barton: NSLinguisticTag(_rawValue: PersonalName)

Objective-C版本:

NSString* text = @"The American Red Cross was established in Washington, D.C., by Clara Barton.";
NSLinguisticTagger* tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[NSLinguisticTagSchemeNameType] options:0];
tagger.string = text;
NSRange range = NSMakeRange(0, text.length);
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerJoinNames;
NSArray* tags = @[NSLinguisticTagPersonalName, NSLinguisticTagPlaceName, NSLinguisticTagOrganizationName];
[tagger enumerateTagsInRange:range unit:NSLinguisticTaggerUnitWord scheme:NSLinguisticTagSchemeNameType options:options usingBlock:^(NSLinguisticTag  _Nullable tag, NSRange tokenRange, BOOL * _Nonnull stop) {
    if ([tags containsObject:tag]) {
        NSString* name = [text substringWithRange:tokenRange];
        NSLog(@"%@: %@", name, tag);
    }
}];

输出:

2018-09-12 09:51:00.323378-0700 App[2408:109005] American Red Cross: OrganizationName
2018-09-12 09:51:00.323755-0700 App[2408:109005] Washington: PlaceName
2018-09-12 09:51:00.323901-0700 App[2408:109005] Clara Barton: PersonalName