iOS पर एमएल किट वाली इकाइयां निकालना

टेक्स्ट के किसी हिस्से का विश्लेषण करने और उसमें मौजूद इकाइयों को एक्सट्रैक्ट करने के लिए, एमएल किट एंटिटी एक्सट्रैक्शन एपीआई को शुरू करें. इसके लिए, टेक्स्ट को सीधे उसके annotateText:completion: तरीके में पास करें. एक वैकल्पिक EntityExtractionParams ऑब्जेक्ट को भी पास किया जा सकता है. इसमें, इकाई के टाइप के सबसेट को खोजने की सीमा तय करने के लिए, पहचान के समय, टाइमज़ोन या फ़िल्टर जैसे कॉन्फ़िगरेशन के अन्य विकल्प मौजूद होते हैं. एपीआई, हर इकाई के बारे में जानकारी वाले EntityAnnotation ऑब्जेक्ट की सूची दिखाता है.

ऐप्लिकेशन के रन टाइम के दौरान, इकाई एक्सट्रैक्शन बेस डिटेक्टर ऐसेट स्टैटिक रूप से लिंक की जाती हैं. ये आपके ऐप्लिकेशन में करीब 10.7 एमबी जोड़ते हैं.

इसे आज़माएं

वेब कंटेनर इंस्टॉल करने से पहले

  1. अपनी Podfile में, नीचे दी गई ML Kit लाइब्रेरी शामिल करें:

    pod 'GoogleMLKit/EntityExtraction', '3.2.0'
    
  2. प्रोजेक्ट के Pods को इंस्टॉल या अपडेट करने के बाद, उसका .xcworkspace इस्तेमाल करके Xcode प्रोजेक्ट खोलें. ML Kit, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करता है.

टेक्स्ट से इकाइयां निकालें

टेक्स्ट से इकाइयां एक्सट्रैक्ट करने के लिए, सबसे पहले भाषा तय करके EntityExtractorOptions ऑब्जेक्ट बनाएं. इसके बाद, EntityExtractor को इंस्टैंशिएट करने के लिए इसका इस्तेमाल करें:

Swift

// Note: You can specify any of the 15 languages entity extraction supports here. 
let options = EntityExtractorOptions(modelIdentifier: 
                                    EntityExtractionModelIdentifier.english)
let entityExtractor = EntityExtractor.entityExtractor(options: options)

Objective-C

// Note: You can specify any of the 15 languages entity extraction supports here. 
MLKEntityExtractorOptions *options = 
    [[MLKEntityExtractorOptions alloc] 
        initWithModelIdentifier:MLKEntityExtractionModelIdentifierEnglish];

MLKEntityExtractor *entityExtractor = 
    [MLKEntityExtractor entityExtractorWithOptions:options];

इसके बाद, पक्का करें कि डिवाइस पर ज़रूरी भाषा मॉडल डाउनलोड किया गया है:

Swift

entityExtractor.downloadModelIfNeeded(completion: {
  // If the error is nil, the download completed successfully.
})

Objective-C

[entityExtractor downloadModelIfNeededWithCompletion:^(NSError *_Nullable error) {
    // If the error is nil, the download completed successfully.
}];

मॉडल डाउनलोड होने के बाद, annotate वाले तरीके में कोई स्ट्रिंग और वैकल्पिक MLKEntityExtractionParams पास करें.

Swift

// The EntityExtractionParams parameter is optional. Only instantiate and
// configure one if you need to customize one or more of its params.
var params = EntityExtractionParams()
// The params object contains the following properties which can be customized on
// each annotateText: call. Please see the class's documentation for a more
// detailed description of what each property represents.
params.referenceTime = Date();
params.referenceTimeZone = TimeZone(identifier: "GMT");
params.preferredLocale = Locale(identifier: "en-US");
params.typesFilter = Set([EntityType.address, EntityType.dateTime])

extractor.annotateText(
    text.string,
    params: params,
    completion: {
      result, error in
      // If the error is nil, the annotation completed successfully and any results 
      // will be contained in the `result` array.
    }
)

