簡體   English   中英

使用 python 將 Dialogflow 代理導出/轉換為 csv 或 excel 文件

[英]Export/convert Dialogflow agent to csv or excel file using python

如何將所有問題和答案導出到 csv 或 excel 文件?

我已將 dialogflow 代理導出到 zip 文件中,並且為每個問題或意圖獲得了兩個 json 文件。

有沒有辦法在 csv 或 excel 文件中創建問答對?

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 文件和 Intents 目錄放在同一目錄中。
  4. 運行 python3 filename.py(包含代碼的文件名)。
  5. agent.csv 將被創建。
  6. 所有沒有響應或訓練短語的意圖都將顯示在終端上。

暫無
暫無

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

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