Example #1
0
        static async Task SpellCheckAsync(string subscriptionKey)
        {
            try
            {
                Console.WriteLine("Please enter your text to correct in one line:");

                var text = Console.ReadLine();

                /**init a spellcheck client*/
                var client = new SpellCheckClient(new ApiKeyServiceClientCredentials(subscriptionKey));

                /**
                 * the API reference: https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/bing-spell-check-api-v7-reference
                 * mode: "proof", Finds most spelling and grammar mistakes
                 * market: The market must be in the form <language code>-<country code>
                 */
                var result = await client.SpellCheckerAsync(text : text, mode : "proof", market : "en-US");

                var correct = Analyze(result, text);

                Console.WriteLine("=> " + correct);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public void SpellCheck()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "SpellCheck");

                ISpellCheckClient client = new SpellCheckClient(new ApiKeyServiceClientCredentials(SubscriptionKey), HttpMockServer.CreateInstance());

                var resp = client.SpellCheckerAsync(text: "Bill Gatas").Result;
                Assert.NotNull(resp);
                Assert.NotNull(resp.FlaggedTokens);
                Assert.Equal(1, resp.FlaggedTokens.Count);

                // verify token
                var flaggedToken = resp.FlaggedTokens.First();
                Assert.NotNull(flaggedToken);
                Assert.NotNull(flaggedToken.Token);
                Assert.Equal("Gatas", flaggedToken.Token);
                Assert.Equal("UnknownToken", flaggedToken.Type);

                // verify suggestions
                Assert.Equal(1, flaggedToken.Suggestions.Count);

                var suggestion = flaggedToken.Suggestions.First();
                Assert.NotNull(suggestion);
                Assert.Equal(0.887992481895458, suggestion.Score);
                Assert.Equal("Gates", suggestion.Suggestion);
            }
        }
Example #3
0
        public static async Task SpellCheckError(string key)
        {
            var client = new SpellCheckClient(new ApiKeyServiceClientCredentials(key));

            try
            {
                var result = await client.SpellCheckerAsync("", mode: "proof");
                throw new Exception("Client didn't throw correct exception.");
            }
            catch (ErrorResponseException ex)
            {
                Console.WriteLine("ErrorResponse : {0} ", ex.Message);
            }
        }
        internal static async Task <string> SpellCheck(this string text, string apiKey, string countryCode = "US", string market = "en-US")
        {
            if (string.IsNullOrEmpty(text))
            {
                return(text);
            }

            var client           = new SpellCheckClient(new ApiKeyServiceClientCredentials(apiKey));
            var spellCheckResult = await client.SpellCheckerAsync(text, setLang : market, market : market, countryCode : countryCode, mode : "spell");

            foreach (var flaggedToken in spellCheckResult.FlaggedTokens)
            {
                text = text.Replace(flaggedToken.Token, flaggedToken.Suggestions.FirstOrDefault().Suggestion);
            }
            return(text);
        }
        internal static async Task <string> SpellCheck(this string text, string apiKey)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(text);
            }

            var client           = new SpellCheckClient(new ServiceCredentials.ApiKeyServiceClientCredentials(apiKey));
            var spellCheckResult = await client.SpellCheckerAsync(text);

            foreach (var flaggedToken in spellCheckResult.FlaggedTokens)
            {
                text = text.Replace(flaggedToken.Token, flaggedToken.Suggestions.FirstOrDefault().Suggestion);
            }
            return(text);
        }
        public static void Main(string[] args)
        {
            string subscriptionKey = "c66350565d054032b93fdb323939d884";

            string sourceString = "james had a very good day at at work he wass so plaesed with himslf";

            SpellCheckClient client = new SpellCheckClient(new ApiKeyServiceClientCredentials(subscriptionKey));
            var spellResult         = client.SpellCheckerAsync(sourceString, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "proof", null, null).Result;

            foreach (var token in spellResult.FlaggedTokens)
            {
                Console.WriteLine(PrettyToken(token));
            }

            Console.WriteLine("Finished");
            Console.ReadKey();
        }
Example #7
0
        //TODO: AZURE SPEEL CHECK
        public string ValidateSpellCheck(string text)
        {
            var spellCheckResult = _spellCheckClient
                                   .SpellCheckerAsync(text, setLang: market, market: market, countryCode: countryCode, mode: mode);



            foreach (var check in spellCheckResult.Result.FlaggedTokens)
            {
                var maxScore = check.Suggestions.Where(p => p.Score == check.Suggestions.Max(m => m.Score)).SingleOrDefault();
                text = text.Replace(check.Token, maxScore.Suggestion);
            }
            ;



            return(text);
        }
        public static async Task checkSpelling(SpellCheckClient client, string query)
        {
            var result = await client.SpellCheckerAsync(text : query, mode : "proof");

            Console.WriteLine("Original query: \n" + query);
            Console.WriteLine();
            Console.WriteLine("Misspelled words:");
            foreach (SpellingFlaggedToken token in result.FlaggedTokens)
            {
                Console.WriteLine(token.Token);
            }

            Console.WriteLine();
            Console.WriteLine("Suggested corrections:");
            foreach (SpellingFlaggedToken token in result.FlaggedTokens)
            {
                foreach (SpellingTokenSuggestion suggestion in token.Suggestions)
                {
                    // Confidence values range from 0 (none) to 1.0 (full confidence)
                    Console.WriteLine(suggestion.Suggestion + " with confidence " + Math.Round((decimal)suggestion.Score, 2));
                }
            }
        }