检测聊天机器人响应来自哪个 json 标签

Detect from which json tag the chatbot responds comes from

我正在开发一个使用 JSON 作为其数据集的聊天机器人。 JSON 文件包含代表不同主题的不同标签,f.ks 'greeting'、'goodbye' 等。我的计划是有一个进度条,根据哪个进度条移动一定百分比标记聊天机器人响应的来源。我正处于聊天机器人响应必须映射回 JSON 以进行组合的最后阶段。由于数据已经来自那里,我一直在思考如何以最简单的方式做到这一点?

<script>
   const url = "http://127.0.0.1:5000/json";
   //const botRep = 
   //const test1 = document.getElemenById('textInput').textContent;
   async function getData(){
   
   const response = await fetch(url);
   const data = await response.json();
   let tag1 = data.intents[0].tag; //greeting tag from json
   console.log(tag1);

   }
    getData();
  
   function botResponse(rawText) {

      // Bot Response
      $.get("/get", { msg: rawText }).done(function (data) {
        console.log(rawText);. 
        console.log(data);
        const msgText = data;
        $.get("/save", { msg: msgText, sender:BOT_NAME})
        appendMessage(BOT_NAME, BOT_IMG, "left", msgText);

      }); 

    } 


  </script>
/*   progress bar div  */

.progress {
  /* display: flex; */
  font-size: medium;
  justify-content: space-between;
  padding: 5px;
  text-align: center;
  /*border-bottom: var(--border);*/
  background: #eee;
  color: #666;
  text-align: left;
}

#id1{
 position: right;   
 right: 50%;
 top: 10px;
 float:  right;
}

#progress30 {
 width: 500px;   
 border: 1px solid black;
 position: relative;
 padding: 3px;
 top: 16px; 
}

#percent {
 position: absolute;   
 left: 50%;
 bottom: 25px;
}

#bar {
 height: 20px;
 background-color: green;
 width: 0%;
}
<body>
  <!-- partial:index.partial.html -->
  <section class="msger">
    <header class="msger-header">
      <div class="msger-header-title">
        <i class="fas fa-bug"></i>Chatbot <i class="fas fa-bug"></i><br>
        
    <div class="progress">
            Trust Bar
    <div id="progress30">
           <span id="percent">0%</span>
           <div id="bar"></div>
    </div>
    </div>

您可以使用 map 创建一个字典来存储与每个标签关联的百分比。

let myMap = new Map([
  ['greeting', 25],
  ['answer', 50],
  ['goodbye', 100],
])

从 json 获取标签后,在地图中查找百分比值并更新 ID 为 percent

的范围内的值