iOS에서 ML Kit를 사용하여 텍스트의 언어 식별
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
ML Kit를 사용하면 텍스트 문자열의 언어를 식별할 수 있습니다. 문자열의 언어일 가능성이 있는 언어를 가져오거나 문자열의 가능한 모든 언어에 대한 신뢰도 점수를 가져올 수 있습니다.
ML Kit는 네이티브 스크립트에서 103개의 다른 언어로 된 텍스트를 인식합니다.
또한 그리스어, 러시아어, 불가리아어, 아랍어, 일본어, 중국어, 힌디어로 된 로마자 텍스트도 인식할 수 있습니다.
시작하기 전에
- 앱에 Firebase를 아직 추가하지 않았다면 시작 가이드의 단계에 따라 추가합니다.
- Podfile에 ML Kit 라이브러리를 포함합니다.
pod 'Firebase/MLNaturalLanguage', '6.25.0'
pod 'Firebase/MLNLLanguageID', '6.25.0'
프로젝트의 포드를 설치하거나 업데이트한 후 .xcworkspace
를 사용하여 Xcode 프로젝트를 열어야 합니다.
- 앱에서 Firebase를 가져옵니다.
Objective-C
@import Firebase;
문자열의 언어 식별
문자열의 언어를 식별하려면 LanguageIdentification
의 인스턴스를 가져온 후 문자열을 identifyLanguage(for:)
메서드로 전달합니다.
예를 들면 다음과 같습니다.
Swift
let languageId = NaturalLanguage.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
FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];
FIRLanguageIdentification *languageId = [naturalLanguage languageIdentification];
[languageId identifyLanguageForText:text
completion:^(NSString * _Nullable languageCode,
NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Failed with error: %@", error.localizedDescription);
return;
}
if (languageCode != nil
&& ![languageCode isEqualToString:@"und"] ) {
NSLog(@"Identified Language: %@", languageCode);
} else {
NSLog(@"No language was identified");
}
}];
호출이 성공하면 BCP-47 언어 코드가 완료 핸들러로 전달되어 텍스트의 언어를 표시합니다. 지원 언어의 전체 목록을 참조하세요. 언어를 확실하게 감지할 수 없는 경우 und
(미확인) 코드가 전달됩니다.
기본적으로 ML Kit는 신뢰값이 0.5 이상인 언어를 식별하는 경우에만 und
가 아닌 값을 반환합니다. LanguageIdentificationOptions
객체를 languageIdentification(options:)
에 전달하여 이 기준을 변경할 수 있습니다.
Swift
let options = LanguageIdentificationOptions(confidenceThreshold: 0.4)
let languageId = NaturalLanguage.naturalLanguage().languageIdentification(options: options)
Objective-C
FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];
FIRLanguageIdentificationOptions *options =
[[FIRLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4];
FIRLanguageIdentification *languageId =
[naturalLanguage languageIdentificationWithOptions:options];
가능한 문자열 언어 가져오기
문자열의 언어일 가능성이 있는 언어의 신뢰도 값을 가져오려면 LanguageIdentification
의 인스턴스를 가져온 후 문자열을 identifyPossibleLanguages(for:)
메서드에 전달합니다.
예를 들면 다음과 같습니다.
Swift
let languageId = NaturalLanguage.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
FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];
FIRLanguageIdentification *languageId = [naturalLanguage languageIdentification];
[languageId identifyPossibleLanguagesForText:text
completion:^(NSArray<FIRIdentifiedLanguage *> * _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 (FIRIdentifiedLanguage *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.naturalLanguage().languageIdentification(options: options)
Objective-C
FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];
FIRLanguageIdentificationOptions *options =
[[FIRLanguageIdentificationOptions alloc] initWithConfidenceThreshold:0.4];
FIRLanguageIdentification *languageId =
[naturalLanguage languageIdentificationWithOptions:options];
이 기준을 충족하는 언어가 없으면 목록에 und
값이 포함된 항목 하나만 표시됩니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-16(UTC)
[null,null,["최종 업데이트: 2025-08-16(UTC)"],[],[],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`."]]