Beispiel #1
0
    public override HoundServerJSON do_text_request(string query,
                                                    ConversationStateJSON conversation_state,
                                                    RequestInfoJSON request_info)
    {
        string url =
            text_request_url_base + "?query=" + Uri.EscapeUriString(query);
        Fetch fetcher = new Fetch(url);

        set_up_fetch(fetcher, conversation_state, request_info);
        try
        {
            HoundServerJSON.HoldingGenerator generator =
                new HoundServerJSON.HoldingGenerator(
                    "the result from the Hound server");
            ResultHandler handler = new ResultHandler(generator);
            fetcher.get(handler);
            return(generator.value);
        }
        catch (Exception e1)
        {
            Console.Error.Write("Error trying to fetch from `{0}': {1}\n", url,
                                e1.Message);
            return(null);
        }
    }
        public ICustomActivityResult Execute()
        {
            HoundCloudRequester requester = new HoundCloudRequester(client_id, client_key, user_id);

            RequestInfoJSON.TypeClientVersion client_version = new RequestInfoJSON.TypeClientVersion();
            client_version.key     = 0;
            client_version.choice0 = "1.0";

            RequestInfoJSON request_info = new RequestInfoJSON();

            request_info.setUnitPreference(RequestInfoJSON.TypeUnitPreference.UnitPreference_US);
            request_info.setRequestID(Guid.NewGuid().ToString());
            request_info.setSessionID("");
            request_info.setClientVersion(client_version);


            ConversationStateJSON conversation_state = null;


            HoundServerJSON hound_result;

            hound_result = requester.do_text_request(request_text, conversation_state, request_info);
            CommandResultJSON commandResult = hound_result.elementOfAllResults(0);


            String resultStr = commandResult.getWrittenResponseLong();

            return(this.GenerateActivityResult(resultStr));
        }
Beispiel #3
0
    public override VoiceRequest start_voice_request(
        ConversationStateJSON conversation_state,
        RequestInfoJSON request_info, PartialHandler partial_handler)
    {
        Fetch fetcher = new Fetch(voice_request_url_base);

        set_up_fetch(fetcher, conversation_state, request_info);
        return(new LocalVoiceRequest(fetcher, partial_handler));
    }
Beispiel #4
0
    public static ConversationStateJSON from_file(TextReader fp, string file_name, bool ignore_extras)
    {
        ConversationStateJSON result = new ConversationStateJSON();
        StringWriter          stream = new StringWriter();
        JSONStreamWriter      writer = new JSONStreamWriter(stream, JSONWriter.whitespace_style.WHITESPACE_NONE);

        JSONParse.parse_json_value(fp, file_name, writer);
        result.contents = stream.ToString();
        return(result);
    }
Beispiel #5
0
    public static ConversationStateJSON from_text(string text, bool ignore_extras)
    {
        ConversationStateJSON result = new ConversationStateJSON();
        StringWriter          stream = new StringWriter();
        JSONStreamWriter      writer = new JSONStreamWriter(stream, JSONWriter.whitespace_style.WHITESPACE_NONE);

        JSONParse.parse_json_value(text, "Text for ConversationStateJSON", writer);
        result.contents = stream.ToString();
        return(result);
    }
Beispiel #6
0
    public static ConversationStateJSON from_json(JSONValue json_value, bool ignore_extras, bool allow_incomplete, bool allow_unpolished)
    {
        ConversationStateJSON result = new ConversationStateJSON();
        StringWriter          stream = new StringWriter();
        JSONStreamWriter      writer = new JSONStreamWriter(stream, JSONWriter.whitespace_style.WHITESPACE_NONE);

        json_value.write(writer);
        result.contents = stream.ToString();
        return(result);
    }
Beispiel #7
0
 public abstract VoiceRequest start_voice_request(
     ConversationStateJSON conversation_state,
     RequestInfoJSON request_info,
     PartialHandler partial_handler = null);
Beispiel #8
0
 public abstract HoundServerJSON do_text_request(string query,
                                                 ConversationStateJSON conversation_state,
                                                 RequestInfoJSON request_info);
