Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / Windows Forms

Word Cloud (Tag Cloud) Generator Control for .NET Windows.Forms in C#

Rate me:
Please Sign up or sign in to vote.
4.91/5 (47 votes)
30 Jul 2011CPOL3 min read 217.3K   13.1K   126  
Generate a word cloud form some input text. A word cloud is a randomly arranged set of words used in your text. The size and the color of each word expresses its usage frequency. Rarely used words are small and pale. The control is clickable and allows to identify a word under mouse.
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Gma.CodeCloud.Controls.TextAnalyses.Processing
{
    public struct WordGroup : IWord, IEnumerable<IWord>
    {
        private readonly IEnumerable<IWord> m_AssociatedWords;

        public WordGroup(string stem, IEnumerable<IWord> associatedWords)
            : this()
        {
            this.Stem = stem;
            this.m_AssociatedWords = associatedWords;
            this.Occurrences = this.m_AssociatedWords.Sum(word => word.Occurrences);
            this.Text = this.m_AssociatedWords.Max().Text;
        }

        public string Stem { get; set; }

        public string Text { get; private set; }

        public int Occurrences { get; private set; }

        public int CompareTo(IWord other)
        {
            return this.Occurrences - other.Occurrences;
        }

        public IEnumerator<IWord> GetEnumerator()
        {
            return m_AssociatedWords.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public string GetCaption()
        {
            string caption =  string.Empty;
            return
                this
                .OrderByDescending(
                    word => word.Occurrences)
                .Aggregate(
                    caption, 
                    (s, word) => string.Format("{0}\r\n{1}\t{2}", s, word.Text, word.Occurrences));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Comments and Discussions