简体   繁体   English

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

[英]IOS/Objective-C: NSLinguisticTagger for Recognizing Named Entities

Apple provides an for using the tagger to identify named entities in Swift but not for Objective-C. Apple提供了一种使用标记器在Swift中标识命名实体的方法,但没有为Objective-C提供标识。

Here is the Swift example they provide: 这是他们提供的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)")
    }
}

I have gotten this far with translating with help from here it but I can't figure out how to spefify tags, eg [.personalName, .placeName, .organizationName]: Is that just an array of tag types though which you enumerate? 我已经从这里获得了翻译的帮助,但到目前为止我还不知道如何使标签具体化,例如[.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);
                  }];

Thanks for any suggestions on how to specify tags in Objective-C. 感谢您提供有关如何在Objective-C中指定标签的建议。

Original Swift code: 原始的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)")
    }
}

Output: 输出:

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

Objective-C version: 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);
    }
}];

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM