LoginSignup
24
16

More than 5 years have passed since last update.

DialogflowのEntityを爆速で作成するライブラリを作った

Last updated at Posted at 2017-12-01

百聞は一見にしかず

こんな感じのCSVファイルを準備します。

sushi.csv
"鯖"
"鯵"
"鰈"
"鱸"
"鮑"
"鮪"
"鯨"
"鮭"

ライブラリを使ってアップロードすると・・・Entityが一瞬で出来上がります!

Screen Shot 0029-12-01 at 23.03.12.png

漢字から ローマ字ひらがなカタカナが作成されました。

ライブラリの紹介

このライブラリにflounderという名前を付けました。flounderは日本語のCSVファイルを読み込んで、自動でEntityの登録とシノニムを作ってくれるライブラリです。

インストール方法

flounderはpykakasiと呼ばれる日本語をローマ字などに変換するライブラリを使用しているため、pykakasiのインストールが必須です。※本当はflounderに取り込みたかったのですが、諸事情により今回は取り込めませんでした。

pykakasiのインストール

pip install git+https://github.com/miurahr/pykakasi.git

flounderのインストール

pip install flounder

これでインストールは完了です。

Developer access tokenを取得

dialogflowからDeveloper access tokenを取得します。

Screen Shot 0029-12-01 at 23.18.55.png

CSVからEntityを作成

サンプルコード

test_entity_upload.py
from flounder.flounder import Flounder

flounder = Flounder(DEVELOPER_ACCESS_TOKEN)
create_request = flounder.create_request('Sushi', 'sushi.csv')
response = create_request.getresponse()

print (response.read())
print (response.status, response.reason)

CSVファイル

sushi.csv
"鯖"
"鯵"
"鰈"
"鱸"
"鮑"
"鮪"
"鯨"
"鮭"

実行

$ python test_entity_upload.py

dialogflowのコンソールに新しくEntityが作成されていたら成功です:smiley:

ご理解とご協力のお願い

flounderは、設計、ネーミング、エラー処理など、見直すべき課題が多くございます。まだまだ発展途上ですが、その辺を踏まえて使用して頂けたら幸いです。本当はこの状態で公開するのを躊躇しましたが、まずは公開して少しずつリファクタリングをしていこうという方針に舵を切りました。

ソースはgithubにございます。お気づきの点がございましたら、issueかプルリクを頂けたら幸いです。

24
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
16