我正在努力从字典中捕获 IBM Watson 实体分析的结果。我想通过一个函数提取每个链接的情绪。我创建了一个函数来提取单个 url。但是试图存储结果的字典仅捕获最后一个 url 结果。我是 Python 新手,感谢任何帮助。
这是我的实体分析代码,
# function to process an URL
def processurl(url_to_analyze):
# end point
endpoint = f"{URL}/v1/analyze"
# credentials
username = "apikey"
password = API_KEY
# parameters
parameters = {
"version": "2020-08-01"
}
# headers
headers = {
"Content-Type":"application/json"
}
# watson options
watson_options = {
"url": url_to_analyze,
"features": {
"entities": {
"sentiment": True,
"emotion": True,
"limit":10
}
}
}
# return
response = requests.post(endpoint,
data=json.dumps(watson_options),
headers=headers,
params=parameters,
auth=(username,password)
)
return response.json()
这是我创建的用于从上面传递结果的函数
# create a function to extract the entities from the result data
def getentitylist(data,threshold):
result = []
for entity in data["entities"]:
relevance = float(entity["relevance"])
if relevance > threshold:
result.append(entity["text"])
return result
遍历 URL 后,我似乎无法将结果存储在字典中,以便我可以将其传递给我的函数以获取实体结果
# method II: loop through news api urls and perform entity analysis and store it in a dictionary
entitydict = {}
for url in url_to_analyze:
entitydict.update(processurl(url))