Exemplo n.º 1
0
        /// <summary>
        /// Detect what language the text should be translated from
        /// </summary>
        /// <param Name="text">The text that needs to be translated</param>
        /// <param Name="argument">The argument provided to the command</param>
        /// <returns>The language the text should be translated from</returns>
        protected string DetectFrom(string text, string[] arguments)
        {
            //If the user specified a language in the argument, get that
            string detected = DetectCommon("from", arguments);

            if (detected != null)
            {
                return(detected);
            }

            //Otherwise, detect the language using the Azure Bing Detection service
            else
            {
                //Send a request to the service
                Dictionary <string, string> args = new Dictionary <string, string>()
                {
                    { "text", text }
                };
                using (Stream stream = LanguageDetectService.Request(args))
                {
                    //Read and return the service'sections response
                    DataContractSerializer dcs = new DataContractSerializer(Type.GetType("System.String"));
                    return((string)dcs.ReadObject(stream));;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Translate text between two languages using the Azure Bing Translate API
        /// </summary>
        /// <param Name="to">The language to translate to</param>
        /// <param Name="from">The language to translate from</param>
        /// <param Name="text">The text to translate</param>
        /// <returns>The translated text</returns>
        protected string Translate(string to, string from, string text)
        {
            //Pack the argument into a dictionary for use in the web service call
            Dictionary <string, string> args = new Dictionary <string, string>()
            {
                { "to", to },
                { "from", from },
                { "text", text }
            };

            //Send a request to the Translate service
            using (Stream stream = TranslateService.Request(args))
            {
                //Read the result from the Translate service
                DataContractSerializer dcs = new DataContractSerializer(Type.GetType("System.String"));
                return((string)dcs.ReadObject(stream));
            }
        }