2017-12-24 11 views
0

私は心理的な質問をして人の感情を分析するチャットボットを作成しようとしています。私はBluemixでチャットボットを作って、このチュートリアルの助けを借りて赤にノードを実装しました:https://github.com/watson-developer-cloud/node-red-labs/tree/master/basic_examples/conversation 私の流れは非常に似ていますが、私はトーンアナライザを追加して、会話。 私の主な問題は、htmlテンプレートのトーンアナライザの値にドット表記でアクセスできないようですが、応答デバッグは入力が確実に分析されていることを示しています。 テンプレートがトーンアナライザの値にアクセスできるように、フローまたはAJAXコードをどのように適合させることができますか? (コメントアウトされているコードは、私の問題を整理しようとした私の試みですが、特にAJAXの手がかりではありませんので少し面倒です)。でノードの赤を使用してチャットボットをhttpに実装するには、AJAXの青色トーンアナライザを使用して入力のトーンを分析するにはどうすればいいですか?

Flow image

<html> <head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title>  My BOT </title> <link rel="stylesheet" 
    type="text/css" 
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> </head> <body> 

<div class="container"> 
    <div id="no-script"class="bg-info"> 
    This application needs JavaScript enabled in your browser! 
    </div> 
    <div id="id_contextdump"></div> 

    <h1>My BOT</h1> 
    <div id=id_botchathistory>  </div> 

    <div> 
     <form> 
     <label for="id_chattext">Your Input: </label> 
     <input type="text" name="chattext" id="id_chattext"> 
     <br/><br/> 
     </form> 
     <button onclick="javascript:onChatClick()">Send</button> </div> 
</div> 
<div id="id_tone"> 
<table> 
    <thead> 
     <tr> 
      <th>Tone</th> 
      <th>Score</th> 
     </tr> 
     {{#response.document_tone.tone_categories.0.tones}} 
     <tr> 
      <td>{{tone_id}}</td> 
      <td>{{score}}</td> 
     </tr> 
     {{/response.document_tone.tone_categories.0.tones}} 
    </thead> 
</table> 
</div> 
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

<script type="text/javascript"> 

    $(document).ready(function() { 
     javascriptCheck(); 
     $('#id_contextdump').hide(); 
    }); 

    // if javascript is enabled on the browser then can 
    // remove the warning message 
    function javascriptCheck() { 
    $('#no-script').remove(); 
    } 

    function createNewDiv(who, message) { 
    var txt = who + ' : ' + message; 
    return $('<div></div>').text(txt); 
    } 

    function chat(person, txt) { 
    $('#id_botchathistory').append(createNewDiv(person, txt)); 
    }  
    //  function updateTone(){ //   $('#id_tone').append(createNewDiv(response.document_tone.tone_categories.0.tones)) //  } 

    function processOK(response) { 
    console.log(response); 
    console.log(response.botresponse.messageout); 
    console.log(response.botresponse.messageout.output.text); 
    console.log(response.botresponse.messageout.context); 
    chat('Bot', response.botresponse.messageout.output.text); 
    $('#id_contextdump').data('convContext', response.botresponse.messageout.context); 
    } 

    function processNotOK() { 
    chat('Error', 'Error whilst attempting to talk to Bot'); 
    } 

    function invokeAjax(message) { 
    var contextdata = $('#id_contextdump').data('convContext'); 
    console.log('checking stashed context data'); 
    console.log(contextdata); 


    //var ajaxData = "msgdata=" + message; 
    var ajaxData = {}; 
    ajaxData.msgdata = message; 
    if (contextdata) { 
     ajaxData.context = contextdata;  
    } //  var tone = { //   document: {}, //   sentence: {} //  }; 
    //  tone.document = getTones(data.document_tone); 

    $.ajax({ 
     type: 'POST', 
     url: 'botchat', 
     data: ajaxData, 
     success: processOK, 
     error: processNotOK 
    }); 
    } 

    // User has entered some text. 
    function onChatClick() { 
    var txt = $('#id_chattext').val(); 
    chat('You', txt); 
    invokeAjax(txt); //  updateTone(); 
    } 
    //  var tone_analyser = watson.tone_analyzer({ //  username: 'e7cc7d78-d63f-42a9-ac1b-c900bbee33a4', //  password: '57NyBACkO5s3', //  version: 'v3', //  version_date: '2016-05-19' // }); 
    //  window.setInterval("refreshDiv()", 500); //  function refreshDiv(){ //   tone_analyser= tone_analyser; // } 

</script> </body> </html> 

_// stash away incoming data_ 
msg.mydata = {}; 
msg.mydata.messagein = msg.req.body.msgdata; 
msg.payload = msg.mydata.messagein; 

msg.params = { "context": msg.req.body.context}; 

return msg; 

アウト:

msg.mydata.messageout = msg.payload; 

msg.payload = {}; 
msg.payload.botresponse = msg.mydata; 

return msg; 

答えて

0

あなたはin機能ノードの後に​​流​​れをフォークしているため、問題になります。 outファンクションノードが2つの入力メッセージを受信した後にのみ出力されない限り、これは機能しません。

フローをフォークすると、フローは順番に実行されますが、並列ではありません。これは、NodeJSが完全に単一スレッドであるためです。つまり、Art Attackノードの結果のみがhttp-outノードに戻され、ページに戻ります。

outファンクションノードの内容を正確に把握することなく、これを修正する方法を言うことはできません。

+0

ああ!それは理にかなっている。その場合、私はあなたにコードを出し入れします! – Domsky

+0

'join'ノードのオプションを見て、' out'関数ノードの前に挿入して、 'out'関数を編集して配列を操作してください。 – hardillb

+0

私はまだNode Redをかなり新しくしています。したがって、結合ノードを接続して配列を作成するように指示した後、out関数でその配列にアクセスするにはどうしたらいいですか?その新しい配列は何ですか? – Domsky

関連する問題