/// <summary>
        /// Getting the cost and array containing compute values as matrix.
        /// </summary>
        /// <param name="stringsModel">The object containing first and second strings whose distance to be calculated.</param>
        /// <returns></returns>
        public async Task <LevenshteinDistanceMatrixAndCostResult> GetLevenshteinDistanceMatrixAndCostResult(StringsModel stringsModel, string JWTToken)
        {
            LevenshteinDistanceMatrixAndCostResult levenshteinDistanceMatrixAndCostResult = null;
            HttpResponseMessage httpResponseMessage = null;
            Uri           resourcePath  = null;
            string        requestUri    = null;
            string        baseAddress   = null;
            StringBuilder stringBuilder = null;

            try
            {
                baseAddress   = configuration.GetValue <string>("MySettings:ApiBaseAddress");
                resourcePath  = _levenshteinDistanceUri;
                stringBuilder = new StringBuilder();
                stringBuilder.Append(baseAddress);
                stringBuilder.Append(resourcePath.ToString());
                requestUri = stringBuilder.ToString();
                //
                using (var httpclient = new HttpClient())
                {
                    httpclient.DefaultRequestHeaders.Add("Authorization", "Bearer " + JWTToken);
                    httpResponseMessage = await httpclient.PostAsJsonAsync(requestUri, stringsModel);

                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        levenshteinDistanceMatrixAndCostResult = httpResponseMessage.Content.ReadAsAsync <LevenshteinDistanceMatrixAndCostResult>().Result;
                    }
                }
            }
            catch (Exception exception)
            {
                throw (exception);
            }
            finally
            {
                httpResponseMessage = null;
                resourcePath        = null;
                baseAddress         = null;
                requestUri          = null;
                stringBuilder       = null;
            }
            return(levenshteinDistanceMatrixAndCostResult);
        }
        public IActionResult ComputeLevenshteinDistance(StringsModel stringsModel)
        {
            if (stringsModel == null)
            {
                return(BadRequest("Invalid argument(s)."));
            }
            //
            if (string.IsNullOrEmpty(stringsModel.FirstString) || string.IsNullOrEmpty(stringsModel.SecondString))
            {
                return(BadRequest("Invalid argument(s)."));
            }
            //
            int           n        = stringsModel.FirstString.Length;
            int           m        = stringsModel.SecondString.Length;
            IActionResult response = Unauthorized();
            LevenshteinDistanceMatrixAndCostResult levenshteinDistanceMatrixAndCostResult = StringDistance.GetLevenshteinDistanceMatrixAndCost(stringsModel.FirstString, stringsModel.SecondString);

            response = Ok(levenshteinDistanceMatrixAndCostResult);
            return(response);
        }
Beispiel #3
0
        public async Task <IActionResult> GetLevenshteinDistanceResult(StringsViewModel stringsViewModel)
        {
            LevenshteinDistanceViewModel           levenshteinDistanceViewModel           = null;
            LevenshteinDistanceMatrixAndCostResult levenshteinDistanceMatrixAndCostResult = null;
            StringsModel stringsModel = null;
            string       JWTToken     = null;

            if (stringsViewModel != null)
            {
                stringsModel = new StringsModel {
                    FirstString    = stringsViewModel.FirstString
                    , SecondString = stringsViewModel.SecondString
                };
                JWTToken             = HttpContext.Session.GetString(accessTokenSession).ToString();
                ViewData["JWTToken"] = HttpContext.Session.GetString(accessTokenSession);
                using (LevenshteinDistanceApiClient levenshteinDistanceApiClient = new LevenshteinDistanceApiClient(this.configuration))
                {
                    levenshteinDistanceMatrixAndCostResult = await levenshteinDistanceApiClient.GetLevenshteinDistanceMatrixAndCostResult(stringsModel, JWTToken);
                }
                if (levenshteinDistanceMatrixAndCostResult != null)
                {
                    levenshteinDistanceViewModel = new LevenshteinDistanceViewModel
                    {
                        StringsAsCriteria = new StringsViewModel
                        {
                            FirstString    = levenshteinDistanceMatrixAndCostResult.StringsAsCriteria.FirstString
                            , SecondString = levenshteinDistanceMatrixAndCostResult.StringsAsCriteria.SecondString
                        },
                        ResultTable = new LevenshteinResultTableViewModel {
                            Cost           = levenshteinDistanceMatrixAndCostResult.Cost
                            , ResultMatrix = levenshteinDistanceMatrixAndCostResult.MatrixResult
                        }
                    };
                }
            }
            return(View("Index", levenshteinDistanceViewModel));
        }