努力创建字典来存储来自 IBM Watson Entity Analysis 的结果
Struggling to create a dictionary to store results from IBM Watson Entity Analysis
我正在努力在字典中捕获 IBM Watson 实体分析的结果。我想通过一个函数提取每个 link 的情绪。我创建了一个函数来提取单个 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))
我看不到你在哪里调用 getentitylist
,但是在你的 url 循环中
entitydict = {}
for url in url_to_analyze:
entitydict.update(processurl(url))
update
将根据键值更新字典。 IE。这将覆盖字典中已有的任何键的值。因为您的回复将类似于:
{
"usage": {
"text_units": 1,
"text_characters": 2708,
"features": 1
},
"retrieved_url": "http://www.cnn.com/",
"language": "en",
"entities": [
{
"type": "Company",
"text": "CNN",
"sentiment": {
"score": 0.0,
"label": "neutral"
},
"relevance": 0.784947,
"disambiguation": {
"subtype": [
"Broadcast",
"AwardWinner",
"RadioNetwork",
"TVNetwork"
],
"name": "CNN",
"dbpedia_resource": "http://dbpedia.org/resource/CNN"
},
"count": 9
}
]
}
将要更新的密钥位于顶层,即。 usage
、retrieved_url
、retrieved_url
、entities
。因此 entitydict
将仅包含最后一个 url 的响应,因为这些键的先前值将被覆盖。
您应该做的是使用 url 作为每个响应的键。
entitydict = {}
for url in url_to_analyze:
entitydict.update({url : processurl(url)})
我正在努力在字典中捕获 IBM Watson 实体分析的结果。我想通过一个函数提取每个 link 的情绪。我创建了一个函数来提取单个 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))
我看不到你在哪里调用 getentitylist
,但是在你的 url 循环中
entitydict = {}
for url in url_to_analyze:
entitydict.update(processurl(url))
update
将根据键值更新字典。 IE。这将覆盖字典中已有的任何键的值。因为您的回复将类似于:
{
"usage": {
"text_units": 1,
"text_characters": 2708,
"features": 1
},
"retrieved_url": "http://www.cnn.com/",
"language": "en",
"entities": [
{
"type": "Company",
"text": "CNN",
"sentiment": {
"score": 0.0,
"label": "neutral"
},
"relevance": 0.784947,
"disambiguation": {
"subtype": [
"Broadcast",
"AwardWinner",
"RadioNetwork",
"TVNetwork"
],
"name": "CNN",
"dbpedia_resource": "http://dbpedia.org/resource/CNN"
},
"count": 9
}
]
}
将要更新的密钥位于顶层,即。 usage
、retrieved_url
、retrieved_url
、entities
。因此 entitydict
将仅包含最后一个 url 的响应,因为这些键的先前值将被覆盖。
您应该做的是使用 url 作为每个响应的键。
entitydict = {}
for url in url_to_analyze:
entitydict.update({url : processurl(url)})