Analizar opiniones sobre aplicaciones
Organízate con las colecciones
Guarda y clasifica el contenido según tus preferencias.
Una aplicación básica de la API Natural Language que realiza análisis de sentimiento en texto.
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,["A basic Natural Language API application that performs sentiment analysis on text.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Sentiment Analysis Tutorial](/natural-language/docs/sentiment-tutorial)\n\nCode sample \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 \"\"\"Demonstrates how to make a simple call to the Natural Language API.\"\"\"\n\n import argparse\n\n from google.cloud import language_v1\n\n\n\n def print_result(annotations):\n score = annotations.document_sentiment.score\n magnitude = annotations.document_sentiment.magnitude\n\n for index, sentence in enumerate(annotations.sentences):\n sentence_sentiment = sentence.sentiment.score\n print(f\"Sentence {index} has a sentiment score of {sentence_sentiment}\")\n\n print(f\"Overall Sentiment: score of {score} with magnitude of {magnitude}\")\n return 0\n\n\n\n\n def analyze(movie_review_filename):\n \"\"\"Run a sentiment analysis request on text within a passed filename.\"\"\"\n client = language_v1.LanguageServiceClient()\n\n with open(movie_review_filename) as review_file:\n # Instantiates a plain text document.\n content = review_file.read()\n\n document = language_v1.Document(\n content=content, type_=language_v1.Document.Type.PLAIN_TEXT\n )\n annotations = 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_analyze_sentiment(request={\"document\": document})\n\n # Print the results\n print_result(annotations)\n\n\n\n\n if __name__ == \"__main__\":\n parser = argparse.ArgumentParser(\n description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter\n )\n parser.add_argument(\n \"movie_review_filename\",\n help=\"The filename of the movie review you'd like to analyze.\",\n )\n args = parser.parse_args()\n\n analyze(args.movie_review_filename)\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)."]]