Exemplo n.º 1
0
        public static String[] Microsoft_GetStrings(string[] strs, string from, string to)
        {
            string texts = "[";
            string split = null;

            foreach (string str in strs)
            {
                texts += split + "\"" + str.Replace(",", ",") + "\"";
                if (null == split)
                {
                    split = ",";
                }
            }
            texts += "]";
            string httpstr = _http.GetRequest(_url_Microsoft_TranslateList + string.Format("?appId={0}&texts={1}&from={2}&to={3}&maxTranslations=5", _url_Microsoft_v2_key, texts, from, to));

            /*
             * IList<String[]> resultList = HelperBase.GetString(httpstr, "TranslatedText\\W{3}([^,]*)\\W,\\WTranslatedTextSentenceLengths");
             * string[] result = new string[resultList.Count];
             * for (int i = 0, len = resultList.Count; i < len; i++)
             * {
             *  result[i] = resultList[i][0];
             * }
             * */
            string[] result = HelperBase.GetString(1, httpstr, "TranslatedText\\W{3}([^,]*)\\W,\\WTranslatedTextSentenceLengths");
            return(result);
        }
Exemplo n.º 2
0
        public static string Microsoft_Get(string str, string from, string to)
        {
            string httpstr = _http.GetRequest(_url_Microsoft_Translate + string.Format("?appId={0}&text={1}&from={2}&to={3}", _url_Microsoft_v2_key, str, from, to));

            httpstr = HelperBase.GetString(httpstr, "\\\"(.*)\\\"", 1);
            return(httpstr.Replace("\\u000d", "").Replace("\\u000a", ""));
        }
Exemplo n.º 3
0
 public static string MeCabParseString(string input)
 {
     try
     {
         string result = MeCabParse(input);
         return(HelperBase.Repalce(result, "[\\t].*[\\n]?(EOS)?", " "));
         //return null;
     }
     catch (Exception ex)
     {
         return(ex.ToString());
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 防止缓存 给url添加参数
 /// </summary>
 /// <param name="targetUrl">目标地址</param>
 /// <returns>新地址</returns>
 private string GetNewUrl(string targetUrl)
 {
     if (!HelperBase.IsHaveString(targetUrl, "http[s]?://"))
     {
         targetUrl = "http://" + targetUrl;
     }
     if (HelperBase.IsHaveString(targetUrl, "\\?"))
     {
         targetUrl += "&";
     }
     else
     {
         targetUrl += "?";
     }
     targetUrl += HelperBase.GetRandomName();
     return(targetUrl);
 }
Exemplo n.º 5
0
        public static string ICTCLAParse(string input)
        {
            if (!ICTCLAS_Init("", 0))
            {
                HttpHelper http = HttpHelper.getInstance();
                http.Encoding = Encoding.UTF8;
                return(http.GetRequest(string.Format("http://ilab.kansea.com/wwwbak/split/test.php?d=string&q={0}", input)));
            }
            else
            {
                //ICTCLAS_FileProcess("Input.txt", "Input_result.txt", 1);
                StringBuilder sResult = new StringBuilder(10000);
                ICTCLAS_ParagraphProcessE(input, sResult, 1);

                ICTCLAS_Exit();
                string result = sResult.ToString();
                return(HelperBase.Repalce(result, "\\/[\\S]*\\s", " "));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Post And Get
        /// </summary>
        /// <param name="targetUrl">URL</param>
        /// <param name="method">Method</param>
        /// <param name="accept">Accept</param>
        /// <param name="contentType">Content Type</param>
        /// <param name="userAgent">User Agent</param>
        /// <param name="encoding">Encoding</param>
        /// <param name="cookies">Cookies</param>
        /// <param name="strPostdata">Post Data</param>
        public string GetRequest(string targetUrl, string method, string accept, string contentType, string userAgent, Encoding encoding, CookieContainer cookies, string strPostdata)
        {
            this.SetRequest(targetUrl, method, accept, contentType, userAgent, encoding, cookies);
            if (null != strPostdata && methodType.POST == this.Method)
            {
                byte[] buffer = this.Encoding.GetBytes(strPostdata);
                this.Request.ContentLength = buffer.Length;
                this.Request.GetRequestStream().Write(buffer, 0, buffer.Length);
                this.Request.GetRequestStream().Close();
            }
            #region 得到请求的response
            string resultData = null;
            try
            {
                using (this.Response = (HttpWebResponse)this.Request.GetResponse())
                {
                    Stream ResposeStream = this.Response.GetResponseStream();
                    if (Encoding.Default == this.Encoding && this.AutoGetEncoding)
                    {
                        MemoryStream _stream     = HelperBase.CopyStream(ResposeStream);
                        byte[]       RawResponse = _stream.ToArray();
                        string       temp        = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);

                        string charset = HelperBase.GetString(temp, "<meta([^<]*)\\bcharset\\b\\s*=\\s*([^<';\"]*)[\"']", 2);
                        if (!string.IsNullOrEmpty(charset))
                        {
                            this.Encoding = Encoding.GetEncoding(charset);
                            resultData    = this.Encoding.GetString(RawResponse);
                        }
                        else
                        {
                            if (this.Response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.Default;
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(this.Response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(this.Response.CharacterSet);
                                }
                            }
                        }
                    }
                    else
                    {
                        string ContentEncoding = this.Response.ContentEncoding;
                        if (null != ContentEncoding && ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            using (this.Reader = new StreamReader(new GZipStream(ResposeStream, CompressionMode.Decompress), this.Encoding))
                            {
                                resultData = this.Reader.ReadToEnd();
                            }
                        }
                        else
                        {
                            using (this.Reader = new StreamReader(ResposeStream, this.Encoding))
                            {
                                resultData = this.Reader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError &&
                    ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        resultData = "Error:Not found";
                    }
                    else
                    {
                        resultData = "Error:Forbidden";
                    }
                }
                else
                {
                    resultData = "Error:Unknow";
                }
            }
            return(resultData);

            #endregion
        }