با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
می توانید از ML Kit برای شناسایی زبان رشته ای از متن استفاده کنید. میتوانید محتملترین زبان رشته را دریافت کنید یا برای همه زبانهای احتمالی رشته امتیازات اطمینان کسب کنید.
ML Kit متن را به 103 زبان مختلف در اسکریپت های اصلی آنها تشخیص می دهد. علاوه بر این، متن رمانسازی شده را میتوان به زبانهای عربی، بلغاری، چینی، یونانی، هندی، ژاپنی و روسی تشخیص داد.
قبل از شروع
اگر قبلاً Firebase را به برنامه خود اضافه نکرده اید، این کار را با دنبال کردن مراحل راهنمای شروع کار انجام دهید.
کتابخانه های ML Kit را در پادفایل خود قرار دهید:
pod 'Firebase/MLNaturalLanguage', '6.25.0'
pod 'Firebase/MLNLLanguageID', '6.25.0'
پس از نصب یا بهروزرسانی Pods پروژه، حتماً پروژه Xcode خود را با استفاده از .xcworkspace آن باز کنید.
در برنامه خود، Firebase را وارد کنید:
سویفت
importFirebase
هدف-C
@importFirebase;
زبان یک رشته را مشخص کنید
برای شناسایی زبان یک رشته، یک نمونه از LanguageIdentification دریافت کنید و سپس رشته را به متد identifyLanguage(for:) منتقل کنید.
به عنوان مثال:
سویفت
letlanguageId=NaturalLanguage.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")}}
هدف-C
FIRNaturalLanguage*naturalLanguage=[FIRNaturalLanguagenaturalLanguage];FIRLanguageIdentification*languageId=[naturalLanguagelanguageIdentification];[languageIdidentifyLanguageForText:textcompletion:^(NSString*_NullablelanguageCode,NSError*_Nullableerror){if(error!=nil){NSLog(@"Failed with error: %@",error.localizedDescription);return;}if(languageCode!=nil && ![languageCodeisEqualToString:@"und"]){NSLog(@"Identified Language: %@",languageCode);}else{NSLog(@"No language was identified");}}];
اگر تماس با موفقیت انجام شود، یک کد زبان BCP-47 به کنترل کننده تکمیل ارسال می شود که زبان متن را نشان می دهد. لیست کامل زبان های پشتیبانی شده را ببینید. اگر هیچ زبانی به طور مطمئن شناسایی نشد، کد und (نامشخص) ارسال می شود.
به طور پیشفرض، ML Kit فقط زمانی یک مقدار غیر und برمیگرداند که زبان را با مقدار اطمینان حداقل 0.5 شناسایی کند. شما می توانید این آستانه را با ارسال یک شی LanguageIdentificationOptions به languageIdentification(options:) تغییر دهید:
برای به دست آوردن مقادیر اطمینان از محتمل ترین زبان های رشته، یک نمونه از LanguageIdentification دریافت کنید و سپس رشته را به متد identifyPossibleLanguages(for:) منتقل کنید.
به عنوان مثال:
سویفت
letlanguageId=NaturalLanguage.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"))}
هدف-C
FIRNaturalLanguage*naturalLanguage=[FIRNaturalLanguagenaturalLanguage];FIRLanguageIdentification*languageId=[naturalLanguagelanguageIdentification];[languageIdidentifyPossibleLanguagesForText:textcompletion:^(NSArray<FIRIdentifiedLanguage*>*_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(FIRIdentifiedLanguage*languageinidentifiedLanguages){[outputTextappendFormat:@"\n(%@, %.2f)",language.languageCode,language.confidence];}NSLog(outputText);}];
اگر تماس با موفقیت انجام شود، لیستی از اشیاء IdentifiedLanguage به کنترل کننده ادامه ارسال می شود. از هر شی، می توانید کد BCP-47 زبان و اطمینان از اینکه رشته در آن زبان است را دریافت کنید. لیست کامل زبان های پشتیبانی شده را ببینید. توجه داشته باشید که این مقادیر اطمینان از اینکه کل رشته در زبان داده شده است را نشان می دهد. ML Kit چندین زبان را در یک رشته شناسایی نمی کند.
به طور پیشفرض، ML Kit فقط زبانهایی را با مقادیر اطمینان حداقل 0.01 برمیگرداند. شما می توانید این آستانه را با ارسال یک شی LanguageIdentificationOptions به languageIdentification(options:) تغییر دهید:
تاریخ آخرین بهروزرسانی 2025-09-06 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-06 بهوقت ساعت هماهنگ جهانی."],[],[],null,["You can use ML Kit to identify the language of a string of text. You can\nget the string's most likely language or get confidence scores for all of the\nstring's possible languages.\n\nML Kit recognizes text in 103 different languages in their native scripts.\nIn addition, romanized text can be recognized for Arabic, Bulgarian, Chinese,\nGreek, Hindi, Japanese, and Russian.\n\n\u003cbr /\u003e\n\nBefore you begin\n\n1. If you have not already added Firebase to your app, do so by following the steps in the [getting started guide](/docs/ios/setup).\n2. Include the ML Kit libraries in your Podfile: \n\n ```\n pod 'Firebase/MLNaturalLanguage', '6.25.0'\n pod 'Firebase/MLNLLanguageID', '6.25.0'\n ```\n After you install or update your project's Pods, be sure to open your Xcode project using its `.xcworkspace`.\n3. In your app, import Firebase: \n\n Swift \n\n ```swift\n import Firebase\n ```\n\n Objective-C \n\n ```objective-c\n @import Firebase;\n ```\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 let languageId = NaturalLanguage.naturalLanguage().languageIdentification()\n\n languageId.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\nObjective-C \n\n FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];\n FIRLanguageIdentification *languageId = [naturalLanguage 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 != nil\n && ![languageCode isEqualToString:@\"und\"] ) {\n NSLog(@\"Identified Language: %@\", languageCode);\n } else {\n NSLog(@\"No language was identified\");\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. See the\n[complete list of supported languages](/docs/ml-kit/langid-support). 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 let options = LanguageIdentificationOptions(confidenceThreshold: 0.4)\n let languageId = NaturalLanguage.naturalLanguage().languageIdentification(options: options)\n\nObjective-C \n\n FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];\n FIRLanguageIdentificationOptions *options =\n [[FIRLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4];\n FIRLanguageIdentification *languageId =\n [naturalLanguage languageIdentificationWithOptions:options];\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 let languageId = NaturalLanguage.naturalLanguage().languageIdentification()\n\n languageId.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\nObjective-C \n\n FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];\n FIRLanguageIdentification *languageId = [naturalLanguage languageIdentification];\n\n [languageId identifyPossibleLanguagesForText:text\n completion:^(NSArray\u003cFIRIdentifiedLanguage *\u003e * _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 (FIRIdentifiedLanguage *language in identifiedLanguages) {\n [outputText appendFormat:@\"\\n(%@, %.2f)\", language.languageCode, language.confidence];\n }\n NSLog(outputText);\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. See the\n[complete list of supported languages](/docs/ml-kit/langid-support). 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 let options = LanguageIdentificationOptions(confidenceThreshold: 0.4)\n let languageId = NaturalLanguage.naturalLanguage().languageIdentification(options: options)\n\nObjective-C \n\n FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];\n FIRLanguageIdentificationOptions *options =\n [[FIRLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4];\n FIRLanguageIdentification *languageId =\n [naturalLanguage languageIdentificationWithOptions:options];\n\nIf no language meets this threshold, the list will have one item, with the value\n`und`."]]