Advertisement
Guest User

Untitled

a guest
Oct 28th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | None | 0 0
  1. using UnityEngine;
  2. using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
  3. using IBM.Watson.DeveloperCloud.Utilities;
  4. using IBM.Watson.DeveloperCloud.Logging;
  5. using System.Collections;
  6. using FullSerializer;
  7. using System.Collections.Generic;
  8. using IBM.Watson.DeveloperCloud.Connection;
  9. using UnityEngine.UI;
  10. using System.Timers;
  11.  
  12. public class ConversationDialog : MonoBehaviour
  13. {
  14.  
  15.     // Configurable Text
  16.     public Text Response;
  17.  
  18.     private TextToSpeechService textToSpeechService;
  19.  
  20.     // Private variables for internal use only
  21.     private string _username = "fb5185e8-f374-48aa-82dc-170bc48e6e44";
  22.     private string _password = "actAdS7O2vMk";
  23.     private string _url = "https://gateway.watsonplatform.net/assistant/api";
  24.     private string _workspaceId = "31495ba4-7faa-477c-886a-2f070d079c65";
  25.  
  26.     private Conversation _conversation;
  27.     private string _conversationVersionDate = "2017-05-26";
  28.  
  29.     private fsSerializer _serializer = new fsSerializer();
  30.     private Dictionary<string, object> _context = null;
  31.  
  32.     // Agregue esto
  33.     public string responsetext;
  34.  
  35.     void Start()
  36.     {
  37.         LogSystem.InstallDefaultReactors();
  38.  
  39.         //  Create credential and instantiate service
  40.         Credentials credentials = new Credentials(_username, _password, _url);
  41.  
  42.         _conversation = new Conversation(credentials);
  43.         _conversation.VersionDate = _conversationVersionDate;
  44.  
  45.         textToSpeechService = TextToSpeechService.FindObjectOfType<TextToSpeechService>();
  46.     }
  47.  
  48.     public void SendMessageRequest(string message)
  49.     {
  50.         MessageRequest messageRequest = new MessageRequest()
  51.         {
  52.             input = new Dictionary<string, object>()
  53.             {
  54.                 {"text", message}
  55.             },
  56.             context = _context
  57.         };
  58.  
  59.         if(!_conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest))
  60.         {
  61.             Log.Debug("SendMessageRequest()", "Error al enviar el mensaje");
  62.         }
  63.  
  64.     }
  65.  
  66.     private void OnMessage(object resp, Dictionary<string, object> customData)
  67.     {
  68.         Log.Debug("ExampleConversation.OnMessage()", "Conversation: Message Response: {0}", customData["json"].ToString());
  69.  
  70.         //  Convert resp to fsdata
  71.         fsData fsdata = null;
  72.         fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata);
  73.         if (!r.Succeeded)
  74.             throw new WatsonException(r.FormattedMessages);
  75.  
  76.         //  Convert fsdata to MessageResponse
  77.         MessageResponse messageResponse = new MessageResponse();
  78.         object obj = messageResponse;
  79.         r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
  80.         if (!r.Succeeded)
  81.             throw new WatsonException(r.FormattedMessages);
  82.  
  83.         //  Set context for next round of messaging
  84.         object _tempContext = null;
  85.         (resp as Dictionary<string, object>).TryGetValue("context", out _tempContext);
  86.  
  87.         if (_tempContext != null)
  88.             _context = _tempContext as Dictionary<string, object>;
  89.         else
  90.             Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
  91.  
  92.         if(resp != null && messageResponse.intents.Length > 0)
  93.         {
  94.             // Agregue esto
  95.             // Se comenta porque debo modificar el response para colocar los subtitulos
  96.             //Response.text = string.Join(", ", messageResponse.output.text);
  97.  
  98.  
  99.  
  100.             responsetext += string.Join(", ", messageResponse.output.text);
  101.             string send = responsetext;
  102.             StartCoroutine (SubtitlesManager (send));
  103.             // Agregue esto
  104.             textToSpeechService.UpdateTestString(send);
  105.             HandleIntents(messageResponse.intents[0]);
  106.         }
  107.     }
  108.  
  109.     public GameObject textBox;
  110.  
  111.     // Agregue esto
  112.     IEnumerator SubtitlesManager(string respuesta)
  113.     {
  114.         string line = "";
  115.  
  116.         for (int i = 0; i < respuesta.Length; i++)
  117.         {
  118.             if (i != 0 && i % 20 == 0) {
  119.                 //Response.text = line;
  120.  
  121.                 textBox.GetComponent<Text> ().text = line;
  122.                 yield return new WaitForSeconds (2);
  123.                 line = "";
  124.             }
  125.             else
  126.             {
  127.                 line += respuesta [i];
  128.             }
  129.         }
  130.  
  131.         if (line.Length != 0)
  132.         {
  133.             textBox.GetComponent<Text> ().text = line;
  134.             yield return new WaitForSeconds (2);
  135.         }
  136.  
  137.         textBox.GetComponent<Text> ().text = "";
  138.            
  139.     }
  140.     // Agregue esto
  141.    
  142.     private void HandleIntents(RuntimeIntent runTimeIntent)
  143.     {
  144.         switch (runTimeIntent.intent)
  145.         {
  146.             case "pulmones":
  147.                 break;
  148.  
  149.             case "alveolos":
  150.                 break;
  151.  
  152.             case "bronquios":
  153.                 break;
  154.  
  155.             case "bronquiolos":
  156.                 break;
  157.  
  158.             case "cartilago":
  159.                 break;
  160.  
  161.             case "glandula":
  162.                 break;
  163.  
  164.             case "hueso":
  165.                 break;
  166.  
  167.             default:
  168.                 break;
  169.         }
  170.     }
  171.  
  172.     private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
  173.     {
  174.         Log.Error("ExampleConversation.OnFail()", "Error received: {0}", error.ToString());
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement