IBMWatsonAssistant:如何用XML语法显示/打印字典(python3)



我想在IBMwatson中显示python字典的一些上下文作为输出。例如,如果我有一本像这样的字典

{'author': 'Bloomberg News',
'title': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
'description': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
'url': 'https://www.bloombergquint.com/business/n-y-deaths-at-5-month-low-india-crisis-deepens-virus-update',
'source': 'Bloomberg | Latest And Live Business',
'image': None,
'category': 'business',
'language': 'en',
'country': 'us',
'published_at': '2021-05-02T04:48:26+00:00'}

然后要在IBM Watson Assistant中打印标题作为输出,我们需要这样写:

<?data['title']?>

这将产生以下产出:印度危机;纽约州死亡人数创5个月新低:病毒更新

现在,如果我有一本这样的字典:

[{'author': 'BQ Desk',
'title': 'Coronavirus Updates: India Reports 3.9 Lakh New Covid Cases, 3,689 Deaths',
'description': 'Coronavirus Updates: India Reports 3.9 Lakh New Covid Cases, 3,689 Deaths',
'url': 'https://www.bloombergquint.com/coronavirus-outbreak/coronavirus-updates-india-reports-39-lakh-new-covid-cases-3689-deaths',
'source': 'Bloomberg | Latest And Live Business',
'image': None,
'category': 'business',
'language': 'en',
'country': 'us',
'published_at': '2021-05-02T05:15:34+00:00'},
{'author': None,
'title': 'Leasebak is geen heilige koe meer',
'description': 'Managers met een leasebak in je arbeidsvoorwaarden, de vanzelfsprekendheid van de auto van de zaak gaat er vanaf. Als je nu solliciteert, zijn er steeds minder bedrijven die daar zomaar in willen meegaan. Door corona en thuiswerken nemen bedrijven hun mobiliteit nog eens extra onder de loep, zien leaseorganisaties en werkgeversorganisatie AWVN.',
'url': 'https://www.telegraaf.nl/financieel/1632068442/leasebak-is-geen-heilige-koe-meer',
'source': 'De Telegraaf - Financieel Overzicht',
'image': 'https://www.telegraaf.nl/images/1200x630/filters:format(jpeg):quality(80)/cdn-kiosk-api.telegraaf.nl/a64d720a-a9c6-11eb-83a5-02d1dbdc35d1.jpg',
'category': 'business',
'language': 'nl',
'country': 'nl',
'published_at': '2021-05-02T05:00:00+00:00'},
{'author': 'Bloomberg News',
'title': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
'description': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
'url': 'https://www.bloombergquint.com/business/n-y-deaths-at-5-month-low-india-crisis-deepens-virus-update',
'source': 'Bloomberg | Latest And Live Business',
'image': None,
'category': 'business',
'language': 'en',
'country': 'us',
'published_at': '2021-05-02T04:48:26+00:00'},
{'author': 'FE Online',
'title': 'Coronavirus in India Live News: PM Modi to meet experts today to review oxygen & medicine availability; India reports 3.92 lakh Covid-19 cases',
'description': 'Coronavirus Daily Cases and Deaths in India, Covid-19 India Lockdown, May 1 Phase 3 vaccine drive, Day 2 vaccination drive for 18+ years, CoWIN Covid-19 Vaccination Registration, May 2: Prime Minister Narendra Modi is expected to review the human resource situation, in relation to the Covid-19 pandemic, and ways to augment it.',
'url': 'https://www.financialexpress.com/lifestyle/health/coronavirus-lockdown-in-india-coronavirus-cases-in-india-today-live-covid-19-cowin-vaccine-registration-vaccination-drive-rollout-coronavirus-daily-cases-and-deaths-in-india-covid-19-lockdown-in-del-2/2244047/',
'source': 'The Financial Express',
'image': 'https://images.financialexpress.com/2021/05/covid-second-wave.jpg',
'category': 'business',
'language': 'en',
'country': 'us',
'published_at': '2021-05-02T04:28:25+00:00'},
{'author': 'Uppsala University',
'title': 'Newly Discovered Immune Cell Function Vital to Healing May Lead to Treatments for Cancer and Cardiovascular Diseases',
'description': 'Cardiovascular disease, the most common cause of death, is the result of oxygen deprivation as blood perfusion to affected tissue is prevented. To halt the...The post Newly Discovered Immune Cell Function Vital to Healing May Lead to Treatments for Cancer and Cardiovascular Diseases appeared first on SciTechDaily.',
'url': 'https://scitechdaily.com/newly-discovered-immune-cell-function-vital-to-healing-may-lead-to-treatments-for-cancer-and-cardiovascular-diseases/',
'source': 'Scitechdaily | Science Space And Technology News 2017',
'image': None,
'category': 'science',
'language': 'en',
'country': 'us',
'published_at': '2021-05-01T22:40:45+00:00'}]

然后如何用XML打印每篇文章的标题名称(正如我上面提到的语法(。

Desired Output:
Coronavirus Updates: India Reports 3.9 Lakh New Covid Cases, 3,689 Deaths
Leasebak is geen heilige koe meer
India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update
Coronavirus in India Live News: PM Modi to meet experts today to review oxygen & medicine availability; India reports 3.92 lakh Covid-19 cases
Newly Discovered Immune Cell Function Vital to Healing May Lead to Treatments for Cancer and Cardiovascular Diseases

查看您的需求,您可能希望查看可应用于数据的方法列表,请参阅Watson Assistant文档中的表达式语言方法。使用这些方法,您可能能够将数组操作为所需的输出

或者,您可以调用一个外部RESTapi来处理数据并返回所需的输出。这个外部api可以是一个IBMCloud函数

最后也是最简单的一点是,您可以拥有一个对话框节点,该节点具有与答案数组(python中的列表/目录(大小相对应的多个响应条件。然而,这种方法通常只适用于小的答案组,并且数组/列表/目录的最大大小是已知的。

最新更新