Clasificar contenido de archivos de Cloud Storage
Organízate con las colecciones
Guarda y clasifica el contenido según tus preferencias.
Analiza un archivo almacenado en Google Cloud Storage y devuelve una lista de categorías de contenido que se aplican al texto encontrado en el documento.
Investigar más
Para obtener documentación detallada que incluya este código de muestra, consulta lo siguiente:
Código de ejemplo
A menos que se indique lo contrario, el contenido de esta página está sujeto a la licencia Reconocimiento 4.0 de Creative Commons y las muestras de código están sujetas a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio web de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Es fácil de entender","easyToUnderstand","thumb-up"],["Me ofreció una solución al problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Es difícil de entender","hardToUnderstand","thumb-down"],["La información o el código de muestra no son correctos","incorrectInformationOrSampleCode","thumb-down"],["Me faltan las muestras o la información que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[],[],null,["Analyze a file stored in Google Cloud Storage and return a list of content categories that apply to the text found in the document\n\nExplore further\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\nGo\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 classifyTextFromGCS(ctx context.Context, gcsURI string) (*languagepb.ClassifyTextResponse, error) {\n \treturn client.ClassifyText(ctx, &languagepb.ClassifyTextRequest{\n \t\tDocument: &languagepb.Document{\n \t\t\tSource: &languagepb.Document_GcsContentUri{\n \t\t\t\tGcsContentUri: gcsURI,\n \t\t\t},\n \t\t\tType: languagepb.Document_PLAIN_TEXT,\n \t\t},\n \t})\n }\n\nJava\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 the GCS content URI path\n Document doc =\n Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();\n ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build();\n // Detect categories in the given file\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\nNode.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.htmluage');\n\n // Creates a client.\n const client = new lhttps://cloud.google.com/nodejs/docs/reference/language/latest/overview.htmlhttps://cloud.google.com/nodejs/docs/reference/language/latest/overview.htmlceClient();\n\n /**\n * TODO(developer): Uncomment the following lines to run this code\n */\n // const bucketName = 'Your bucket name, e.g. my-bucket';\n // const fileName = 'Your file name, e.g. my-file.txt';\n\n // Prepares a document, representing a text file in Cloud Storage\n const document = {\n gcsContentUri: `gs://${bucketName}/${fileName}`,\n type: 'PLAIN_TEXT',\n };\n\n // Classifies text in the document\n const [classification] = await client.classifyText({document});\n\n console.log('Categories:');\n clas\u003esification.categories.forEach(category = {\n console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);\n });\n\nPHP\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 $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)\n */\n function classify_text_from_file(string $uri): void\n {\n $languageServiceClient = new LanguageServiceClient();\n\n // Create a new Document, pass GCS URI and set type to PLAIN_TEXT\n $document = (new Document())\n -\u003esetGcsContentUri($uri)\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, $c\u003eategory-getName());\n printf('Confidence: %s' . PHP\u003e_EOL, $category-getConfidence());\n print(PHP_EOL);\n }\n }\n\nPython\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(gcs_content_uri):\n \"\"\"\n Classifying Content in text file stored in Cloud Storage\n\n Args:\n gcs_content_uri Google Cloud Storage URI where the file content is located.\n e.g. gs://[Your Bucket]/[Path to File]\n The text file must include at least 20 words.\n \"\"\"\n\n client = language_v1.LanguageServiceClient()\n\n # gcs_content_uri = 'gs://cloud-samples-data/language/classify-entertainment.txt'\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 = {\n \"gcs_content_uri\": gcs_content_uri,\n \"type_\": type_,\n \"language\": language,\n }\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(request={\"document\": document})\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\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=language)."]]