是否可以以批处理模式运行大量文档的Google Cloud Platform NLP-API实体情感分析?

问题描述 投票:1回答:1

我对Google Cloud Platform相对陌生。我有一个庞大的数据集(1800万篇文章)。我需要使用GCP的NLP-API进行实体情感分析。我不确定我进行分析的方式是否是获得所有文章的实体情感所需的时间的最佳方法。我想知道是否有一种方法可以对所有这些文章进行批处理,而不是遍历每个文章并进行API调用。这是我一直在使用的过程的摘要。

  1. 我有大约500个文件,每个文件包含大约30,000个文章。
  2. 使用本地服务器上的python脚本,遍历每个文件,并为每篇文章,调用给定的函数here
  3. 我将每篇文章的全部输出存储在protobuf中。

此步骤后,我不需要Google API并对存储在protobuf中的API输出执行最终分析。

[这对于一个研究项目来说已经足够好了,我有150万篇文章,花了几天时间。现在我有1800万篇文章,我想知道是否有更好的方法可以解决此问题。我读过的有关批处理的文章旨在解决应用程序或图像处理任务。有类似我想要的内容here,但我不确定是否可以使用NLP-API进行此操作。

这是我的代码的片段,DF是我有文章的Pandas数据框。

def entity_sentiment_text(text):
    """Detects entity sentiment in the provided text."""
    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)
    return result


for i,id_val in enumerate(article_ids):
    loop_start = time.time()
    if i%100 == 0:
        print i
    # create dynamic name, like "D:\Current Download\Attachment82673"
        dynamic_folder_name = os.path.join(folder, str(i))
    # create 'dynamic' dir, if it does not exist
        if not os.path.exists(dynamic_folder_name):
            os.makedirs(dynamic_folder_name)
    file_name = str(id_val) + ".txt"
    text = list(DF.loc[id_val])[1]
    try:
        text = unicode(text, errors='ignore')
        result = entity_sentiment_text(text)
        # print result
        with open(dynamic_folder_name + "/" + str(id_val) + ".bin", 'w') as result_file:
            result_file.write(result.SerializeToString())
            result_file.close()     
    except Exception as e: 
        print(e)
        with open("../"article_id_error.log", "a") as error_file:
            error_file.write(json.dumps(str(id_val) + "\n"))
        log_exception(e,id_val)

请注意,这是一项一次性的研究分析,我不是在构建应用程序。我也知道我无法减少对API的调用次数。总而言之,如果我要进行1800万次调用,那么进行所有这些调用而不是逐篇阅读并分别调用函数的最快方法是什么?

我感觉我应该进行某种并行处理,但是对于花更多的时间学习Dataproc而不知道是否对我的问题有所帮助,我有些谨慎。

python google-cloud-platform google-cloud-nl
1个回答
0
投票

您将需要管理合并文档以获得较小的总作业数。您还需要对每分钟的请求数和每天的总请求数进行速率限制。

[定价基于以1,000个字符为单位的字符。如果您打算处理1800万篇文章(每篇文章要多少个单词?),我将与Google Sales联系以讨论您的项目并安排信贷审批。您将很快达到配额限制,然后您的作业将返回API错误。

我将从阅读本节内容开始:

https://cloud.google.com/natural-language/docs/resources

© www.soinside.com 2019 - 2024. All rights reserved.