ML Kit を使用してテキストの言語を識別する(iOS)
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
ML Kit を使用すると、テキストの文字列の言語を識別できます。文字列の言語として特に可能性の高いものを取得することも、文字列の言語として可能性のあるものすべての信頼スコアを取得することもできます。
ML Kit では、103 種類の言語がネイティブ スクリプトのテキストで認識されます。また、アラビア語、ブルガリア語、中国語、ギリシャ語、ヒンディー語、日本語、ロシア語の場合はローマ字化されたテキストが認識されます。
始める前に
- まだアプリに Firebase を追加していない場合は、スタートガイドの手順に沿って追加してください。
- Podfile に ML Kit ライブラリを含めます。
pod 'Firebase/MLNaturalLanguage', '6.25.0'
pod 'Firebase/MLNLLanguageID', '6.25.0'
プロジェクトの Pod をインストールまたは更新した後に、.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 では、1 つの文字列内の複数の言語は識別されません。
デフォルトでは、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];
このしきい値を満たす言語がない場合、リストには 1 つの項目が含まれ、その値は und
になります。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は 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`."]]