Пример #1
0
        public string Translate(string sentence, string inLang, string outLang)
        {
            sentence = sentence.Replace(":", " : ");
            string result = string.Empty;
            string url    = @"https://papago.naver.com/apis/n2mt/translate";

            if (_PapagoEncoder == null)
            {
                _PapagoEncoder = new PapagoEncoder(GlobalTranslationSettings.PapagoEncoderPath, _Logger);
            }

            if (inLang == "auto")
            {
                inLang = DetectLanguage(sentence);
            }
            if (inLang.Length == 0)
            {
                return(result);
            }

            if (_PapagoEncoder.IsAvaliable)
            {
                try
                {
                    PapagoTranslationRequest papagoRequest = new PapagoTranslationRequest()
                    {
                        deviceId    = "",
                        dict        = false,
                        dictDisplay = 0,
                        honorific   = false,
                        instant     = false,
                        paging      = false,
                        source      = inLang,
                        target      = outLang,
                        text        = sentence
                    };

                    var reqv = _PapagoEncoder.EncodePapagoTranslationRequest(JsonConvert.SerializeObject(papagoRequest));

                    var tmpResponse = PapagoReader.GetWebData(url, WebApi.WebReader.WebMethods.POST, reqv);

                    PapagoResponse papagoResponse = JsonConvert.DeserializeObject <PapagoResponse>(tmpResponse);

                    result = papagoResponse.translatedText;
                }
                catch (Exception e)
                {
                    _Logger?.WriteLog(e.ToString());
                }
            }

            return(result);
        }
Пример #2
0
        public string Translate(string sentence, string inLang, string outLang)
        {
            string result = String.Empty;

            try
            {
                string yaApiKey = @"";
                string _outLang = outLang;
                string _inLang  = inLang;

                string _baseUrl = @"https://translate.yandex.net/api/v1.5/tr.json/translate?lang={0}-{1}&key={2}";
                string url      = string.Format(_baseUrl, _inLang, _outLang, yaApiKey);

                var tmpResult = _YandexWebReader.GetWebData(url, WebApi.WebReader.WebMethods.POST, "text=" + sentence);

                var resp = JsonConvert.DeserializeObject <YandexResponse>(tmpResult);

                if (resp.code == 200)
                {
                    for (int i = 0; i < resp.text.Count; i++)
                    {
                        result += resp.text[i] + " ";
                    }
                }
            }
            catch (Exception e)
            {
                _Logger?.WriteLog(Convert.ToString(e));
            }

            return(result);
        }
Пример #3
0
        public string Translate(string sentence, string inLang, string outLang)
        {
            string result = String.Empty;

            try
            {
                string _outLang = outLang;
                string _inLang  = inLang;

                string _baseUrl = "https://translate.google.com/m?hl=ru&sl={0}&tl={1}&ie=UTF-8&prev=_m&q={2}";
                string url      = string.Format(_baseUrl, _inLang, _outLang, sentence);

                var tmpResult = _GoogleWebReader.GetWebData(url, WebApi.WebReader.WebMethods.GET);

                var rxMatch = GoogleRx.Match(tmpResult);

                if (rxMatch.Success)
                {
                    result = System.Net.WebUtility.HtmlDecode(rxMatch.Value);
                }
            }
            catch (Exception e)
            {
                _Logger?.WriteLog(Convert.ToString(e));
            }

            return(result);
        }
        public string Translate(string sentence, string inLang, string outLang)
        {
            string result = String.Empty;

            try
            {
                string _outLang = outLang;
                string _inLang  = inLang;

                string _baseUrl = @"https://translate.multillect.com/form.json?from={0}&to={1}&text={2}";
                string url      = string.Format(_baseUrl, _inLang, _outLang, sentence);

                var tmpResult = _MultillectWebReader.GetWebData(url, WebApi.WebReader.WebMethods.GET);

                var resp = JsonConvert.DeserializeObject <MultillectResponse.MultillectRoot>(tmpResult);

                result = resp.result.translations;
            }
            catch (Exception e)
            {
                _Logger?.WriteLog(Convert.ToString(e));
            }

            return(result);
        }
        public BaiduTranslater()
        {
            try
            {
                string url = "https://fanyi.baidu.com/";

                BaiduWebRead = new WebApi.WebReader(@"fanyi.baidu.com");
                var tmpResult = BaiduWebRead.GetWebData(url, WebApi.WebReader.WebMethods.GET);

                tmpResult = BaiduWebRead.GetWebDataAndSetCookie(url, WebApi.WebReader.WebMethods.GET);
                Regex tokenRegex = new Regex("token: '(.*)'");
                Regex gtkRegex   = new Regex("gtk = '(.*)'");

                var tokenMatch = tokenRegex.Match(tmpResult);
                var gtkMatch   = gtkRegex.Match(tmpResult);

                if (tokenMatch.Success && gtkMatch.Success)
                {
                    token = tokenMatch.Value;
                    gtk   = gtkMatch.Value;
                    token = token.Substring(8, token.Length - 8).TrimEnd(new char[] { (char)39 });
                    gtk   = gtk.Substring(7, gtk.Length - 7).TrimEnd(new char[] { (char)39 });
                }
                string s = "In order to resolve this, Kindly go to the below path";
                Translate(s, "en", "zh");
            }
            catch (Exception e)
            {
                Logger.WriteLog(Convert.ToString(e));
            }
        }
Пример #6
0
        private void InitTranslator()
        {
            _IsInitialised        = false;
            _InitializationFailed = false;

            Task.Run(() =>
            {
                try
                {
                    _BaiduEncoder = new BaiduReqEncoder(GlobalTranslationSettings.BaiduEncoder, _Logger);

                    string url = "https://fanyi.baidu.com/";

                    var tmpResult = _BaiduWebRead.GetWebData(url, WebApi.WebReader.WebMethods.GET);

                    tmpResult        = _BaiduWebRead.GetWebDataAndSetCookie(url, WebApi.WebReader.WebMethods.GET);
                    Regex tokenRegex = new Regex("token: '(.*)'");
                    Regex gtkRegex   = new Regex("gtk = '(.*)'");

                    Match tokenMatch = tokenRegex.Match(tmpResult);
                    Match gtkMatch   = gtkRegex.Match(tmpResult);

                    if (tokenMatch.Success && gtkMatch.Success)
                    {
                        _Token = tokenMatch.Value;
                        _Gtk   = gtkMatch.Value;
                        _Token = _Token.Substring(8, _Token.Length - 8).TrimEnd(new char[] { (char)39 });
                        _Gtk   = _Gtk.Substring(7, _Gtk.Length - 7).TrimEnd(new char[] { (char)39 });

                        _IsInitialised = true;
                    }
                    else
                    {
                        _IsInitialised        = false;
                        _InitializationFailed = true;
                    }
                }
                catch (Exception e)
                {
                    _InitializationFailed = true;
                    _IsInitialised        = false;
                    _Logger?.WriteLog(e.ToString());
                }
            });
        }
        public string Translate(string sentence, string inLang, string outLang)
        {
            string reqv = encoder.encoding(sentence, inLang, outLang, gtk, token);

            var tmpResponse = BaiduWebRead.GetWebData("https://fanyi.baidu.com/v2transapi", WebApi.WebReader.WebMethods.POST, reqv);

            var unescaped = Regex.Unescape(tmpResponse);

            var result = JsonConvert.DeserializeObject <BaiduResponse>(unescaped);

            return(result.trans_result.data[0].dst);
        }