2017-08-04 43 views
0

因此,我在django框架中爲我的聊天bot製作了一個對話框。 「對話框」面板由意圖和實體下拉列表以及對話框textarea組成。下拉列表將取決於我的json格式的訓練數據。與json和python相關的下拉列表

我想要下拉列表,這樣如果我選擇intent,實體下拉列表會自動創建並顯示與選定的intent相關的所有實體。

我試過了,我能夠顯示意圖下拉菜單,但也有重複意圖(我使用python set函數刪除)。但我無法弄清楚如何顯示基於一個特定意圖的所有實體。

幫助我。這是我的例子JSON:

{"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.I在一定程度上改進了格式 –

回答

0

基本上,所有你需要做的是循環在裏面的物品common_examples並檢查intent在下拉列表中選擇的值相匹配。如果是這樣,請將entities添加到實體下拉列表中。

因爲您沒有提供有關您的HTML很多信息,我會嘗試用幾個假設來回答:

  1. 你已經一個select元素與ID intentDropdown表明意圖。
  2. 你有一個select元素的ID爲entitiesDropdown來顯示實體。
  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> 

最後,我想提請您注意一件事。考慮下面的例子:

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

entities列表中有一個項目。該項目有4個子項目。在您的問題中,如果您想要在一個下拉選項中顯示所有子項目(例如start: 8, end: 15, value: chinese, entity: cuisine),或者您需要爲每個子項目單獨選項,您都沒有說清楚。

我發佈的JS代碼會創建如下的下拉選項:
<option value="chinese">cuisine</option>

如果你想顯示其他項目,你可以創建另一個循環,並繼續添加項目下拉。

+0

非常感謝。 –