คุณใช้ ML Kit เพื่อระบุภาษาของสตริงข้อความได้ คุณสามารถดูภาษาที่เป็นไปได้มากที่สุดของสตริง รวมถึงคะแนนความเชื่อมั่นสำหรับภาษาที่เป็นไปได้ทั้งหมดของสตริง
ML Kit จดจำข้อความในภาษาต่างๆ มากกว่า 100 ภาษาในสคริปต์ภาษาของคุณให้ นอกจากนี้ ยังจดจำข้อความแบบอักษรโรมันในภาษาอาหรับ บัลแกเรีย จีน กรีก ฮินดี ญี่ปุ่น และรัสเซียได้ด้วย ดูรายการทั้งหมดของภาษาและสคริปต์ที่รองรับ
ลองเลย
- ลองใช้แอปตัวอย่างเพื่อดูตัวอย่างการใช้งาน API นี้
ก่อนเริ่มต้น
- รวมพ็อด ML Kit ต่อไปนี้ใน Podfile:
pod 'GoogleMLKit/LanguageID', '3.2.0'
- หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้
.xcworkspace
ของโปรเจ็กต์นั้น Xcode เวอร์ชัน 12.4 ขึ้นไปรองรับ ML Kit
ระบุภาษาของสตริง
หากต้องการระบุภาษาของสตริง ให้รับอินสแตนซ์ของ LanguageIdentification
จากนั้นส่งสตริงไปยังเมธอด identifyLanguage(for:)
เช่น
Swift
let languageId = NaturalLanguage.languageIdentification() languageId.identifyLanguage(for: text) { (languageCode, error) in if let error = error { print("Failed with error: \(error)") return } if let languageCode = languageCode, languageCode != "und" { print("Identified Language: \(languageCode)") } else { print("No language was identified") } }
Objective-C
MLKLanguageIdentification *languageId = [MLKLanguageIdentification languageIdentification]; [languageId identifyLanguageForText:text completion:^(NSString * _Nullable languageCode, NSError * _Nullable error) { if (error != nil) { NSLog(@"Failed with error: %@", error.localizedDescription); return; } if (![languageCode isEqualToString:@"und"] ) { NSLog(@"Identified Language: %@", languageCode); } else { NSLog(@"No language was identified"); } }];
หากเรียกสำเร็จ ระบบจะส่งรหัสภาษา BCP-47 ไปยังเครื่องจัดการการเสร็จสมบูรณ์เพื่อระบุภาษาของข้อความ หากไม่สามารถตรวจจับภาษาได้อย่างมั่นใจ จะมีการส่งผ่านรหัส und
(ไม่กำหนด)
โดยค่าเริ่มต้น ML Kit จะแสดงผลค่าที่ไม่ใช่ und
เมื่อระบุภาษาที่มีค่าความเชื่อมั่นอย่างน้อย 0.5 เท่านั้น คุณเปลี่ยนเกณฑ์นี้ได้โดยการส่งออบเจ็กต์ LanguageIdentificationOptions
ไปยัง languageIdentification(options:)
:
Swift
let options = LanguageIdentificationOptions(confidenceThreshold: 0.4) let languageId = NaturalLanguage.languageIdentification(options: options)
Objective-C
MLKLanguageIdentificationOptions *options = [[MLKLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4]; MLKLanguageIdentification *languageId = [MLKLanguageIdentification languageIdentificationWithOptions:options];
ดูภาษาที่เป็นไปได้ของสตริง
หากต้องการค่าความเชื่อมั่นของภาษาที่มีแนวโน้มมากที่สุดของสตริง ให้รับอินสแตนซ์ของ LanguageIdentification
จากนั้นส่งสตริงไปยังเมธอด identifyPossibleLanguages(for:)
เช่น
Swift
let languageId = NaturalLanguage.languageIdentification() languageId.identifyPossibleLanguages(for: text) { (identifiedLanguages, error) in if let error = error { print("Failed with error: \(error)") return } guard let identifiedLanguages = identifiedLanguages, !identifiedLanguages.isEmpty, identifiedLanguages[0].languageCode != "und" else { print("No language was identified") return } print("Identified Languages:\n" + identifiedLanguages.map { String(format: "(%@, %.2f)", $0.languageCode, $0.confidence) }.joined(separator: "\n")) }
Objective-C
MLKLanguageIdentification *languageId = [MLKLanguageIdentification languageIdentification]; [languageId identifyPossibleLanguagesForText:text completion:^(NSArray* _Nonnull identifiedLanguages, NSError * _Nullable error) { if (error != nil) { NSLog(@"Failed with error: %@", error.localizedDescription); return; } if (identifiedLanguages.count == 1 && [identifiedLanguages[0].languageCode isEqualToString:@"und"] ) { NSLog(@"No language was identified"); return; } NSMutableString *outputText = [NSMutableString stringWithFormat:@"Identified Languages:"]; for (MLKIdentifiedLanguage *language in identifiedLanguages) { [outputText appendFormat:@"\n(%@, %.2f)", language.languageCode, language.confidence]; } NSLog(outputText); }];
หากการเรียกสำเร็จ ระบบจะส่งรายการออบเจ็กต์ IdentifiedLanguage
รายการไปยังเครื่องจัดการความต่อเนื่อง จากออบเจ็กต์แต่ละรายการ คุณสามารถดูรหัส BCP-47 ของภาษา
และความมั่นใจว่าสตริงนั้นเป็นภาษานั้น โปรดทราบว่าค่าเหล่านี้บ่งบอกถึงความมั่นใจว่าสตริงทั้งสตริงเป็นภาษาที่ระบุ ML Kit ไม่ได้ระบุหลายภาษาในสตริงเดียว
โดยค่าเริ่มต้น ML Kit จะแสดงผลเฉพาะภาษาที่มีค่าความเชื่อมั่นอย่างน้อย 0.01 คุณเปลี่ยนเกณฑ์นี้ได้โดยการส่งออบเจ็กต์ LanguageIdentificationOptions
ไปยัง languageIdentification(options:)
ดังนี้
Swift
let options = LanguageIdentificationOptions(confidenceThreshold: 0.4) let languageId = NaturalLanguage.languageIdentification(options: options)
Objective-C
MLKLanguageIdentificationOptions *options = [[MLKLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4]; MLKLanguageIdentification *languageId = [MLKLanguageIdentification languageIdentificationWithOptions:options];
หากไม่มีภาษาที่ตรงตามเกณฑ์นี้ รายการดังกล่าวจะมี 1 รายการที่มีค่า und