【问题标题】:Multiple API calls in python for Watson NLU用于 Watson NLU 的 python 中的多个 API 调用
【发布时间】:2021-05-18 08:58:18
【问题描述】:

我正在研究 Watson NLU,我需要对问卷数据进行分析。来自不同人的大约 300 个答案。 我可以在“...”格式的文本上运行它,但我很想获得一些帮助,了解如何一次运行所有 300 个。我当前的输入是在带有 ID 列的 excel 中。 感谢您对此进行调查。

nlu_api_key = "MY API KEY"
nlu_url = "https://api.eu-gb.natural-language-understanding.watson.cloud.ibm.com/instances/MY INSTANCE"


import json
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson.natural_language_understanding_v1 import Features, EntitiesOptions, KeywordsOptions, CategoriesOptions,SentimentOptions
import pandas as pd


gtm_Q6 = pd.read_excel(r'C:\Users\...\INPUT FILE.xlsx', sheet_name='OUPUT1')
print(gtm_Q6)


authenticator = IAMAuthenticator(nlu_api_key)
natural_language_understanding = NaturalLanguageUnderstandingV1(
    version='2020-08-01',
    authenticator=authenticator)

natural_language_understanding.set_service_url(nlu_url)

response = natural_language_understanding.analyze(
    text='Where is the firetruck with the flaming paint the tigers on top?',
    features=Features(
        entities=EntitiesOptions(emotion=True, sentiment=True, limit=5),
        keywords=KeywordsOptions(emotion=True, sentiment=True,limit=5),
        categories=CategoriesOptions(limit=3),
        sentiment=SentimentOptions(targets=['investments']) #sentiment=SentimentOptions(targets=['stocks'])
        )).get_result()

print(json.dumps(response, indent=2))
RESP_ID ANSWER
Q6_109.000000 team building
Q6_110.000000 enablement and coordination between tech and sevices
Q6_111.000000 skill building
Q6_113.000000 speed to the right resources
Q6_114.000000 information on the practicalities of the change at the moment

【问题讨论】:

  • 所以您的问题实际上是如何为 excel 文件的每一行做一些事情? for row in gtm_Q6['<your_id_field_name>']: ...
  • 有点像,对。有了我需要调用 API 的问题。 + 我需要以 API 可以使用的方式格式化我的 excel 行。
  • 在不知道 excel/csv 文件的内容的情况下真的很难提供帮助
  • 是的,有道理,我添加了一小段数据作为示例。
  • 所以在请求之间唯一会改变的是<MY INSTANCE>,它必须是RESP_ID

标签: python json csv ibm-watson nlu


【解决方案1】:

所以解决方案已经提供了 py T.J. Python中的克劳德: 这是他原始答案的链接:How to get rid of "\n" and " ' ' " in my json file

json_arr = []
for text in gtm_Q6_df['ANSWER']:
    response = natural_language_understanding.analyze(
        text=text,language = 'en',
            features=Features(
            entities=EntitiesOptions(sentiment=True,emotion=False,limit=3),
            categories=CategoriesOptions(limit=3),
            #sentiment=SentimentOptions(EntitiesOptions)
            )).get_result()
    
    #text = ("{ Answer: " + text + "}")
    json_arr.append(text)
    json_arr.append (response) # <==== change is here
    with open(r'C:\Users\...\Documents\Python NLP\WATSON NLU\OUTPUT JSON\data.json','w') as outline: #with open('data.json','w') as outline: ## data.json is the output file created ## the file can be renamed
        json.dump(json_arr,outline, indent = 2) # indent = 2 creates the pretty json!!
    
print("break")
print(json_arr)

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2017-08-17
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多