То, что вы хотите сделать, это:
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
def analyze(movie_review_filename):
"""Run a sentiment analysis request on text within a passed filename."""
client = language.LanguageServiceClient()
with open(movie_review_filename, 'r') as review_file:
# Instantiates a plain text document.
content = review_file.read()
document = types.Document(
content=content,
type=enums.Document.Type.PLAIN_TEXT)
annotations = client.analyze_sentiment(document=document)
# Print the results
print_result(annotations)
def print_result(annotations):
score = annotations.document_sentiment.score
magnitude = annotations.document_sentiment.magnitude
for index, sentence in enumerate(annotations.sentences):
sentence_sentiment = sentence.sentiment.score
print('Sentence {} has a sentiment score of {}'.format(
index, sentence_sentiment))
print('Overall Sentiment: score of {} with magnitude of {}'.format(
score, magnitude))
Вам может потребоваться установить GOOGLE_APPLICATION_CREDENTIALS, если вы можете сделать это так:
export GOOGLE_APPLICATION_CREDENTIALS='/path/to/your/client_secret.json'
Это было бы то же самоеФайл JSON, который вы использовали в своем вопросе.
Другие образцы Cloud Natural Language можно найти здесь: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/language