Beispiel #9
0
    public static int sample_hound_driver(HoundRequester requester)
    {
        string session_id = Guid.NewGuid().ToString();
        ConversationStateJSON conversation_state = null;

        while (true)
        {
            string line = Console.ReadLine();
            if (line == null)
            {
                return(0);
            }
            RequestInfoJSON request_info = new RequestInfoJSON();
            request_info.setUnitPreference(
                RequestInfoJSON.TypeUnitPreference.UnitPreference_US);
            request_info.setRequestID(Guid.NewGuid().ToString());
            request_info.setSessionID(session_id);
            RequestInfoJSON.TypeClientVersion client_version =
                new RequestInfoJSON.TypeClientVersion();
            client_version.key     = 0;
            client_version.choice0 = "1.0";
            request_info.setClientVersion(client_version);

            HoundServerJSON hound_result;
            if ((line.StartsWith("-audio ")) ||
                (line.StartsWith("-transcript-audio ")) ||
                (line.StartsWith("-slow-transcript-audio ")))
            {
                bool go_slowly       = line.StartsWith("-slow-transcript-audio ");
                bool show_transcript =
                    (line.StartsWith("-transcript-audio ") ||
                     line.StartsWith("-slow-transcript-audio "));
                LocalPartialHandler partial_handler =
                    new LocalPartialHandler(show_transcript);
                HoundRequester.VoiceRequest request =
                    requester.start_voice_request(conversation_state,
                                                  request_info, partial_handler);
                int file_name_position = 0;
                while (line[file_name_position] != ' ')
                {
                    ++file_name_position;
                }
                ++file_name_position;
                String       file_name = line.Substring(file_name_position);
                BinaryReader audio_stream;
                try
                {
                    audio_stream = new BinaryReader(
                        File.Open(file_name, FileMode.Open));
                }
                catch (Exception e1)
                {
                    Console.Error.Write(
                        "Error trying to open audio file `{0}': {1}\n",
                        file_name, e1.Message);
                    return(1);
                }
                while (true)
                {
                    byte[] buffer     = new byte[2052];
                    int    byte_count = audio_stream.Read(buffer, 0, 2052);
                    if (byte_count > 0)
                    {
                        request.add_audio(byte_count, buffer);
                    }
                    if (byte_count < 2052)
                    {
                        break;
                    }
                    if (go_slowly)
                    {
                        Thread.Sleep(500);
                    }
                }
                audio_stream.Close();
                hound_result = request.finish();
            }
            else
            {
                hound_result = requester.do_text_request(line,
                                                         conversation_state, request_info);
            }

            if (hound_result == null)
            {
                return(1);
            }

            if (hound_result.hasAllResults())
            {
                if (hound_result.countOfAllResults() == 0)
                {
                    Console.Write("No match.\n");
                    conversation_state = null;
                }
                else
                {
                    CommandResultJSON command =
                        hound_result.elementOfAllResults(0);
                    Console.Write("Result");
                    if (hound_result.hasLocalOrRemote())
                    {
                        Console.Write(" [{0}]",
                                      ((hound_result.getLocalOrRemote() ==
                                        HoundServerJSON.TypeLocalOrRemote.
                                        LocalOrRemote_Local) ? "Local" :
                                       "Remote"));
                    }
                    Console.Write(": {0}\n", command.getWrittenResponseLong());
                    if (command.hasConversationState())
                    {
                        conversation_state = command.getConversationState();
                    }
                    else
                    {
                        conversation_state = null;
                    }
                }
            }
            else if (hound_result.hasErrorMessage())
            {
                Console.Write("Error from server: {0}\n",
                              hound_result.getErrorMessage());
                conversation_state = null;
            }
            else
            {
                Console.Write("No result or error from server.\n");
                conversation_state = null;
            }
        }
    }
Beispiel #10
0
 protected Generator(bool ignore_extras) : base(JSONWriter.whitespace_style.WHITESPACE_NONE)
 {
     result = new ConversationStateJSON();
 }
Beispiel #11
0
 protected override void handle_result(ConversationStateJSON result)
 {
     top.value.Add(result);
 }
Beispiel #12
0
        protected override void handle_result(ConversationStateJSON result)
        {
//@@@            Debug.Assert(!have_value);
            have_value = true;
            value      = result;
        }
