Word Cloud (Tag Cloud) Generator Control for .NET Windows.Forms in c#
Background
This control is inspired by the free web based word cloud generator called Wordle.
In fact the control is a screw-out product of my project at http://sourcecodecloud.codeplex.com .
I really loved visualizations produced by Wordle, but my goal was to write a non web based local solution to process large amount of sensible data. There where number of components I found on the web, but most of them had either very pure performance when processing text and the visualization or layout was not that I expected.
Architecture and usage
There are 4 phases when visualizing the word cloud:
Processing data like text, HTML or source code and extracting relevant words, while ignoring others. As an example I have implemented two of them TextExtractor
extracts all words from some text string ignoring spaces and all non letter characters. Another one UriExtractor
fetches a URL content and tries to clean away HTML tags and Javascript. To be honest I just implemented as a showcase and its filtering capabilities are very pure.
To tap your own data source just implement IWordExtractor
interface.
public interface IWordExtractor
{
IEnumerable<string> GetWords();
}
Counting words and ignoring ones from blacklist.
The result is an enumeration with pairs of words and integers representing number of occurrences of this word in a text. You can sort this sequence either by weight (number of occurrences) or alphabetically and pass to control to visualize them.
IBlacklist blacklist = new CommonBlacklist(new[] {"the", "and", "to", "for", "out"});
WordCounter counter = new WordCounter(blacklist);
progress = new ProgressBarWrapper(progressBar);
extractor = new StringExtractor(textBox.Text, progress);
wordRegistry = counter.Count(extractor);
KeyValuePair<string, int>[] pairs = wordRegistry.GetSortedByOccurances();
cloudControl.WeightedWords = pairs;
Layouting – I use a QuadTree
data structure to create a non overlapping map of words on controls graphics. The same data structure is also used to query control which words are under certain rectangular area or point. This query is used to redraw only particular area when needed or perform some action when control was clicked. Thereby it is very useful to know which word was clicked to perform a word related action, let’s say show statistics or navigate to some URL.
private void cloudControl_Click(object sender, EventArgs e)
{
LayoutItem itemUderMouse;
Point mousePositionRelativeToControl =
cloudControl.PointToClient(new Point(MousePosition.X, MousePosition.Y));
if (!cloudControl.TryGetItemAtLocation(mousePositionRelativeToControl, out itemUderMouse))
{
return;
}
MessageBox.Show(itemUderMouse.Word);
}
Configuring the Word Cloud Control
There are several things you may very on this control:
You can change font type and size.
cloudControl.MinFontSize = 6;
cloudControl.MaxFontSize = 60;
cloudControl.Font = new Font(new FontFamily("Verdana"), 8, FontStyle.Regular);
Use different colours.
cloudControl.Palette = new Brush[] {Brushes.DarkRed, Brushes.Red, Brushes.LightPink};
Use different layout. Currently there are two layouts implemented. You can implement your own one by deriving from BaseLayout
or just by implementing ILayout
interface on your own.
cloudControl.LayoutType = LayoutType.Typewriter;
The logic of layouting and drawing graphics is strictly separated by IGraphicEngine
interface. So I think it would not be a big deal to port it to WPF or Silverlight in the future.
Credits
Thanks to Michael Coyle for the great article A Simple QuadTree Implementation in C# http://www.codeproject.com/KB/recipes/QuadTree.aspx
Thanks to Jonathan Feinberg, creator of Wordle for that beautiful cloud and hints about algorithms behind http://stackoverflow.com/questions/342687/algorithm-to-implement-something-like-wordle
Post Comment
zq0L4M Really appreciate you sharing this blog article.Really thank you!
udlFDw This is one awesome blog post. Keep writing.
IAOdFM Spot on with this write-up, I absolutely feel this site needs a lot more attention. I all probably be returning to read more, thanks for the advice!
kKLONa This page truly has all the info I wanted concerning this subject and didn at know who to ask.
Pretty! This was an incredibly wonderful article. Many thanks for providing this info.
vOZTnu Thank you for your blog post.Really thank you! Want more.
eZIWgG
1svkcJ Major thanks for the post.Really looking forward to read more. Awesome.
GJqRuj Very nice post, thanks so much for sharing. Do you happen to have an RSS feed I can subscribe to?
hp5o4j You have made some really good points there. I checked on the net for additional information about the issue and found most individuals will go along with your views on this web site.
wIwKA9 This is one awesome article.Really thank you! Will read on...
reOiGM very handful of web-sites that occur to become in depth beneath, from our point of view are undoubtedly effectively worth checking out
m8osAb Wow! Thank you! I always wanted to write on my blog something like that. Can I implement a portion of your post to my blog?
tD34sm Wow, great article post.Thanks Again. Great.
MR6pys I'm curious to find out what blog platform you're using? I'm having some small security problems with my latest site and I'd like to find something more secure. Do you have any solutions?
Lca5zM It's laborious to seek out knowledgeable individuals on this topic, however you sound like you recognize what you're speaking about! Thanks
54VjRu Thank you for the good writeup. It actually was once a amusement account it. Glance complicated to far added agreeable from you! However, how could we keep up a correspondence?
TojYLm I am curious to find out what blog platform you're utilizing? I'm experiencing some minor security problems with my latest blog and I'd like to find something more safe. Do you have any solutions?
7yWzXh Im obliged for the blog post.Really thank you! Great.
l5tR5N Thank you for your blog post.Much thanks again. Will read on...
El5ORX I think this is a real great post. Really Cool.
Gb8iyP Major thanks for the blog article.Much thanks again. Will read on...
qoNJII Im grateful for the blog.Much thanks again. Great.
rkgEGJ Looking forward to reading more. Great blog post.Really thank you! Cool.
oiUl3D Thanks for sharing, this is a fantastic blog post.Thanks Again. Cool.
EbvypK I am so grateful for your blog post.Thanks Again. Want more.
FLPEii Fantastic blog post.Really looking forward to read more.
1OaPNg Fantastic blog article.Much thanks again. Fantastic.
gH382I I really enjoy the article.Really thank you! Want more.
aX7vPl A big thank you for your blog article.Thanks Again.
F8mIL6 Thanks a lot for the blog article.Much thanks again. Awesome.
noBZTw Thanks a lot for the blog.Really looking forward to read more. Want more.
3T0MdW Im grateful for the article post.Thanks Again. Keep writing.
8yvozN Thanks again for the blog post. Much obliged.