App de análisis de opiniones
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Una aplicación básica de la API de Natural Language que realiza análisis de opiniones en textos.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos 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)."]]