本文介绍了from google.cloud import language ImportError:没有名为cloud的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Google情感分析。以下是我从Google教程获得的代码。



演示如何简单调用Natural Language API。

  import argparse 

from google.cloud汇入语言


def print_result(注释):
score = annotations.sentiment.score
magnitude = annotations.sentiment.magnitude

for enumerate(annotations.sentences)中的句子:
sentence_sentiment格式(
index,sentence_sentiment))

print('Overall Sentiment:'sentence.sentiment.score
print('Sentence {} has a bustment score of {}'。格式(
分数,大小))
返回0

print('情感:{}的分数,大小为{} '.format(
score,magnitude))
return 0


def analyze(movie_review_filename):
运行情绪分析请求文字与。
language_client = language.Client()

with open(movie_review_filename,'r')as review_file:
#实例化一个纯文本文档。
document = language_client.document_from_html(review_file.read())

#检测文档中的情绪。
annotations = document.annotate_text(include_sentiment = True,
include_syntax = False,
include_entities = False)

#打印结果
print_result(annotations)


if __name__ =='__main__':
parser = argparse.ArgumentParser(
description = __ doc__,
formatter_class = argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'movie_review_filename',
help ='您想分析的电影评论的文件名。')
args = parser.parse_args()

analyze(args.movi​​e_review_filename)

运行代码我收到以下错误:



from google.cloud import language
ImportError:没有模块名为cloud

解决方案

你需要安装实际的Googl e SDK Python模块。 google-cloud库是pip可安装的:

  pip install --upgrade google-cloud 

查看更多。


Trying to use Google Sentiment Analysis. Here is the code I got from the Google tutorial.

"""Demonstrates how to make a simple call to the Natural Language API."""

import argparse

from google.cloud import language


def print_result(annotations):
    score = annotations.sentiment.score
    magnitude = annotations.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))
    return 0

    print('Sentiment: score of {} with magnitude of {}'.format(
        score, magnitude))
    return 0


def analyze(movie_review_filename):
    """Run a sentiment analysis request on text within a passed filename."""
    language_client = language.Client()

    with open(movie_review_filename, 'r') as review_file:
        # Instantiates a plain text document.
        document = language_client.document_from_html(review_file.read())

        # Detects sentiment in the document.
        annotations = document.annotate_text(include_sentiment=True,
                                             include_syntax=False,
                                             include_entities=False)

        # Print the results
        print_result(annotations)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        'movie_review_filename',
        help='The filename of the movie review you\'d like to analyze.')
    args = parser.parse_args()

    analyze(args.movie_review_filename)

Running the code I receive the following error:

from google.cloud import languageImportError: No module named cloud

解决方案

You need to install the actual Google SDK Python module. The google-cloud library is pip install-able:

pip install --upgrade google-cloud

See more here.

这篇关于from google.cloud import language ImportError:没有名为cloud的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:35