iOS-এ ML Kit-এর সাহায্যে সত্তা এক্সট্র্যাক্ট করুন

পাঠ্যের একটি অংশ বিশ্লেষণ করতে এবং এর মধ্যে থাকা সত্তাগুলিকে বের করতে, পাঠ্যটিকে সরাসরি তার annotateText:completion: পদ্ধতিতে পাস করে ML Kit সত্তা নিষ্কাশন API চালু করুন। এটি একটি ঐচ্ছিক EntityExtractionParams অবজেক্টে পাস করাও সম্ভব যেখানে অন্যান্য কনফিগারেশন বিকল্প রয়েছে যেমন একটি রেফারেন্স টাইম, টাইমজোন বা একটি ফিল্টার সত্তার প্রকারের উপসেটের জন্য অনুসন্ধান সীমিত করতে। API প্রতিটি সত্তা সম্পর্কে তথ্য সম্বলিত EntityAnnotation অবজেক্টের একটি তালিকা প্রদান করে।

সত্তা নিষ্কাশন বেস ডিটেক্টর সম্পদগুলি অ্যাপ্লিকেশান চালানোর সময়ে স্ট্যাটিকভাবে লিঙ্ক করা হয়। তারা আপনার অ্যাপে প্রায় 10.7MB যোগ করে।

চেষ্টা কর

তুমি শুরু করার আগে

  1. আপনার পডফাইলে নিম্নলিখিত এমএল কিট লাইব্রেরিগুলি অন্তর্ভুক্ত করুন:

    pod 'GoogleMLKit/EntityExtraction', '3.2.0'
    
  2. আপনি আপনার প্রোজেক্টের পড ইনস্টল বা আপডেট করার পরে, আপনার Xcode প্রকল্পটি .xcworkspace ব্যবহার করে খুলুন। ML কিট Xcode সংস্করণ 13.2.1 বা উচ্চতর সমর্থিত।

টেক্সট থেকে সত্তা বের করুন

টেক্সট থেকে সত্তা বের করতে, প্রথমে ভাষা নির্দিষ্ট করে একটি EntityExtractorOptions অবজেক্ট তৈরি করুন এবং একটি EntityExtractor ইনস্ট্যান্টিয়েট করতে এটি ব্যবহার করুন:

সুইফট

// 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)

উদ্দেশ্য গ

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

MLKEntityExtractor *entityExtractor = 
    [MLKEntityExtractor entityExtractorWithOptions:options];

পরবর্তী, নিশ্চিত করুন যে প্রয়োজনীয় ভাষা মডেল ডিভাইসে ডাউনলোড করা হয়েছে:

সুইফট

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

উদ্দেশ্য গ

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

মডেলটি ডাউনলোড হয়ে গেলে, annotate পদ্ধতিতে একটি স্ট্রিং এবং একটি ঐচ্ছিক MLKEntityExtractionParams পাস করুন।

সুইফট

// 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.
    }
)

উদ্দেশ্য গ

// 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.
}

স্বীকৃত সত্তা সম্পর্কে তথ্য পুনরুদ্ধার করতে টীকা ফলাফলের উপর লুপ.

সুইফট

// 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);
    }
  }
}

উদ্দেশ্য গ

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);
              }
            }