Archive

Archive for the ‘Programming’ Category

Convert a Walk In Closet Into an Office

November 15, 2010 Leave a comment

During the month of November I have been working full time to create a software title for Android and other mobile devices. My work started on the family computer, which was located in a central place. This worked OK, but I also found my productivity slipping. There was no clear delinieation between work and other times. If the children were crying then I also wasn’t working. I knew I had to do something if I was serious about publishing this software title, but I just didn’t think I had many options in my small townswomen.

My wife and I considered using one of the children’s rooms, but thought it was too much stress on them. I also looked into the possibility of renting a small office space, but that was an entirely different can of worms.  We did not want to pay for a separate set of utility bills not to mention the sheer inconvenience of being stuck in traffic going to and from the office. So we put our heads together and thought some more. Then we thought, what about the closet? It seemed big enough for the desk. We measured and it was. It also had space for books. (I like to have my references handy, saves time). We cleared everything out moved the computer in. It fit perfectly! Plus it had the advantage of being easy to partition so I could have peace and quiet for those times that needed more concentration.

Home office out of a walkin closet

Home office out of a walkin closet

Categories: Programming

Word Frequency Analysis Program

Recently I was browsing for interesting programming problems and stumbled across a virtual “ghost town” of programming projects called  “Programming Fun Challenge”  While the activity has appeared to have died down long ago, most of the problems are still relevant and fun to tackle. I decided to take on the Word Count Challenge where you determine the word frequency for 35 of Shakespeare’s Plays.

The original post is here: Christmas Programming Fun Challenge 9

I did find the link to the book to analyze to be out of date, here is the corrected link: Shakespeare’s First Folio by William Shakespeare

Basic Strategy

  • Read the entire file contents into meory
  • Use a regular expression to break the text into words (ignoring all punctuation except apostrophes)
  • Use a Hash Table (Dictionary<TKey, TValue> type) to keep track of each word and its frequency
    • If the word hasn’t been seen before, add it to the Hash with a count of 1
    • If the word has already been found, increment the count
  • Sort the Hash in descending order based on the frequency count

Most of this behavior was captured in two methods. Here is the code:

        public static Dictionary<string, int> AnalyzeWordFrequency(string textToAnalyze)
        {
            Dictionary<string, int> wordDistribution = new Dictionary<string,int>();
            Regex allWordsPattern = new Regex(@"[w']+");
            MatchCollection allWords = allWordsPattern.Matches(textToAnalyze.ToLower());
            foreach(Match word in allWords)
            {
                if (wordDistribution.ContainsKey(word.Value))
                {
                    wordDistribution[word.Value]++;
                }
                else
                {
                    wordDistribution.Add(word.Value, 1);
                }
            }

            return wordDistribution;
        }

        public static void PrintDistributionToFile(Dictionary<string, int> distribution, string fileName)
        {
            StreamWriter dataWriter = new StreamWriter(new FileStream(fileName, FileMode.Create));
            foreach(KeyValuePair<string, int> dataPoint in distribution.OrderByDescending(x => x.Value))
            {
                dataWriter.WriteLine(dataPoint.Key + " " + dataPoint.Value);
            }
            dataWriter.Close();
        }
50 Most Common Words Used by Shakespeare

Word Frequency Data Collected With Program

Categories: Programming