public static void SaveWordCloud(string csvFilePath, string savingFilePath) { var wordsAndFreqs = new List <Tuple <string, int> >(); using (var streamReader = new StreamReader(csvFilePath, Encoding.GetEncoding("gb2312"))) { string reading; while ((reading = streamReader.ReadLine()) != null) { var slices = reading.Split(','); wordsAndFreqs.Add(new Tuple <string, int>(slices[0], int.Parse(slices[1]))); } } wordsAndFreqs.Sort((lhs, rhs) => rhs.Item2.CompareTo(lhs.Item2)); var wordCloud = new WordCloud(1600, 900, allowVerical: true); var image = wordCloud.Draw(wordsAndFreqs.Select(item => item.Item1).ToList(), wordsAndFreqs.Select(item => item.Item2).ToList()); using (var stream = File.OpenWrite(savingFilePath)) { image.Save(stream, ImageFormat.Png); } }
public ActionResult Index(Website website) { if (website == null || string.IsNullOrEmpty(website.URL)) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized)); } try { var wcService = new WebParserService(); var wcList = wcService.Parse(website.URL); List <string> wordList = wcList.Select(m => m.WordText).ToList(); List <int> countList = wcList.Select(m => m.Count).ToList(); var wc = new WordCloud(800, 800); Image wcImage = wc.Draw(wordList, countList); byte[] imageByteData = imageToByteArray(wcImage); string imageBase64Data = Convert.ToBase64String(imageByteData); string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data); ViewBag.ImageData = imageDataURL; return(View()); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } }
public static void Show(WordCloud wordCloud, string keyword) { File.WriteAllText("WordClouds" + Path.DirectorySeparatorChar + "currentData.js", "var WordCloud = {keyword:\"" + keyword + "\", words:" + CreateWordCloud2JsArray(wordCloud.ToDictionary()) + "};"); BrowserService.OpenBrowser("WordClouds" + Path.DirectorySeparatorChar + "WordCloud.html"); }
private async void WordCloudPage_Load(object sender, EventArgs e) { pictureEdit1.Location = new Point((this.Width - pictureEdit1.Width) / 2, (this.Height - pictureEdit1.Height) / 2); var img = await Task.Run <Image>(new Func <Image>(() => { WordCloud wc = new WordCloud(this.Width, this.Height); return(wc.Draw(words, freqs)); })); pictureEdit1.Dock = DockStyle.Fill; pictureEdit1.Image = img; }
private void DrawWordcloud(int width, int height, Image mask) { this.wc = new WordCloud(width, height, mask: mask, allowVerical: true, fontname: "YouYuan"); this.wc.OnProgress += Wc_OnProgress; #if DEBUG this.wc.StepDrawMode = (this.flowLayoutPanelButtons.Controls["checkBoxStepDraw"] as CheckBox).Checked; this.wc.OnStepDrawResultImg += ShowResultImage; this.wc.OnStepDrawIntegralImg += ShowIntegralImage; #endif var i = this.wc.Draw(this.Words, this.Frequencies); Wc_OnProgress(1); ShowResultImage(i); }
static void Main(string[] args) { Console.WriteLine("Let's populate word cloud"); string sentence = "We came, we saw, we conquered...then we ate Bill's (Mille-Feuille) cake.'The bill came to five dollars.'"; WordCloud wordCloud = new WordCloud(sentence); var cloud = wordCloud.PopulateWordCloud(); foreach (var item in cloud) { Console.WriteLine($"{item.Key} appears {item.Value}"); } Console.ReadLine(); }
public bool drawwordcloud(List <string> Words, List <int> Frequencies, string path, string name) { String realpath = path + "\\" + name; this.wc = new WordCloud(1000, 800, mask: null, allowVerical: true, fontname: "YouYuan"); /* List<string> Words = new List<string>(); * * Words.Add("温钧盛"); * Words.Add("谢瑞鹏"); * Words.Add("贾康健"); * Words.Add("张一鸣"); * List<int> Frequencies = new List<int>(); * * Frequencies.Add(1); * Frequencies.Add(1); * Frequencies.Add(1); * Frequencies.Add(1);*/ Image image = wc.Draw(Words, Frequencies); bool a = SaveImage(image, realpath, "image/jpeg"); return(a); }
internal override async Task ExecuteJobAsync(CancellationToken stoppingToken) { try { var wordCloudParameter = Job.JobParameters.FirstOrDefault(jp => jp.Name.Equals("GenerateWordCloud", StringComparison.OrdinalIgnoreCase)); bool.TryParse(wordCloudParameter?.Value ?? "false", out bool generateWordCloud); HashSet <string> stopWords = null; var now = DateTime.UtcNow; var startOfThisMonth = new DateTime(now.Year, now.Month, 1, 0, 0, 0); var startOfLastMonth = startOfThisMonth.AddMonths(-1); if (generateWordCloud) { stopWords = new HashSet <string>((await BrobotService.GetStopWords()).Select(sw => sw.Word)); } string[] separatingStrings = { " ", "\t", "\n", "\r\n", ",", ":", ".", "!", "/", "\\", "%", "&", "?", "(", ")", "\"", "@" }; foreach (var channel in Job.Channels) { TimeSpan lastMonthOffset = TimeSpan.Zero, currentOffset = TimeSpan.Zero; if (!string.IsNullOrWhiteSpace(channel.PrimaryTimezone)) { var tzInfo = TZConvert.GetTimeZoneInfo(channel.PrimaryTimezone); lastMonthOffset = tzInfo.GetUtcOffset(startOfLastMonth); currentOffset = tzInfo.GetUtcOffset(startOfThisMonth); } var startDateTime = new DateTimeOffset(startOfLastMonth, lastMonthOffset); var endDateTime = new DateTimeOffset(startOfThisMonth, currentOffset); var discordChannel = DiscordClient.GetChannel(channel.ChannelId); if (!(discordChannel is SocketTextChannel socketTextChannel)) { continue; } var messageCount = new Dictionary <ulong, (string UserName, int MessageCount)>(); var words = new Dictionary <string, int>(); foreach (var user in discordChannel.Users) { messageCount.Add(user.Id, (UserName: user.Username, MessageCount: 0)); } foreach (var message in await socketTextChannel.GetMessagesAsync(startDateTime, endDateTime)) { if (messageCount.TryGetValue(message.Author.Id, out var count)) { count.MessageCount++; messageCount[message.Author.Id] = count; } if (generateWordCloud) { foreach (var word in message.Content.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries).Where(w => !stopWords.Contains(w, StringComparer.OrdinalIgnoreCase))) { if (!words.ContainsKey(word.ToLower())) { words.Add(word.ToLower(), 0); } words[word.ToLower()]++; } } } var builder = new StringBuilder(); foreach (var count in messageCount.Values.OrderByDescending(c => c.MessageCount)) { builder.AppendLine($"{count.UserName}: {count.MessageCount}"); } await socketTextChannel.SendMessageAsync(builder.ToString()); if (generateWordCloud) { try { var wc = new WordCloud(1280, 720); var frequencies = words .OrderByDescending(w => w.Value) .Take(100) .Select(w => new { Frequency = w.Value, Word = w.Key }); wc.Draw(frequencies.Select(f => f.Word).ToList(), frequencies.Select(f => f.Frequency).ToList()).Save(_wordcloudPath); await socketTextChannel.SendFileAsync(_wordcloudPath, string.Empty); File.Delete(_wordcloudPath); } catch (Exception ex) // if there was an error with the word cloud, don't kill the entire process { Logger.LogWarning(ex, "Failed to generate word cloud"); } } } } catch (ArgumentException argEx) { Logger.LogError(argEx, "Invalid argument, stopping job"); await StopAsync(); } catch (Exception ex) { Logger.LogError(ex, "Failed to execute stats job."); NumberOfFailures++; } }