Example #1
0
        public IActionResult WordCloud(WordCloudModel model)
        {
            try
            {
                string jsonStr = JsonConvert.SerializeObject(new
                {
                    Words = model.Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries),
                    Theme = model.Theme,
                    //FontName = "Samim"
                }
                                                             );

                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetJWTToken());
                var response = client.PostAsync($"{_urlPath}InformationRetrieval/WordCloudGeneration",
                                                new StringContent(jsonStr, Encoding.UTF8, "application/json")).Result;
                var bytesArray = response.Content.ReadAsByteArrayAsync().Result;
                using (var ms = new MemoryStream(bytesArray))
                {
                    Image  image    = Image.FromStream(ms);
                    string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".jpg";
                    image.Save(Path.Combine(_currentPath, "wordcloud", fileName));
                    return(PartialView("_WordCloudOutput", "~/wordcloud/" + fileName));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                ShowError("خطا در ترسیم ابر کلمات");
                return(new EmptyResult());
            }
        }
Example #2
0
        public IActionResult WordCloud(WordCloudModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.Text))
            {
                try
                {
                    string[] words = model.Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    // اگر به جای لیست کلمات متن وارد کرده بود
                    if (words.Length < 20 && words.Average(w => w.Length) > 20)
                    {
                        var model2 = new KeywordExtractionModel
                        {
                            MinWordLength       = 3,
                            MaxWordCount        = 3,
                            MinKeywordFrequency = 1,
                            ResultKeywordCount  = 500,
                            Method = "FNG",
                            Text   = model.Text
                        };

                        try
                        {
                            var apiOutput = CallApi($"{_urlPath}InformationRetrieval/KeywordExtraction", model2);
                            if (apiOutput.Item2)
                            {
                                var viewModel = JsonConvert.DeserializeObject <Dictionary <string, double> >(apiOutput.Item1);
                                words = viewModel.Keys.ToArray();
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex);
                            ShowError("خطا در استخراج کلمات کلیدی");
                            return(new EmptyResult());
                        }
                    }

                    string jsonStr = JsonConvert.SerializeObject(new
                    {
                        Words = words, model.Theme,     //FontName = "Samim"
                    }
                                                                 );

                    HttpClient client = new HttpClient();
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetJWTToken());
                    var response = client.PostAsync($"{_urlPath}InformationRetrieval/WordCloudGeneration",
                                                    new StringContent(jsonStr, Encoding.UTF8, "application/json")).Result;
                    var bytesArray = response.Content.ReadAsByteArrayAsync().Result;
                    using (var ms = new MemoryStream(bytesArray))
                    {
                        Image  image    = Image.FromStream(ms);
                        string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".jpg";
                        image.Save(Path.Combine(_currentPath, "wordcloud", fileName));
                        return(PartialView("_WordCloudOutput", "~/wordcloud/" + fileName));
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    ShowError("خطا در ترسیم ابر کلمات");
                }
            }
            return(new EmptyResult());
        }