示例#1
0
        public JsonWeb SpellRequest(string checkText, eSpellMode spellMode)
        {
            queryString.Clear();
            queryString["mode"] = spellMode.ToString();
            queryString["mkt"]  = mkt;
            //queryString["preContextText"] = "";
            //queryString["postContextText"] = "";
            var uri = "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/?" + queryString;

            string temp = Regex.Replace(checkText, @"[^A-Za-z0-9 _\-]", " ");

            if (temp.Length == checkText.Length)
            {
                checkText = temp;
            }
            Dictionary <string, string> dict = new Dictionary <string, string>();

            dict["text"] = checkText;
            FormUrlEncodedContent content = new FormUrlEncodedContent(dict);

            //string json = new JavaScriptSerializer().Serialize(new
            //{
            //    text = "bill clintom",
            //});

            //StringContent content1 = new StringContent("text="+json, Encoding.UTF8, "application/json");
            //content1.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //byte[] byteData = Encoding.UTF8.GetBytes(json);
            //ByteArrayContent content2 = new ByteArrayContent(byteData);
            //content2.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //content.Headers.ContentLength = byteData.Length;
            //clientSpell.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = clientSpell.PostAsync(uri, content).Result;
            Stream stream = response.Content.ReadAsStreamAsync().Result;

            return((JsonWeb)jsonSerializer.ReadObject(stream));
        }
示例#2
0
文件: Search.cs 项目: levi1994/LitDev
        /// <summary>
        /// Do a text spell check (proof).  This is for longer text with more detailed information like in Word.
        /// </summary>
        /// <param name="text">The text to proof.</param>
        /// <param name="mode">A mode "Proof" (default for longer text) or "Spell" (for short or single word checks).</param>
        /// <returns>An array of spelling and other suggestions or "" if no suggestions found.</returns>
        public static Primitive GetProof(Primitive text, Primitive mode)
        {
            try
            {
                eSpellMode spellMode = ((string)mode).ToLower() == "spell" ? eSpellMode.spell : eSpellMode.proof;
                JsonWeb    jsonWeb   = cognitive.SpellRequest(text, spellMode);
                Primitive  result    = "";
                int        i         = 1;

                if (null != jsonWeb.flaggedTokens)
                {
                    foreach (var token in jsonWeb.flaggedTokens)
                    {
                        Primitive check = "";
                        check["token"]  = token.token;
                        check["type"]   = token.type;
                        check["offset"] = token.offset + 1;
                        if (null != token.suggestions)
                        {
                            Primitive suggestions = "";
                            int       j           = 1;
                            foreach (var suggestion in token.suggestions)
                            {
                                suggestions[j++] = suggestion.suggestion;
                            }
                            check["suggestions"] = suggestions;
                        }
                        result[i++] = check;
                    }
                }
                return(Utilities.CreateArrayMap(result));
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return("");
            }
        }
示例#3
0
        public JsonWeb SpellRequest(string checkText, eSpellMode spellMode)
        {
            queryString.Clear();
            queryString["mode"] = spellMode.ToString();
            queryString["mkt"] = mkt;
            //queryString["preContextText"] = "";
            //queryString["postContextText"] = "";
            var uri = "https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?" + queryString;

            string temp = Regex.Replace(checkText, @"[^A-Za-z0-9 _\-]", " ");
            if (temp.Length == checkText.Length) checkText = temp;
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict["text"] = checkText;
            FormUrlEncodedContent content = new FormUrlEncodedContent(dict);

            //string json = new JavaScriptSerializer().Serialize(new
            //{
            //    text = "bill clintom",
            //});

            //StringContent content1 = new StringContent("text="+json, Encoding.UTF8, "application/json");
            //content1.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //byte[] byteData = Encoding.UTF8.GetBytes(json);
            //ByteArrayContent content2 = new ByteArrayContent(byteData);
            //content2.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //content.Headers.ContentLength = byteData.Length;
            //clientSpell.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = clientSpell.PostAsync(uri, content).Result;
            Stream stream = response.Content.ReadAsStreamAsync().Result;
            return (JsonWeb)jsonSerializer.ReadObject(stream);
        }