Esempio n. 1
0
        public static List <SpellCheckCollation> Collate(SpellingResult result, String originalQuery, int maxCollations)
        {
            List <SpellCheckCollation> collations = new List <SpellCheckCollation>();

            int collNo = 0;
            PossibilityIterator possibilityIter = new PossibilityIterator(result.getSuggestions());

            while (collNo < maxCollations && possibilityIter.hasNext())
            {
                RankedSpellPossibility possibility = (RankedSpellPossibility)possibilityIter.next();
                string collationQueryStr           = GetCollationQuery(originalQuery, possibility.getCorrections());

                collNo++;
                SpellCheckCollation collation = new SpellCheckCollation();
                collation.CollationQuery = collationQueryStr;
                collation.InternalRank   = possibility.getRank();

                var misspellingsAndCorrections = new List <KeyValuePair <string, string> >();
                for (var iter = possibility.getCorrections().iterator(); iter.hasNext();)
                {
                    SpellCheckCorrection corr = (SpellCheckCorrection)iter.next();
                    misspellingsAndCorrections.Add(new KeyValuePair <string, string>(corr.getOriginal().term(), corr.getCorrection()));
                }

                collation.MisspellingsAndCorrections = misspellingsAndCorrections;
                collations.Add(collation);
            }
            return(collations);
        }
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            try
            {
                string clientId       = null;
                string clientIdCookie = null;
                _httpContextAccessor.HttpContext.Request.Cookies.TryGetValue("ClientId", out clientIdCookie);
                clientId = clientIdCookie ?? "";

                string         userText      = turnContext.Activity.Text;
                SpellingResult spelledResult = _spellingService.CheckSpellingAsync(userText, clientId).Result;

                if (spelledResult.ClientId != null)
                {
                    _httpContextAccessor.HttpContext.Response.Cookies.Append("ClientId", spelledResult.ClientId);
                }

                string spelledText = spelledResult.Text;
                if (!string.IsNullOrEmpty(spelledText))
                {
                    string replyText = $"Echo: {spelledText}";
                    await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
                }
            }
            catch (System.Exception e)
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("error", "error"), cancellationToken);
            }
        }
        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);
        }
Esempio n. 4
0
        public SpellingResult GetSuggestions(SpellingOptions options)
        {
            SpellingResult result          = new SpellingResult(options.tokens);
            bool           haveSuggestions = false;

            IndexReader reader = options.reader;
            Term        term   = field != null ? new Term(field, "") : null;
            //float theAccuracy = (options.accuracy == java.lang.Float.MIN_VALUE) ? spellChecker.getAccuracy() : options.accuracy;

            int count = java.lang.Math.max(options.count, DEFAULT_SUGGESTION_COUNT);

            for (var iter = options.tokens.iterator(); iter.hasNext();)
            {
                Token    token       = (Token)iter.next();
                string   tokenText   = new string(token.termBuffer(), 0, token.termLength());
                string[] suggestions = spellChecker.suggestSimilar(tokenText,
                                                                   count,
                                                                   field != null ? reader : null,
                                                                   field,
                                                                   options.onlyMorePopular /*, theAccuracy*/);
                if (suggestions.Length == 1 && suggestions[0].Equals(tokenText))
                {
                    //These are spelled the same, continue on
                    continue;
                }

                if (options.extendedResults && reader != null && field != null)
                {
                    term = term.createTerm(tokenText);
                    var termFreq = reader.docFreq(term);
                    result.add(token, termFreq);

                    // AC: add the original term to suggestions if it's frequent enough
                    // TODO: make the treshold configurable
                    if (termFreq > 100)
                    {
                        result.add(token, tokenText, termFreq);
                    }

                    int countLimit = java.lang.Math.min(options.count, suggestions.Length);
                    if (countLimit > 0)
                    {
                        for (int i = 0; i < countLimit; i++)
                        {
                            term = term.createTerm(suggestions[i]);
                            result.add(token, suggestions[i], reader.docFreq(term));
                            haveSuggestions = true;
                        }
                    }
                }
                else
                {
                    if (suggestions.Length > 0)
                    {
                        List /*<String>*/ suggList = Arrays.asList(suggestions);
                        if (suggestions.Length > options.count)
                        {
                            suggList = suggList.subList(0, options.count);
                        }
                        result.add(token, suggList);
                        haveSuggestions = true;
                    }
                }
            }
            return(haveSuggestions? result : null);
        }