Recently, I sharply wanted to know how often the word “coffee” is found in the public of my friend, as well as to build a beautiful cloud of words, based on the frequency of their appearance in posts.
If you are interested in how to do it literally on your knee, using C #, then please under the cat.
NoteBecause Since this application was written on my knees and the only task was to amuse my curiosity, it was decided to divide it into 2 stages: get the words and save to a file, clean out all the prepositions with it, and then build a cloud on the file.
For some more serious problem, use dictionaries of prepositions, endings, or any other alternatives.
Getting data from VK
First, install the appropriate
library . Create an application VK.
var services = new ServiceCollection(); var vkApi = new VkApi(services);
Let us log in, although this stage can be skipped, but then the number of available “walls” in VK will decrease dramatically.
vkApi.Authorize(new ApiAuthParams { AccessToken = " ",Settings = Settings.All});
or:
')
vkApi.Authorize(new ApiAuthParams { Login = "Login", Password = "Password", Settings = Settings.All });
We get the last 100 posts on the selected wall.
var posts=vkApi.Wall.Get(new WallGetParams { OwnerId = (long)Id,
From the existing collection will make one big line.
foreach (var post in posts.WallPosts) { if (!string.IsNullOrEmpty(post.Text)) data += post.Text; }
Next, you can clear the selected line of punctuation.
data =Regex.Replace(data,"\\!|\\?|\\(|\\)|\"|\\#|\\,|»|«|-" , string.Empty);
Well, we extract a collection of words.
var words = data.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries).ToList();
At the penultimate stage we will compile a dictionary with the frequency of words.
var wordsDictionary = new Dictionary<string, int>(); foreach (var word in words) { if (wordsDictionary.ContainsKey(word.ToLower())) wordsDictionary[word.ToLower()] += 1; else { wordsDictionary.Add(word.ToLower(),1); } }
At the end, sort it and, if desired, save it to a file.
wordsDictionary = wordsDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
Moving on to create a word cloud.
To do this, you must add a System.Drawing dependency to the project and this
package .
Add a dependency to our application.
using WordCloudGen = WordCloud.WordCloud;
And form the image.
var wc = new WordCloudGen(1024, 1024); wc.Draw(wordsDictionary.Keys.ToList(), wordsDictionary.Values.ToList()) .Save("cloudwords.jpg"); Console.WriteLine("pict create");
In conclusion, I would like to say thanks to the
worldbeater user and remind you that the library for VC has excellent support in
shopping center . By the way, the word "coffee" for 100 posts met 142 times.