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

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

এনটিটি এক্সট্র্যাকশন বেস ডিটেক্টর অ্যাসেটগুলো অ্যাপ রান টাইমে স্ট্যাটিক্যালি লিঙ্ক করা হয়। এগুলো আপনার অ্যাপে প্রায় ১০.৭ মেগাবাইট জায়গা যোগ করে।

চেষ্টা করে দেখুন

  • এই API-টির একটি উদাহরণমূলক ব্যবহার দেখতে নমুনা অ্যাপটি ব্যবহার করে দেখুন।

শুরু করার আগে

  1. আপনার Podfile-এ নিম্নলিখিত ML Kit লাইব্রেরিগুলো অন্তর্ভুক্ত করুন:

    pod 'GoogleMLKit/EntityExtraction', '8.0.0'
    
  2. আপনার প্রোজেক্টের পডগুলো ইনস্টল বা আপডেট করার পর, সেটির .xcworkspace ব্যবহার করে আপনার Xcode প্রোজেক্টটি খুলুন। ML Kit, 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);
              }
            }