无法使用 google 云自然语言 API 打印情绪
Unable to print sentiment using google cloud natural language API
下面是我试过的代码,但它运行时出现如下所示的错误
from google.cloud import bigquery
from google.cloud.language import enums
from google.cloud.language import types
import sys
import six
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
client = bigquery.Client.from_service_account_json('simple_project//MQ News Matching-2443b471b5ff.json')
with open('articles.txt',mode='r', encoding="utf8") as reader:
text=reader.read()
if isinstance(text,six.binary_type):
text = text.decode('utf-8')
document = types.Document(content=text.encode('utf-8'),type=enums.Document.Type.PLAIN_TEXT)
# Detect and send native Python encoding to receive correct word offsets.
encoding = enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = enums.EncodingType.UTF16
result = client.analyze_entity_sentiment(document, encoding)
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(SentimentIntensityAnalyzer.score, sentiment.magnitude))
我得到的错误如下
print('Sentiment: {}, {}'.format(SentimentIntensityAnalyzer.score,
sentiment.magnitude)) AttributeError: type object
'SentimentIntensityAnalyzer' has no attribute 'score'
你要做的是:
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
下面是我试过的代码,但它运行时出现如下所示的错误
from google.cloud import bigquery
from google.cloud.language import enums
from google.cloud.language import types
import sys
import six
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
client = bigquery.Client.from_service_account_json('simple_project//MQ News Matching-2443b471b5ff.json')
with open('articles.txt',mode='r', encoding="utf8") as reader:
text=reader.read()
if isinstance(text,six.binary_type):
text = text.decode('utf-8')
document = types.Document(content=text.encode('utf-8'),type=enums.Document.Type.PLAIN_TEXT)
# Detect and send native Python encoding to receive correct word offsets.
encoding = enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = enums.EncodingType.UTF16
result = client.analyze_entity_sentiment(document, encoding)
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(SentimentIntensityAnalyzer.score, sentiment.magnitude))
我得到的错误如下
print('Sentiment: {}, {}'.format(SentimentIntensityAnalyzer.score, sentiment.magnitude)) AttributeError: type object 'SentimentIntensityAnalyzer' has no attribute 'score'
你要做的是:
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