Aplikasi analisis sentimen
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Aplikasi Natural Language API dasar yang melakukan analisis sentimen pada teks.
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,["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)."]]