示例#1
0
        public string GetJsonParsedModel(WordCounterResultViewModel inputModel)
        {
            var formatModel = new FriendlyJsonFormatModel();

            formatModel.ExternalSourceUrl = inputModel.ExternalSourceUrl;
            formatModel.ResponseStatus    = inputModel.ResponseStatus.ToString();
            formatModel.ResponseStatusMsg = inputModel.ResponseStatusMsg;
            if (inputModel.WordsFrequencies != null)
            {
                formatModel.WordsFrequencies = inputModel.WordsFrequencies.Select(c => new DictEntry {
                    Key = c.Key, Value = c.Value
                }).ToList();
            }

            if (inputModel.MetaKeywordsFrequencies != null)
            {
                formatModel.MetaKeywordsFrequencies = inputModel.MetaKeywordsFrequencies.Select(c => new DictEntry {
                    Key = c.Key, Value = c.Value
                }).ToList();
            }

            if (inputModel.ExternalLinks != null)
            {
                formatModel.ExternalLinks = inputModel.ExternalLinks.Select(c => new DictEntry {
                    Key = c
                }).ToList();
            }

            var jsonResult = JsonConvert.SerializeObject(formatModel);

            return(jsonResult);
        }
        public async Task <ActionResult> CountWords(WordCounterViewModel model)
        {
            var result = new WordCounterResultViewModel();

            result.ResponseStatus    = Status.NotSpecified;
            result.ResponseStatusMsg = string.Empty;
            if (!string.IsNullOrEmpty(model.Text))
            {
                result.WordsFrequencies = wordService.GetWordsFrequenciesFromText(model.Text);
            }
            else if (!string.IsNullOrEmpty(model.ExternlLink))
            {
                result.ExternalSourceUrl = model.ExternlLink;
                var source = await wordService.GetRemoteData(model.ExternlLink);

                result.ResponseStatus    = source.Status;
                result.ResponseStatusMsg = source.StatusMsg;
                if (result.ResponseStatus == Status.Success)
                {
                    result.WordsFrequencies = wordService.GetWordsFrequenciesFromExternalSource(source.Document);
                    if (model.CountWordsInMeta)
                    {
                        result.MetaKeywordsFrequencies = wordService.GetMetaKeywordsFrequencies(source.Document, result.WordsFrequencies);
                    }

                    if (model.IncludeExternalLinks)
                    {
                        result.ExternalLinks = wordService.GetExternalLinks(model.ExternlLink, source.Document);
                    }
                }
            }

            var jsonResult = this.responseFormatHelper.GetJsonParsedModel(result);

            return(this.Json(jsonResult));
        }