Beispiel #13
0
 public override void reset()
 {
     result = new ConversationStateJSON();
 }
Beispiel #14
0
 protected abstract void handle_result(ConversationStateJSON new_result);
Beispiel #15
0
    private void set_up_fetch(Fetch fetcher,
                              ConversationStateJSON conversation_state,
                              RequestInfoJSON request_info)
    {
        fetcher.setUserAgent("HoundCloudRequesterCSharp/1.0");

        if (use_proxy)
        {
            fetcher.setProxyServer(proxy_server, proxy_port);
            int param_count = proxy_parameters.Count;
            for (int param_num = 0; param_num < param_count; ++param_num)
            {
                fetcher.setProxyParameter(proxy_parameters[param_num].key,
                                          proxy_parameters[param_num].value);
            }
        }

        long time_stamp =
            (long)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).
                   TotalSeconds);

        request_info.setTimeStamp(time_stamp);
        if (conversation_state != null)
        {
            request_info.setConversationState(conversation_state);
        }
        string request_id = request_info.getRequestID();

        request_info.setUserID(user_id);
        request_info.setClientID(client_id);
        request_info.setPartialTranscriptsDesired(true);
        request_info.setObjectByteCountPrefix(false);
        request_info.setResultVersionAccepted(1.1);
        request_info.setSDK("C Sharp");
        {
            JSONObjectValue sdk_info = new JSONObjectValue();
            sdk_info.setField("SDKVariant",
                              new JSONStringValue(BuildInfo.SDKVariant));
            sdk_info.setField("SDKPrivateDomains",
                              new JSONStringValue(BuildInfo.SDKPrivateDomains));
            sdk_info.setField("BuildUser",
                              new JSONStringValue(BuildInfo.BuildUser));
            sdk_info.setField("BuildDate",
                              new JSONStringValue(BuildInfo.BuildDate));
            sdk_info.setField("BuildMachine",
                              new JSONStringValue(BuildInfo.BuildMachine));
            sdk_info.setField("BuildSVNRevision",
                              new JSONStringValue(BuildInfo.BuildSVNRevision));
            sdk_info.setField("BuildSVNBranch",
                              new JSONStringValue(BuildInfo.BuildSVNBranch));
            sdk_info.setField("BuildNumber",
                              new JSONStringValue(BuildInfo.BuildNumber));
            sdk_info.setField("BuildKind",
                              new JSONStringValue(BuildInfo.BuildKind));
            sdk_info.setField("BuildVariant",
                              new JSONStringValue(BuildInfo.BuildVariant));
            request_info.setSDKInfo(sdk_info);
        }
        StringWriter     request_info_stream = new StringWriter();
        JSONStreamWriter request_info_writer = new JSONStreamWriter(
            request_info_stream,
            JSONWriter.whitespace_style.WHITESPACE_NONE);

        request_info.write_as_json(request_info_writer);
        fetcher.setParameter("Hound-Request-Info",
                             request_info_stream.ToString());

        string auth_id = user_id + ";" + request_id;

        fetcher.setParameter("Hound-Request-Authentication", auth_id);

        byte[]     key_bits     = Base64.base64url_decode(client_key);
        HMACSHA256 sha_256_hmac = new HMACSHA256(key_bits);

        byte[] hash_result = sha_256_hmac.ComputeHash(
            Encoding.UTF8.GetBytes(
                String.Format("{0}{1}", auth_id, time_stamp)));

        fetcher.setParameter("Hound-Client-Authentication",
                             client_id + ";" + String.Format("{0}", time_stamp) + ";" +
                             new String(Base64.base64url_encode(hash_result)));

        if (request_info.hasInputLanguageEnglishName())
        {
            fetcher.setParameter("Hound-Input-Language-English-Name",
                                 request_info.getInputLanguageEnglishName());
        }
        if (request_info.hasInputLanguageNativeName())
        {
            fetcher.setParameter("Hound-Input-Language-Native-Name",
                                 request_info.getInputLanguageNativeName());
        }
        if (request_info.hasInputLanguageIETFTag())
        {
            fetcher.setParameter("Hound-Input-Language-IETF-Tag",
                                 request_info.getInputLanguageIETFTag());
        }

        fetcher.setTimeoutSeconds(30);
    }