Question : Console application for top 10 most frequently occuring words in a document

Hi,

I am trying to create a console aplication that will allow the user to enter in the path to a text file.  The application should then determine the 10 most commonly occuring words.  
Can anyone help?  I have never written a console application before. C#.

Answer : Console application for top 10 most frequently occuring words in a document

Hi,
   I have used regular expression, a powerful feature  to manipulate text.The code to find maximum occurrence of word of given file is as follows:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO; 
using System.Text.RegularExpressions;

namespace tstRegex
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter File Name with Extension :");  
            string sFileName = Console.ReadLine();
            Console.Write("Enter File Path :");
            string sPath = Console.ReadLine();
            Console.WriteLine();
            FileInfo fi = new FileInfo(Path.Combine (sPath,sFileName));
            if (!fi.Exists)
            {
                Console.WriteLine("No input file found");
                Console.ReadLine();
                return;
            }
            SortedDictionary occurrences = new SortedDictionary();
            using (StreamReader reader = new StreamReader(fi.FullName))
            {
                string line = reader.ReadToEnd();
                //the word is splited with white space,comma,dot.
                //you can add any other delimeter if needed
                string[] words = Regex.Split(line, @"[ .,\s]");
                foreach (string word in words)
                {
                    if (occurrences.ContainsKey(word.ToLower()) || string.IsNullOrEmpty(word)) 
                        continue;
                    string pattern = string.Format("{0}{1}{0}", @"\b", word); //to get whole word matched
                    MatchCollection col = Regex.Matches(line, pattern, RegexOptions.IgnoreCase);
                    occurrences.Add(word.ToLower(), col.Count);
                }
            }
             
            var results = occurrences.OrderByDescending(x => x.Value);
            int i = 1;
            foreach (KeyValuePair result in results )
            {
                Console.WriteLine("{0} Occurs {1} times ",result.Key,result.Value);
                if (i++ ==3 ) break;

            }
            Console.ReadLine();
        }
    }
}
Random Solutions  
 
programming4us programming4us