קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
אתם יכולים להשתמש ב-ML Kit כדי לזהות את השפה של מחרוזת טקסט. אפשר
את רמת הסמך הגבוהה ביותר במחרוזת וגם את דירוגי הסמך
של המחרוזת.
ערכת ML Kit מזהה טקסט ביותר מ-100 שפות שונות בסקריפטים המקוריים שלהן.
בנוסף, ניתן לזהות טקסט באותיות לטיניות עבור ערבית, בולגרית, סינית,
יוונית, הינדי, יפנית ורוסית. לצפייה
הרשימה המלאה של השפות והסקריפטים הנתמכים.
אחרי שמתקינים או מעדכנים את קבוצות ה-Pod של הפרויקט, פותחים את פרויקט Xcode באמצעות
.xcworkspace יש תמיכה ב-ML Kit ב-Xcode מגרסה 12.4 ואילך.
זיהוי השפה של מחרוזת
כדי לזהות את השפה של מחרוזת, נשתמש במופע של
LanguageIdentification, ולאחר מכן מעבירים את המחרוזת אל
identifyLanguage(for:).
לדוגמה:
Swift
letlanguageId=NaturalLanguage.languageIdentification()languageId.identifyLanguage(for:text){(languageCode,error)inifleterror=error{print("Failed with error: \(error)")return}ifletlanguageCode=languageCode,languageCode!="und"{print("Identified Language: \(languageCode)")}else{print("No language was identified")}}
Objective-C
MLKLanguageIdentification*languageId=[MLKLanguageIdentificationlanguageIdentification];[languageIdidentifyLanguageForText:textcompletion:^(NSString*_NullablelanguageCode,NSError*_Nullableerror){if(error!=nil){NSLog(@"Failed with error: %@",error.localizedDescription);return;}if(![languageCodeisEqualToString:@"und"]){NSLog(@"Identified Language: %@",languageCode);}else{NSLog(@"No language was identified");}}];
אם השיחה מצליחה,
קוד השפה BCP-47 הוא
מועבר ל-handler של ההשלמה האוטומטית, שמציין את שפת הטקסט. אם לא
ניתן לזהות באופן בטוח את השפה, הקוד und (לא ידוע) מועבר.
כברירת מחדל, ערכת ML Kit מחזירה ערך שאינו und רק כאשר היא מזהה
עם ערך סמך של 0.5 לפחות. אפשר לשנות את הסף הזה
על ידי העברת אובייקט LanguageIdentificationOptions אל
languageIdentification(options:):
כדי לקבל את ערכי הסמך של השפות הנפוצות ביותר במחרוזת, צריך לקבל את
המופע של LanguageIdentification ואז מעבירים את המחרוזת
identifyPossibleLanguages(for:).
לדוגמה:
Swift
letlanguageId=NaturalLanguage.languageIdentification()languageId.identifyPossibleLanguages(for:text){(identifiedLanguages,error)inifleterror=error{print("Failed with error: \(error)")return}guardletidentifiedLanguages=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=[MLKLanguageIdentificationlanguageIdentification];[languageIdidentifyPossibleLanguagesForText:textcompletion:^(NSArray*_NonnullidentifiedLanguages,NSError*_Nullableerror){if(error!=nil){NSLog(@"Failed with error: %@",error.localizedDescription);return;}if(identifiedLanguages.count==1&&[identifiedLanguages[0].languageCodeisEqualToString:@"und"]){NSLog(@"No language was identified");return;}NSMutableString*outputText=[NSMutableStringstringWithFormat:@"Identified Languages:"];for(MLKIdentifiedLanguage*languageinidentifiedLanguages){[outputTextappendFormat:@"\n(%@, %.2f)",language.languageCode,language.confidence];}NSLog(outputText);}];
אם הקריאה תתבצע בהצלחה, תישלח רשימה של IdentifiedLanguage אובייקטים אל
מטפל המשך. מכל אובייקט אפשר לקבל את קוד BCP-47 של השפה.
את רמת הסמך שהמחרוזת כתובה בשפה הזו. שימו לב
הערכים האלה מציינים את הביטחון שכל המחרוזת נמצאת
שפה; ML Kit לא מזהה שפות מרובות במחרוזת אחת.
כברירת מחדל, ערכת ML Kit מחזירה רק שפות עם ערכי ודאות של לפחות
0.01 כדי לשנות את הסף הזה צריך להעביר
אובייקט LanguageIdentificationOptions אל languageIdentification(options:):
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-09-04 (שעון UTC)."],[[["\u003cp\u003eML Kit can identify the language of a string of text and provide confidence scores for all possible languages, supporting over 100 languages.\u003c/p\u003e\n"],["\u003cp\u003eYou can get the most likely language of a string using the \u003ccode\u003eidentifyLanguage(for:)\u003c/code\u003e method or get confidence values for possible languages using the \u003ccode\u003eidentifyPossibleLanguages(for:)\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eAdjust the confidence threshold for language identification by passing a \u003ccode\u003eLanguageIdentificationOptions\u003c/code\u003e object to \u003ccode\u003elanguageIdentification(options:)\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo use ML Kit for language identification, include the \u003ccode\u003eGoogleMLKit/LanguageID\u003c/code\u003e pod in your Podfile and ensure your Xcode version is 12.4 or greater.\u003c/p\u003e\n"]]],["ML Kit can identify the language of text strings, supporting over 100 languages and romanized text for select languages. To use it, add the `GoogleMLKit/LanguageID` pod to your project. The `identifyLanguage(for:)` method returns the most likely language code, or \"und\" if undetermined. The method `identifyPossibleLanguages(for:)` provides confidence scores for multiple potential languages. Both methods allow setting a confidence threshold, with the default at 0.5 and 0.01 respectively.\n"],null,["You can use ML Kit to identify the language of a string of text. You can\nget the string's most likely language as well as confidence scores for all of the\nstring's possible languages.\n\nML Kit recognizes text in more than 100 different languages in their native scripts.\nIn addition, romanized text can be recognized for Arabic, Bulgarian, Chinese,\nGreek, Hindi, Japanese, and Russian. See the\n[complete list](/ml-kit/language/identification/langid-support) of supported languages and scripts.\n\n\u003cbr /\u003e\n\n| **Note:** ML Kit iOS APIs only run on 64-bit devices. If you build your app with 32-bit support, check the device's architecture before using this API.\n\nTry it out\n\n- Play around with [the sample app](https://github.com/googlesamples/mlkit/tree/master/ios/quickstarts/languageid) to see an example usage of this API.\n\nBefore you begin\n\n1. Include the following ML Kit pods in your Podfile: \n\n ```\n pod 'GoogleMLKit/LanguageID', '8.0.0'\n ```\n2. After you install or update your project's Pods, open your Xcode project using its `.xcworkspace`. ML Kit is supported in Xcode version 12.4 or greater.\n\nIdentify the language of a string\n\nTo identify the language of a string, get an instance of\n`LanguageIdentification`, and then pass the string to the\n`identifyLanguage(for:)` method.\n\nFor example: \n\nSwift \n\n```swift\nlet languageId = NaturalLanguage.languageIdentification()\n\nlanguageId.identifyLanguage(for: text) { (languageCode, error) in\n if let error = error {\n print(\"Failed with error: \\(error)\")\n return\n }\n if let languageCode = languageCode, languageCode != \"und\" {\n print(\"Identified Language: \\(languageCode)\")\n } else {\n print(\"No language was identified\")\n }\n}\n```\n\nObjective-C \n\n```objective-c\nMLKLanguageIdentification *languageId = [MLKLanguageIdentification languageIdentification];\n\n[languageId identifyLanguageForText:text\n completion:^(NSString * _Nullable languageCode,\n NSError * _Nullable error) {\n if (error != nil) {\n NSLog(@\"Failed with error: %@\", error.localizedDescription);\n return;\n }\n if (![languageCode isEqualToString:@\"und\"] ) {\n NSLog(@\"Identified Language: %@\", languageCode);\n } else {\n NSLog(@\"No language was identified\");\n }\n }];\n```\n\nIf the call succeeds, a\n[BCP-47 language code](//en.wikipedia.org/wiki/IETF_language_tag) is\npassed to the completion handler, indicating the language of the text. If no\nlanguage could be confidently detected, the code `und` (undetermined) is passed.\n\nBy default, ML Kit returns a non-`und` value only when it identifies the\nlanguage with a confidence value of at least 0.5. You can change this threshold\nby passing a `LanguageIdentificationOptions` object to\n`languageIdentification(options:)`: \n\nSwift \n\n```swift\nlet options = LanguageIdentificationOptions(confidenceThreshold: 0.4)\nlet languageId = NaturalLanguage.languageIdentification(options: options)\n```\n\nObjective-C \n\n```objective-c\nMLKLanguageIdentificationOptions *options =\n [[MLKLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4];\nMLKLanguageIdentification *languageId =\n [MLKLanguageIdentification languageIdentificationWithOptions:options];\n```\n\nGet the possible languages of a string\n\nTo get the confidence values of a string's most likely languages, get an\ninstance of `LanguageIdentification` and then pass the string to the\n`identifyPossibleLanguages(for:)` method.\n\nFor example: \n\nSwift \n\n```swift\nlet languageId = NaturalLanguage.languageIdentification()\n\nlanguageId.identifyPossibleLanguages(for: text) { (identifiedLanguages, error) in\n if let error = error {\n print(\"Failed with error: \\(error)\")\n return\n }\n guard let identifiedLanguages = identifiedLanguages,\n !identifiedLanguages.isEmpty,\n identifiedLanguages[0].languageCode != \"und\"\n else {\n print(\"No language was identified\")\n return\n }\n\n print(\"Identified Languages:\\n\" +\n identifiedLanguages.map {\n String(format: \"(%@, %.2f)\", $0.languageCode, $0.confidence)\n }.joined(separator: \"\\n\"))\n}\n```\n\nObjective-C \n\n```objective-c\nMLKLanguageIdentification *languageId = [MLKLanguageIdentification languageIdentification];\n\n[languageId identifyPossibleLanguagesForText:text\n completion:^(NSArray * _Nonnull identifiedLanguages,\n NSError * _Nullable error) {\n if (error != nil) {\n NSLog(@\"Failed with error: %@\", error.localizedDescription);\n return;\n }\n if (identifiedLanguages.count == 1\n && [identifiedLanguages[0].languageCode isEqualToString:@\"und\"] ) {\n NSLog(@\"No language was identified\");\n return;\n }\n NSMutableString *outputText = [NSMutableString stringWithFormat:@\"Identified Languages:\"];\n for (MLKIdentifiedLanguage *language in identifiedLanguages) {\n [outputText appendFormat:@\"\\n(%@, %.2f)\", language.languageCode, language.confidence];\n }\n NSLog(outputText);\n}];\n```\n\nIf the call succeeds, a list of `IdentifiedLanguage` objects is passed to the\ncontinuation handler. From each object, you can get the language's BCP-47 code\nand the confidence that the string is in that language. Note that\nthese values indicate the confidence that the entire string is in the given\nlanguage; ML Kit doesn't identify multiple languages in a single string.\n\nBy default, ML Kit returns only languages with confidence values of at least\n0.01. You can change this threshold by passing a\n`LanguageIdentificationOptions` object to `languageIdentification(options:)`: \n\nSwift \n\n```swift\nlet options = LanguageIdentificationOptions(confidenceThreshold: 0.4)\nlet languageId = NaturalLanguage.languageIdentification(options: options)\n```\n\nObjective-C \n\n```objective-c\nMLKLanguageIdentificationOptions *options =\n [[MLKLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4];\nMLKLanguageIdentification *languageId =\n [MLKLanguageIdentification languageIdentificationWithOptions:options];\n```\n\nIf no language meets this threshold, the list has one item, with the value\n`und`."]]