2017-08-04 16 views
0

私はdjangoフレームワークで私のチャットボット用のダイアログパネルを作っています。ダイアログパネルは、インテントとエンティティのドロップダウンリストとダイアログテキストエリアで構成されています。ドロップダウンリストは、json形式のトレーニングデータに依存します。依存ドロップダウンリストとjsonとpython

私がインテントを選択すると、エンティティのドロップダウンリストが自動的に作成され、選択したインテントに関連するすべてのエンティティを表示するようにドロップダウンリストが必要です。

私はしようとしていると私はドロップダウンを表示することができますが、それも重複した意図(私は、Pythonの関数を使用して削除されました。)しかし、私はどのように特定の1つの意図に基づいてすべてのエンティティを表示する方法を把握することはできません。

私を助けてください。

{"rasa_nlu_data": { 

"common_examples": [ 
    { 
    "text": "hey", 
    "intent": "greet", 
    "entities": [] 
    }, 
    { 
    "text": "yep", 
    "intent": "affirm", 
    "entities": [] 
    }, 
    { 
    "text": "i'm looking for a place to eat", 
    "intent": "restaurant_search", 
    "entities": [] 
    }, 
    { 
    "text": "i'm looking for a place in the north of town", 
    "intent": "restaurant_search", 
    "entities": [ 
     { 
     "start": 31, 
     "end": 36, 
     "value": "north", 
     "entity": "location" 
     } 
    ] 
    }, 
    { 
    "text": "show me chinese restaurants", 
    "intent": "restaurant_search", 
    "entities": [ 
     { 
     "start": 8, 
     "end": 15, 
     "value": "chinese", 
     "entity": "cuisine" 
     } 
    ] 
    }, 
    { 
    "text": "bye", 
    "intent": "goodbye", 
    "entities": [] 
    } 
]}} 
+0

あなたの質問は非常にひどくフォーマットされているので、私は下降しました。接続詞の欠如、文章間のスペースの欠如、段落の欠如、小文字の "i"であなた自身への言及は、少なくとも私のために、あなたの質問を読むことを非常に困難にします。投稿したサンプルコードが長すぎて冗長です。あなたの質問を解決してください - もしあなたが誰かがあなたの問題を手伝ってくれることを期待しているなら、あなたができることは少なくともあります。 – xyres

+0

Ok.私はある程度フォーマットを改善しました –

答えて

0

基本的に、あなたがしなければならないすべてはcommon_examples内の項目をループであり、intentドロップダウンで選択した値と一致するかどうかを確認:ここに私の例のJSONです。存在する場合は、エンティティのドロップダウンにentitiesを追加します。

あなたがHTMLについて多くの情報を提供していないので、私はいくつかの仮定にお答えしようとするでしょう:

  1. あなたはインテントを表示するID intentDropdownselect要素をしました。
  2. エンティティを表示するIDがentitiesDropdownselect要素があります。
  3. あなたはjQueryを使用しています。

コードには、その動作を説明するコメントが含まれています。

<!-- intents dropdown --> 
<select id="intentsDrowpdown"> 
    <!-- intent options--> 
</select> 

<!-- entities dropdown --> 
<select id="entitesDrowpdown"></select> 

<!-- Javascript --> 
<script> 
var data = {"rasa_nlu_data": { ... }}; // the json data 

var totalExamples = data.rasa_nlu_data.common_examples.length; // total items inside common_examples 

// listen to the event when selected value in 
// the intent dropdown changes 
$("#intentsDropdown").on('change', function() { 

    $("#entitiesDropdown").empty(); // clear the previously added entities from entities drowpdown 

    var selectedIntent = this.value; // currently selected intent 

    // loop over the items in common_examples 
    for (var i = 0; i < totalExamples; i++) { 

     var currentExample = data.rasa_nlu_data.common_examples[i] // current example in the loop 

     // see if the selected intent matches the 
     // intent of the current example in the loop 
     if (currentExample.intent == selectedIntent) { 

      // if intent matches 
      // loop over the items inside entities 
      // of the current example 
      for (var j = 0; j < currentExample.entities.length; j++) { 
       // add the option in the dropdown 
       $("#entitiesDropdown").append($('<option>', { 
        value: currentExample.entities[j].value, 
        text: currentExample.entities[j].entity 
       })); 
      } 

     } 
    } 
}); 

</script> 

最後に、1つのことを通知したいと思います。以下の例を参考にしてください:

"entities": [ 
    { 
    "start": 8, 
    "end": 15, 
    "value": "chinese", 
    "entity": "cuisine" 
    } 

entitiesリストには1つの項目があります。そのアイテムには4つのサブアイテムがあります。あなたの質問では、すべてのサブアイテムを1つのドロップダウンオプション(たとえばstart: 8, end: 15, value: chinese, entity: cuisine)に表示するか、各サブアイテムごとに別々のオプションを表示するかどうかを明確にしていません。

投稿したJSコードは、次のようなドロップダウンオプションを作成します:
<option value="chinese">cuisine</option>

他のアイテムを表示する場合は、別のループを作成してアイテムをドロップダウンに追加したままでください。

+0

ありがとうございました。 –

関連する問題