【问题标题】:Export/convert Dialogflow agent to csv or excel file using python使用 python 将 Dialogflow 代理导出/转换为 csv 或 excel 文件
【发布时间】:2020-09-17 15:56:25
【问题描述】:

如何将所有问题和答案导出到 csv 或 excel 文件?

我已将对话流代理导出到 zip 文件中,并且我为每个问题或意图获得了两个 json 文件。

有没有办法在 csv 或 excel 文件中创建问答对?

【问题讨论】:

  • “问答对”是什么意思?就像,意图是问题,响应是答案?训练短语怎么样,您想将它们作为相同的问题包含吗?
  • 你中了靶心????....是的,我也想包括训练短语。

标签: python json python-3.x csv dialogflow-es


【解决方案1】:

zip 文件包含两个目录意图和实体。意图目录包含 Dialogflow 每个意图的响应和训练短语。您可以观察 JSON 文件中的模式并编写脚本来制作 csv 文件。

import os
import csv
import json

all_intents = os.listdir('intents')


with open('agent.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["Response", "Questions"])
    for intent in all_intents:
        write = []
        if intent.find('_usersays_en.json') == -1:
            try:
                with open('intents/' + intent) as f:
                    data = json.load(f)
                    resp = ''
                    try:
                        resp = data['responses'][0]['messages'][0]['speech'][0]
                    except:
                        print(intent)
                    write.append(resp)
            except:
                print(intent)
            try:
                with open('intents/' + intent.replace(".json", "") + '_usersays_en.json') as f:
                    data = json.load(f)
                    for d in data:
                        qn = (d['data'][0]['text'])
                        write.append(qn)
            except:
                print(intent.replace(".json", "") + '_usersays_en.json')
            writer.writerow(write)

代码运行说明:

  1. 将代理导出为 zip。
  2. 解压缩文件。您将看到从 zip 中提取的实体和意图目录。
  3. 将此 python 文件和意图目录放在同一目录中。
  4. 运行python3 filename.py(包含代码的文件名)。
  5. agent.csv 将被创建。
  6. 所有没有响应或训练短语的意图都将显示在终端上。

【讨论】:

    猜你喜欢
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多