Mengklasifikasikan teks
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menganalisis teks dan menampilkan daftar kategori konten yang berlaku untuk teks tersebut.
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]