public Image getImageWordmap(Dictionary <string, int> wordCount, int width, int height) { //List to show wordmap List <string> wordslist = new List <string>(); List <int> frequencylist = new List <int>(); var items = from pair in wordCount orderby pair.Value descending select pair; //add sports words and frequency to the list foreach (var kvp in items) { wordslist.Add(kvp.Key); frequencylist.Add(kvp.Value); } //create word cloud generation var wc = new WordCloudGen(width, height); // display wordmap image of sports news Image wordmap = wc.Draw(wordslist, frequencylist); return(wordmap); }
public static void GenerateWordCloud(List <KeyValuePair <string, int> > sortedList) { /* creating a WordCloud image */ List <string> words = new List <string>(); List <int> freq = new List <int>(); // seperating the keyvalue pairs into the correct format of two lists foreach (KeyValuePair <string, int> e in sortedList) { words.Add(e.Key); } foreach (KeyValuePair <string, int> f in sortedList) { freq.Add(f.Value); } // 500, 500 refers to the image quality, true gives size based on freq, null gives words random colors, -1, 1 are step sizes var wc = new WordCloudGen(500, 500, true, null, -1, 1); // save image to solution's image folder wc.Draw(words, freq).Save(@"c:\users\nickt\source\repos\testai\testai\Images\wordcloud.bmp"); Console.WriteLine("picture saved as wordcloud.bmp"); foreach (KeyValuePair <string, int> test in sortedList) { Console.WriteLine(test); } }
public void CommentInfoCommand(MessageInfo message, string[] args) { if (args.Length != 4 && args.Length != 5) { _service.SendToGroup(message.GroupNumber, "使用方法: !直播 评论 <Vtuber名字> <序号> [复读排序]"); return; } var vtuber = Config.DefaultConfig.GetVtuber(args[2]); if (vtuber == null) { _service.SendToGroup(message.GroupNumber, "数据库中不存在" + args[2]); return; } var history = _liveCollection.FindAsync(doc => doc.Channel == vtuber.YoutubeChannelId).GetAwaiter().GetResult().ToList(); if (!history.Any()) { _service.SendToGroup(message.GroupNumber, "未找到任何记录,请等待下次直播,机器人将自动记录"); return; } _service.SendToGroup(message.GroupNumber, "正在查询.."); var info = history[int.Parse(args[3]) - 1]; var comments = _chatCollection.FindAsync(doc => doc.VideoId == info.VideoId).GetAwaiter().GetResult().ToList(); var dic = new Dictionary <string, int>(); foreach (var comment in comments) { if (!dic.ContainsKey(comment.DisplayMessage)) { dic.Add(comment.DisplayMessage, 0); } dic[comment.DisplayMessage]++; } var msg = string.Empty; if (args.Length == 5 && args.Last() == "复读") { var dicDesc = dic.OrderByDescending(v => v.Value).ToDictionary(key => key.Key, value => value.Value); var wordCloud = new WordCloud.WordCloud(1920, 1080); var image = wordCloud.Draw(dicDesc.Keys.Take(30).ToList(), dicDesc.Values.Take(30).ToList()); _service.SendImageToGroup(message.GroupNumber, image); return; } var count = comments.Where(v => v.TextMessageDetails?.HasValues ?? false).Count(v => v.TextMessageDetails["messageText"].ToObject <string>().ChineseRatio() > 80 && v.DisplayMessage.Length >= 2 && v.DisplayMessage.ToCharArray() .All(c => !c.IsHinaganaOrKatakana() && c.IsSimplifiedChinese())); msg = $"关于 {vtuber.OriginalName} 直播的评论分析:" + $"\r\n标题: {info.Title}" + $"\r\n评论总数: {comments.Count}" + $"\r\n平均每分钟评论数: {(comments.Count / (info.EndTime - info.BeginTime).TotalMinutes):F}" + $"\r\n最多复读的词: {dic.First(v => v.Value == dic.Max(d => d.Value)).Key} (复读 {dic.Max(v => v.Value)} 次)" + $"\r\n疑似大陆天狗评论数: {count} (估算)"; _service.SendToGroup(message.GroupNumber, msg); }
private async void Button1_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } _query = textBox1.Text; var result = ""; var client = new HttpClient(); var uri = Host + "cx=" + Cx + "&key=" + Key + "&num=" + Num + "&start=" + Start + "&q=" + System.Net.WebUtility.UrlEncode(_query); var response = await client.GetAsync(uri); var contentString = await response.Content.ReadAsStringAsync(); dynamic parsedJson = JsonConvert.DeserializeObject(contentString); var items = parsedJson?.items; for (var i = Start; i < Num; i++) { result += items?[i].snippet.ToString(); } var extractor = new TfidfExtractor(); var pairs = extractor.ExtractTagsWithWeight(result, 30); var words = new List <string>(); var freqs = new List <int>(); foreach (var pair in pairs) { if (pair.Word.Equals("...")) { continue; } words.Add(pair.Word); freqs.Add(Convert.ToInt32(pair.Weight * Math.Pow(10, 6))); } var wc = new WordCloud.WordCloud(1920, 1080); var image = wc.Draw(words, freqs); pictureBox1.Image = image; button2.Enabled = true; button2.Visible = true; }
private void button1_Click(object sender, EventArgs e) { var s = new Stopwatch(); s.Start(); var wc = new WordCloudGen(1000, 600); if (resultPictureBox.Image != null) { resultPictureBox.Image.Dispose(); } Image i = wc.Draw(Words, Frequencies); s.Stop(); elapsedLabel.Text = s.Elapsed.TotalMilliseconds.ToString(); resultPictureBox.Image = i; }
public void createWordCloud() { List <string> listTagName = new List <string>(); List <int> listFreqs = new List <int>(); List <Tuple <TAG, int> > listNumNotePerTag = new List <Tuple <TAG, int> >(); listNumNotePerTag = xlTag.statisticNumberNotePerTag(); int maxCount = listNumNotePerTag.Max(t => t.Item2); maxCount++; listNumNotePerTag.Sort(delegate(Tuple <TAG, int> tuple1, Tuple <TAG, int> tuple2) { return(tuple2.Item2.CompareTo(tuple1.Item2)); }); foreach (var entry in listNumNotePerTag) { listTagName.Add(entry.Item1.mContent); listFreqs.Add(maxCount - entry.Item2); } var wc = new WordCloudGen(400, 400); System.Drawing.Image wordCloudImage = wc.Draw(listTagName, listFreqs); // Đổi từ kiểu dữ liệu Drawing.Image sang ImageSource ==> bi BitmapImage bi = new BitmapImage(); bi.BeginInit(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); wordCloudImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Seek(0, System.IO.SeekOrigin.Begin); bi.StreamSource = ms; bi.EndInit(); // Đặt hình mới được tạo vào (Đây là hình Word Cloud) wordCloud.Source = bi; }
private void button3_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; string filePath = Path.Combine(Directory.GetCurrentDirectory(), "sportsNews.txt"); string sportsText = File.ReadAllText(filePath); wordmap wm = new wordmap(); List <string> prepVerbList = wm.GetprepVerbList(); label2.Text = "loading preposition and verb files ..."; sportsText = wm.normalize(sportsText); label2.Text = "normalizing text ..."; string[] words = wm.tokenize(sportsText); label2.Text = "tokenizing text ..."; Dictionary <string, int> SportswordCount = wm.CalcWordCount(words); label2.Text = "lemmatizing and counting words frequencies ..."; SportswordCount = wm.deletePrepVerb(prepVerbList, SportswordCount); label2.Text = "deleting preposition and verb from words ..."; filePath = Path.Combine(Directory.GetCurrentDirectory(), "politicsNews .txt"); string politicsText = File.ReadAllText(filePath); politicsText = wm.normalize(politicsText); label2.Text = "normalizing text ..."; words = wm.tokenize(politicsText); label2.Text = "tokenizing text ..."; Dictionary <string, int> PoliticswordCount = wm.CalcWordCount(words); label2.Text = "lemmatizing and counting words frequencies ..."; PoliticswordCount = wm.deletePrepVerb(prepVerbList, PoliticswordCount); label2.Text = "deleting preposition and verb from words ..."; decimal totalsportsWordCount = (decimal)SportswordCount.Sum(x => x.Value); decimal totalpoliticsWordCount = (decimal)PoliticswordCount.Sum(x => x.Value); List <string> AllWord = new List <string>(); List <string> sportsWordList = new List <string>(); List <string> politicsWordList = new List <string>(); sportsWordList = SportswordCount.Keys.ToList(); politicsWordList = PoliticswordCount.Keys.ToList(); AllWord = sportsWordList.Union(politicsWordList).ToList(); DataTable Allword = new DataTable(); Allword.Columns.Add("word", typeof(string)); Allword.Columns.Add("SportsCount", typeof(decimal)); Allword.Columns.Add("PoliticsCount", typeof(decimal)); Allword.Columns.Add("Distance", typeof(decimal)); foreach (string word in AllWord) { decimal sportValue; decimal politicsValue; if (SportswordCount.ContainsKey(word)) { sportValue = (decimal)SportswordCount[word]; } else { sportValue = 0; } if (PoliticswordCount.ContainsKey(word)) { politicsValue = (decimal)PoliticswordCount[word]; } else { politicsValue = 0; } decimal distance = (sportValue / totalsportsWordCount) - (politicsValue / totalpoliticsWordCount); Allword.Rows.Add(word, sportValue, politicsValue, distance); } dataGridView1.Rows.Clear(); dataGridView1.Columns.Clear(); Allword.DefaultView.Sort = "Distance DESC"; Allword = Allword.DefaultView.ToTable(); dataGridView1.DataSource = Allword; List <string> wordslist = new List <string>(); List <int> frequencylist = new List <int>(); for (int i = 0; i < Allword.Rows.Count; i++) { wordslist.Add(Allword.Rows[i]["word"].ToString()); frequencylist.Add((int)(Math.Abs(Convert.ToDecimal(Allword.Rows[i]["Distance"].ToString()) * 10000))); } //create word cloud generation var wc = new WordCloudGen(pictureBox1.Width, pictureBox1.Height); // display wordmap image of sports news Image newImage = wc.Draw(wordslist, frequencylist); pictureBox1.Image = newImage; label2.Text = "done"; }
public void ChartBlocksTest() { var wc = new WordCloud.WordCloud(1920, 1080); }
private void button1_Click(object sender, EventArgs e) { var s = new Stopwatch(); s.Start(); var wc = new WordCloudGen(1000, 600); if (resultPictureBox.Image != null) { resultPictureBox.Image.Dispose(); } Words.Clear(); Frequencies.Clear(); List <string> AllWords = new List <string>(); List <int> AllFrequencies = new List <int>(); var file_words = File.ReadAllText(@"D:\c#\图云词频计算\words.txt"); var words = file_words.Split('\n'); foreach (var word in words) { if (AllWords.Contains(word)) { //如果已经存在就+1 AllFrequencies[AllWords.IndexOf(word)]++; } else { bool result = false; for (int j = 0; j < word.Length; j++) { if (Char.IsNumber(word, j)) { result = true; } } if (!result) { //如果不存在 且不为数字就添加 AllWords.Add(word); AllFrequencies.Add(1); } } } int index = 0; foreach (var temp in AllFrequencies) { if (temp > 200) { Words.Add(AllWords[index]); Frequencies.Add(temp); } index++; } Image i = wc.Draw(Words, Frequencies); s.Stop(); elapsedLabel.Text = s.Elapsed.TotalMilliseconds.ToString(); resultPictureBox.Image = i; }