텍스트 분류
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
텍스트를 분석하고 텍스트에 적용되는 콘텐츠 카테고리 목록을 반환합니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],[],[],[],null,["# Classify text\n\nAnalyze text and return a list of content categories that apply to the text.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Classifying Content](/natural-language/docs/classifying-text)\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Natural Language, see\n[Natural Language client libraries](/natural-language/docs/reference/libraries).\n\n\nFor more information, see the\n[Natural Language Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/language/latest/apiv1).\n\n\nTo authenticate to Natural Language, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n func classifyText(ctx context.Context, client *language.Client, text string) (*languagepb.ClassifyTextResponse, error) {\n \treturn client.ClassifyText(ctx, &languagepb.ClassifyTextRequest{\n \t\tDocument: &languagepb.Document{\n \t\t\tSource: &languagepb.Document_Content{\n \t\t\t\tContent: text,\n \t\t\t},\n \t\t\tType: languagepb.Document_PLAIN_TEXT,\n \t\t},\n \t\tClassificationModelOptions: &languagepb.ClassificationModelOptions{\n \t\t\tModelType: &languagepb.ClassificationModelOptions_V2Model_{\n \t\t\t\tV2Model: &languagepb.ClassificationModelOptions_V2Model{\n \t\t\t\t\tContentCategoriesVersion: languagepb.ClassificationModelOptions_V2Model_V2,\n \t\t\t\t},\n \t\t\t},\n \t\t},\n \t})\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Natural Language, see\n[Natural Language client libraries](/natural-language/docs/reference/libraries).\n\n\nFor more information, see the\n[Natural Language Java API\nreference documentation](/java/docs/reference/google-cloud-language/latest/overview).\n\n\nTo authenticate to Natural Language, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Instantiate the Language client com.google.cloud.language.v2.LanguageServiceClient\n try (LanguageServiceClient language = LanguageServiceClient.create()) {\n // Set content to the text string\n Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();\n ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build();\n // Detect categories in the given text\n ClassifyTextResponse response = language.classifyText(request);\n\n for (ClassificationCategory category : response.getCategoriesList()) {\n System.out.printf(\n \"Category name : %s, Confidence : %.3f\\n\",\n category.getName(), category.getConfidence());\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Natural Language, see\n[Natural Language client libraries](/natural-language/docs/reference/libraries).\n\n\nFor more information, see the\n[Natural Language Node.js API\nreference documentation](/nodejs/docs/reference/language/latest).\n\n\nTo authenticate to Natural Language, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client library\n const language = require('https://cloud.google.com/nodejs/docs/reference/language/latest/overview.html');\n\n // Creates a client\n const client = new language.https://cloud.google.com/nodejs/docs/reference/language/latest/overview.html();\n\n /**\n * TODO(developer): Uncomment the following line to run this code.\n */\n // const text = 'Your text to analyze, e.g. Hello, world!';\n\n // Prepares a document, representing the provided text\n const document = {\n content: text,\n type: 'PLAIN_TEXT',\n };\n\n const classificationModelOptions = {\n v2Model: {\n contentCategoriesVersion: 'V2',\n },\n };\n\n // Classifies text in the document\n const [classification] = await client.classifyText({\n document,\n classificationModelOptions,\n });\n console.log('Categories:');\n classification.categories.forEach(category =\u003e {\n console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);\n });\n\n### PHP\n\n\nTo learn how to install and use the client library for Natural Language, see\n[Natural Language client libraries](/natural-language/docs/reference/libraries).\n\n\nTo authenticate to Natural Language, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Language\\V1\\ClassifyTextRequest;\n use Google\\Cloud\\Language\\V1\\Client\\LanguageServiceClient;\n use Google\\Cloud\\Language\\V1\\Document;\n use Google\\Cloud\\Language\\V1\\Document\\Type;\n\n /**\n * @param string $text The text to analyze\n */\n function classify_text(string $text): void\n {\n // Make sure we have enough words (20+) to call classifyText\n if (str_word_count($text) \u003c 20) {\n printf('20+ words are required to classify text.' . PHP_EOL);\n return;\n }\n $languageServiceClient = new LanguageServiceClient();\n\n // Create a new Document, add text as content and set type to PLAIN_TEXT\n $document = (new Document())\n -\u003esetContent($text)\n -\u003esetType(Type::PLAIN_TEXT);\n\n // Call the analyzeSentiment function\n $request = (new ClassifyTextRequest())\n -\u003esetDocument($document);\n $response = $languageServiceClient-\u003eclassifyText($request);\n $categories = $response-\u003egetCategories();\n // Print document information\n foreach ($categories as $category) {\n printf('Category Name: %s' . PHP_EOL, $category-\u003egetName());\n printf('Confidence: %s' . PHP_EOL, $category-\u003egetConfidence());\n print(PHP_EOL);\n }\n }\n\n### Python\n\n\nTo learn how to install and use the client library for Natural Language, see\n[Natural Language client libraries](/natural-language/docs/reference/libraries).\n\n\nFor more information, see the\n[Natural Language Python API\nreference documentation](/python/docs/reference/language/latest).\n\n\nTo authenticate to Natural Language, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import language_v1\n\n\n def sample_classify_text(text_content):\n \"\"\"\n Classifying Content in a String\n\n Args:\n text_content The text content to analyze.\n \"\"\"\n\n client = language_v1.LanguageServiceClient()\n\n # text_content = \"That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.\"\n\n # Available types: PLAIN_TEXT, HTML\n type_ = language_v1.Document.Type.PLAIN_TEXT\n\n # Optional. If not specified, the language is automatically detected.\n # For list of supported languages:\n # https://cloud.google.com/natural-language/docs/languages\n language = \"en\"\n document = {\"content\": text_content, \"type_\": type_, \"language\": language}\n\n content_categories_version = (\n language_v1.https://cloud.google.com/python/docs/reference/language/latest/google.cloud.language_v1.types.ClassificationModelOptions.html.https://cloud.google.com/python/docs/reference/language/latest/google.cloud.language_v1.types.ClassificationModelOptions.V2Model.html.https://cloud.google.com/python/docs/reference/language/latest/google.cloud.language_v1.types.ClassificationModelOptions.V2Model.ContentCategoriesVersion.html.V2\n )\n response = client.https://cloud.google.com/python/docs/reference/language/latest/google.cloud.language_v1.services.language_service.LanguageServiceClient.html#google_cloud_language_v1_services_language_service_LanguageServiceClient_classify_text(\n request={\n \"document\": document,\n \"classification_model_options\": {\n \"v2_model\": {\"content_categories_version\": content_categories_version}\n },\n }\n )\n # Loop through classified categories returned from the API\n for category in response.categories:\n # Get the name of the category representing the document.\n # See the predefined taxonomy of categories:\n # https://cloud.google.com/natural-language/docs/categories\n print(f\"Category name: {category.name}\")\n # Get the confidence. Number representing how certain the classifier\n # is that this category represents the provided text.\n print(f\"Confidence: {category.confidence}\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=language)."]]