Classificazione dei contenuti di un file Cloud Storage
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Analizza un file archiviato in Google Cloud Storage e restituisce un elenco di categorie di contenuti applicabili al testo trovato nel documento.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]