برای تجزیه و تحلیل یک متن و استخراج موجودیتهای موجود در آن، API استخراج موجودیت ML Kit را با ارسال مستقیم متن به متد annotateText:completion: فراخوانی کنید. همچنین میتوان یک شیء اختیاری EntityExtractionParams را ارسال کرد که شامل گزینههای پیکربندی دیگری مانند زمان مرجع، منطقه زمانی یا یک فیلتر برای محدود کردن جستجوی زیرمجموعهای از انواع موجودیتها است. API لیستی از اشیاء EntityAnnotation را برمیگرداند که حاوی اطلاعاتی در مورد هر موجودیت است.
داراییهای آشکارساز پایگاه داده استخراج موجودیت در زمان اجرای برنامه به صورت ایستا متصل میشوند. آنها حدود ۱۰.۷ مگابایت به حجم برنامه شما اضافه میکنند.
امتحانش کن.
- برای مشاهدهی نحوهی استفاده از این API، با برنامهی نمونه کار کنید.
قبل از اینکه شروع کنی
کتابخانههای ML Kit زیر را در Podfile خود قرار دهید:
pod 'GoogleMLKit/EntityExtraction', '8.0.0'پس از نصب یا بهروزرسانی Pods پروژه خود، پروژه Xcode خود را با استفاده از فضای کاری .xc باز کنید. ML Kit در Xcode نسخه ۱۳.۲.۱ یا بالاتر پشتیبانی میشود.
استخراج موجودیتها از متن
برای استخراج موجودیتها از متن، ابتدا یک شیء 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. }];
پس از دانلود مدل، یک رشته و یک MLKEntityExtractionParams اختیاری را به متد annotate ارسال کنید.
سویفت
// 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); } }