Objective-C

// The MLKEntityExtractionParams property is optional. Only instantiate and
// configure one if you need to customize one or more of its params.
MLKEntityExtractionParams *params = [[MLKEntityExtractionParams alloc] init];
// The params object contains the following properties which can be customized on
// each annotateText: call. Please see the class's documentation for a fuller 
// description of what each property represents.
params.referenceTime = [NSDate date];
params.referenceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
params.preferredLocale = [NSLocale localWithLocaleIdentifier:@"en-US"];
params.typesFilter = 
    [NSSet setWithObjects:MLKEntityExtractionEntityTypeAddress, 
                          MLKEntityExtractionEntityTypeDateTime, nil];

[extractor annotateText:text.string
             withParams:params
             completion:^(NSArray *_Nullable result, NSError *_Nullable error) {
  // If the error is nil, the annotation completed successfully and any results 
  // will be contained in the `result` array.
}

पहचानी गई इकाइयों के बारे में जानकारी पाने के लिए, एनोटेशन के नतीजों को लूप में चलाएं.

Swift

// let annotations be the Array! returned from EntityExtractor
for annotation in annotations {
  let entities = annotation.entities
  for entity in entities {
    switch entity.entityType {
      case EntityType.dateTime:
        guard let dateTimeEntity = entity.dateTimeEntity else {
          print("This field should be populated.")
          return
        }
        print("Granularity: %d", dateTimeEntity.dateTimeGranularity)
        print("DateTime: %@", dateTimeEntity.dateTime)
      case EntityType.flightNumber:
        guard let flightNumberEntity = entity.flightNumberEntity else {
          print("This field should be populated.")
          return
        }
        print("Airline Code: %@", flightNumberEntity.airlineCode)
        print("Flight number: %@", flightNumberEntity.flightNumber)
      case EntityType.money:
        guard let moneyEntity = entity.moneyEntity else {
          print("This field should be populated.")
          return
        }
        print("Currency: %@", moneyEntity.integerPart)
        print("Integer Part: %d", moneyEntity.integerPart)
        print("Fractional Part: %d", moneyEntity.fractionalPart)
      // Add additional cases as needed.
      default:
        print("Entity: %@", entity);
    }
  }
}

Objective-C

NSArray *annotations; // Returned from EntityExtractor

for (MLKEntityAnnotation *annotation in annotations) {
            NSArray *entities = annotation.entities;
            NSLog(@"Range: [%d, %d)", (int)annotation.range.location, (int)(annotation.range.location + annotation.range.length));
            for (MLKEntity *entity in entities) {
              if ([entity.entityType isEqualToString:MLKEntityExtractionEntityTypeDateTime]) {
                MLKDateTimeEntity *dateTimeEntity = entity.dateTimeEntity;
                NSLog(@"Granularity: %d", (int)dateTimeEntity.dateTimeGranularity);
                NSLog(@"DateTime: %@", dateTimeEntity.dateTime);
                break;
              } else if ([entity.entityType isEqualToString:MLKEntityExtractionEntityTypeFlightNumber]) {
                MLKFlightNumberEntity *flightNumberEntity = entity.flightNumberEntity;
                NSLog(@"Airline Code: %@", flightNumberEntity.airlineCode);
                NSLog(@"Flight number: %@", flightNumberEntity.flightNumber);
                break;
              } else if ([entity.entityType isEqualToString:MLKEntityExtractionEntityTypeMoney]) {
                MLKMoneyEntity *moneyEntity = entity.moneyEntity;
                NSLog(@"Currency: %@", moneyEntity.unnormalizedCurrency);
                NSLog(@"Integer Part: %d", (int)moneyEntity.integerPart);
                NSLog(@"Fractional Part: %d", (int)moneyEntity.fractionalPart);
                break;
              } else {
                // Add additional cases as needed.
                NSLog(@"Entity: %@", entity);
              }
            }