คุณใช้ ML Kit เพื่อระบุภาษาของสตริงข้อความได้ คุณดูภาษาที่มีความเป็นไปได้สูงของสตริง ตลอดจนคะแนนความเชื่อมั่นสําหรับภาษาที่เป็นไปได้ทั้งหมดของสตริงได้
ML Kit จะจดจําข้อความในภาษาต่างๆ มากกว่า 100 ภาษาในสคริปต์เนทีฟ นอกจากนี้ ข้อความที่มีตัวอักษรโรมันยังรู้จักภาษาอาหรับ บัลแกเรีย จีน กรีก ฮินดี ญี่ปุ่น และรัสเซียด้วย ดูรายการทั้งหมดของภาษาและสคริปต์ที่รองรับ
ลองใช้งาน
- ลองใช้แอปตัวอย่างเพื่อดูตัวอย่างการใช้งาน API นี้
ข้อควรทราบก่อนที่จะเริ่มต้น
- รวมพ็อด ML Kit ต่อไปนี้ใน Podfile
pod 'GoogleMLKit/LanguageID', '3.2.0'
- หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์ ให้เปิดโปรเจ็กต์ Xcode โดยใช้
.xcworkspace
XKit เวอร์ชัน 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