Exemplo n.º 1
0
        private static async Task <List <SpellingCheckResult> > CheckSpelling(Dictionary <string, string> textsToCheck)
        {
            List <SpellingCheckResult> result = new List <SpellingCheckResult>();

            foreach (var text in textsToCheck)
            {
                var response = await spellCheckClient.SpellCheckerWithHttpMessagesAsync(
                    text : text.Value,
                    market : "pl-PL",
                    mode : "spell");

                if (response.Body.FlaggedTokens.Count == 0)
                {
                    continue;
                }

                result.Add(new SpellingCheckResult
                {
                    RowKey = text.Key,
                    SpellingFlaggedToken = response.Body.FlaggedTokens
                });
            }

            return(result);
        }
        private static async Task <string> CorrectSpelling(string text)
        {
            spellCheckClient = new SpellCheckClient(new Microsoft.Azure.CognitiveServices.Language.SpellCheck.ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            var result = await spellCheckClient.SpellCheckerWithHttpMessagesAsync(text : text.ToLowerInvariant(), mode : "proof");

            var firstSpellCheckResult = result.Body.FlaggedTokens.FirstOrDefault();

            if (firstSpellCheckResult != null)
            {
                if (firstSpellCheckResult.Suggestions?.Count > 0)
                {
                    return(firstSpellCheckResult.Suggestions.FirstOrDefault().Suggestion);
                }
                else
                {
                    return(text.ToLowerInvariant());
                }
            }
            else
            {
                return(text.ToLowerInvariant());
            }
        }
Exemplo n.º 3
0
        public string GetSpellCheckFromMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return(string.Empty);
            }

            try
            {
                var result = spellCheckClient.SpellCheckerWithHttpMessagesAsync(text: message, mode: MODE_SPELLCHECK, acceptLanguage: LANGUAGE_SPELLCHECK).Result;

                if (result?.Body.FlaggedTokens?.Count > 0)
                {
                    var firstspellCheckResult = result.Body.FlaggedTokens.FirstOrDefault();

                    if (firstspellCheckResult != null)
                    {
                        var suggestions = firstspellCheckResult.Suggestions;
                        if (suggestions?.Count > 0)
                        {
                            return(suggestions.FirstOrDefault().Suggestion);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(message);
            }


            return(message);
        }
Exemplo n.º 4
0
        public static async Task SpellCheckCorrection(string key)
        {
            var client = new SpellCheckClient(new ApiKeyServiceClientCredentials(key));

            var result = await client.SpellCheckerWithHttpMessagesAsync(text: "Bill Gatas", mode: "proof");
            Console.WriteLine("Correction for Query# \"bill gatas\"");

            // SpellCheck Results
            if (result?.Body.FlaggedTokens?.Count == 0)
            {
                throw new Exception("Didn't see any SpellCheck results..");
            }

            // find the first spellcheck result
            var firstspellCheckResult = result.Body.FlaggedTokens.FirstOrDefault();

            if (firstspellCheckResult == null)
            {
                throw new Exception("Couldn't get any Spell check results!");
            }

            Console.WriteLine("SpellCheck Results#{0}", result.Body.FlaggedTokens.Count);
            Console.WriteLine("First SpellCheck Result token: {0} ", firstspellCheckResult.Token);
            Console.WriteLine("First SpellCheck Result Type: {0} ", firstspellCheckResult.Type);
            Console.WriteLine("First SpellCheck Result Suggestion Count: {0} ", firstspellCheckResult.Suggestions.Count);

            var suggestions = firstspellCheckResult.Suggestions;
            if (suggestions?.Count > 0)
            {
                var firstSuggestion = suggestions.FirstOrDefault();
                Console.WriteLine("First SpellCheck Suggestion Score: {0} ", firstSuggestion.Score);
                Console.WriteLine("First SpellCheck Suggestion : {0} ", firstSuggestion.Suggestion);
            }
        }
        public async Task <SpellingResult> CheckSpellingAsync(string text, string clientId = "")
        {
            var market      = "en-GB";
            var geoLocation = London;
            var ipAddress   = GetPublicIpAddress();

            SpellCheckClient client = new SpellCheckClient(new ApiKeyServiceClientCredentials(Key));

            var response = await
                           client.SpellCheckerWithHttpMessagesAsync(text, market : market,
                                                                    clientId : clientId, clientIp : ipAddress, location : geoLocation);

            throw new NotImplementedException();
        }
        public async Task <SpellCheckResult> CheckSpellingAsync(string text)
        {
            if (_checkes.TryGetValue(text, out var existingCheck))
            {
                return(existingCheck);
            }

            var result = await _spellChecker.SpellCheckerWithHttpMessagesAsync(text : text, mode : "proof");

            var spellCheckResult = ResultFromFlags(text, result.Body.FlaggedTokens);

            _checkes.Add(text, spellCheckResult);

            return(spellCheckResult);
        }
        public async Task <string> CheckSpellingAsync(ArticleViewModel model)
        {
            var market      = "en-GB";
            var clientId    = ""; // we don't have this yet
            var geoLocation = London;
            var ipAddress   = GetPublicIpAddress();

            SpellCheckClient client = new SpellCheckClient(new ApiKeyServiceClientCredentials(Key));

            var response = await
                           client.SpellCheckerWithHttpMessagesAsync(model.Body, market : market,
                                                                    clientId : clientId, clientIp : ipAddress, location : geoLocation);

            return(await response.Response.Content.ReadAsStringAsync());
        }
Exemplo n.º 8
0
        public static string Query_SpellCheck(string querytext)
        {
            List <String> correction = new List <string>();

            var client = new SpellCheckClient(new ApiKeyServiceClientCredentials("c1a10976d73e469382f0860c0ab2dac4"));

            char[]   splits = new char[] { ' ', '\t', '\'', '"', '-', '(', ')', ',', '’', '\n', ':', ';', '?', '.', '!' }; // Set token delimiters
            string[] tokens = querytext.ToLower().Split(splits, StringSplitOptions.RemoveEmptyEntries);                    // Tokenisation

            var result = client.SpellCheckerWithHttpMessagesAsync(text: querytext.Replace("Title:", "").Replace("Author:", ""), mode: "proof", acceptLanguage: "en-US").Result;

            //Console.WriteLine("Correction for Query:" + querytext);

            // SpellCheck Results
            if (result?.Body.FlaggedTokens?.Count > 0)
            {
                foreach (var x in result.Body.FlaggedTokens)
                {
                    int index = Array.IndexOf(tokens, x.Token);

                    var suggestions = x.Suggestions;
                    if (suggestions?.Count > 0)
                    {
                        var firstSuggestion = suggestions.FirstOrDefault();
                        //Console.WriteLine(firstSuggestion.Suggestion);
                        tokens[index] = firstSuggestion.Suggestion;
                        correction.Add("\"" + x.Token + "\" has been corrected to \"" + tokens[index] + "\"");
                    }
                }
            }

            string afterQuery = "";

            foreach (string t in tokens)
            {
                afterQuery += t + " ";
            }
            //Console.WriteLine(afterQuery.TrimEnd());
            if (correction.Count > 0)
            {
                var message = string.Join(Environment.NewLine, correction);
                MessageBox.Show("Words have been corrected by the Spell Checker\n\n" + message, "Word Correction", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return(afterQuery.TrimEnd().Replace("title", "Title:").Replace("author", "Author:"));
        }
        public async Task <SpellingResult> CheckSpellingAsync(string text, string clientId = "")
        {
            string Key         = _configuration["BingSpellCheckKey"];
            var    geoLocation = Istanbul;
            var    ipAddress   = GetPublicIpAddress();

            SpellCheckClient client = new SpellCheckClient(new ApiKeyServiceClientCredentials(Key));
            var response            = await client.SpellCheckerWithHttpMessagesAsync(text, market : Market, clientId : clientId, clientIp : ipAddress, location : geoLocation, mode : Mode);

            HttpResponseHeaders responseHeaders = response.Response.Headers;
            var spellingResult = new SpellingResult
            {
                ClientId = GetHeaderValue(responseHeaders, "X-MSEdge-ClientID"),
                TraceId  = GetHeaderValue(responseHeaders, "BingAPIs-SessionId"),
                Text     = ProcessResults(text, response.Body.FlaggedTokens)
            };

            return(spellingResult);
        }
        public async Task <SpellingResult> CheckSpellingAsync(string text, string clientId = "")
        {
            var market      = "en-GB";
            var geoLocation = London;
            var ipAddress   = GetPublicIpAddress();

            SpellCheckClient client = new SpellCheckClient(new ApiKeyServiceClientCredentials(Key));

            var response = await
                           client.SpellCheckerWithHttpMessagesAsync(text, market : market,
                                                                    clientId : clientId, clientIp : ipAddress, location : geoLocation);

            var spellingResult = new SpellingResult
            {
                ClientId = GetHeaderValue(response.Response.Headers, "X-MSEdge-ClientID"),
                TraceId  = GetHeaderValue(response.Response.Headers, "BingAPIs-SessionId"),
                Text     = ProcessResults(text, response.Body.FlaggedTokens)
            };

            return(spellingResult);
        }
Exemplo n.º 11
0
        private async Task <string> SpellCheckAsync(string inputText)
        {
            var client = new SpellCheckClient(new ApiKeyServiceClientCredentials(ConfigurationManager.AppSettings["BingSpellCheckKey"]));

            //var response = await client.SpellCheckerWithHttpMessagesAsync(inputText);

            /*
             * For SpellCheck mode, The default is Proof. 1) Proof—Finds most spelling and grammar mistakes.
             * 2) Spell—Finds most spelling mistakes but does not find some of the grammar errors that Proof
             * catches (for example, capitalization and repeated words). Possible values
             * include: 'proof', 'spell'
             * Ref SDK - https://github.com/Azure/azure-sdk-for-net/blob/psSdkJson6/src/SDKs/CognitiveServices/dataPlane/Language/SpellCheck/BingSpellCheck/Generated/SpellCheckClient.cs
             * */
            var response = await client.SpellCheckerWithHttpMessagesAsync(text : inputText,
                                                                          mode : "spell", acceptLanguage : "en-US");

            //Get the service task result
            var result = response.Response;

            var spellcheckContent = await result.Content.ReadAsStringAsync();

            return(spellcheckContent);
        }