簡體   English   中英

Watson NLU 的 python 中的多個 API 調用

[英]Multiple API calls in python for Watson NLU

我正在研究 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 回答
Q6_109.000000 團隊建設
Q6_110.000000 技術和服務之間的支持和協調
Q6_111.000000 技能建設
Q6_113.000000 快速找到正確的資源
Q6_114.000000 有關當前更改的實用性的信息

因此,解決方案已在 Python 中提供了 py TJ Crowder:這是他原始答案的鏈接: 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM