/***************************************** ** File: CMSC104SurveyParser.cs ** Author: Tom Reich ** Date: 9/17/16 ** E-mail: reicht1@umbc.edu ** ** This file contains code that will parse the ** survey results submitted by students as part ** of Project 1A. It requires a single command ** line parameter: the path to the directory ** containing the survey result files. It ** outputs several CSV files to the current ** directory with summarized survey result counts. ** ** Other files required are ** N/A ** ***********************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.IO; namespace CMSC104SurveyParser { class CMSC104SurveyParser { private const string START_STR_MAJOR = "MAJOR:"; private const string START_STR_YEAR = "YEAR:"; private const string START_STR_OS = "PRIMARY OS"; private const string START_STR_CMD_LN_EXP = "COMMAND LINE EXPERIENCE LEVEL"; private const string START_STR_GEN_EXP = "GENERAL PROGRAMMING EXPERIENCE LEVEL"; private const string START_STR_C_EXP = "C EXPERIENCE LEVEL"; private const string START_STR_MOBILE = "MOBILE PLATFORM"; private const string START_STR_GAMING = "GAMING PLATFORM"; public static void Main(string[] args) { // Check for input directory parameter. if (args.Length < 1) { Console.WriteLine("Missing input directory parameter."); Environment.Exit(-1); } // Counter vars Dictionary majors = new Dictionary(); Dictionary years = new Dictionary(); Dictionary oses = new Dictionary(); Dictionary cmdLnExp = new Dictionary(); Dictionary genExp = new Dictionary(); Dictionary cExp = new Dictionary(); Dictionary mobile = new Dictionary(); Dictionary gaming = new Dictionary(); // Get the response counts. string[] filesToParse = Directory.GetFiles(args[0], "*.txt"); foreach(string fileName in filesToParse) { using (TextReader tw = File.OpenText(fileName)) { string line; while((line = tw.ReadLine()) != null) { line = line.ToUpper().Trim(); if (line.StartsWith(START_STR_MAJOR)) Increment(majors, ParseResponse(line, START_STR_MAJOR)); else if (line.StartsWith(START_STR_YEAR)) Increment(years, ParseResponse(line, START_STR_YEAR)); else if (line.StartsWith(START_STR_OS)) Increment(oses, ParseResponse(line, START_STR_OS)); else if (line.StartsWith(START_STR_CMD_LN_EXP)) Increment(cmdLnExp, ParseResponse(line, START_STR_CMD_LN_EXP)); else if (line.StartsWith(START_STR_GEN_EXP)) Increment(genExp, ParseResponse(line, START_STR_GEN_EXP)); else if (line.StartsWith(START_STR_C_EXP)) Increment(cExp, ParseResponse(line, START_STR_C_EXP)); else if (line.StartsWith(START_STR_MOBILE)) Increment(mobile, ParseResponse(line, START_STR_MOBILE)); else if (line.StartsWith(START_STR_GAMING)) Increment(gaming, ParseResponse(line, START_STR_GAMING)); } } } // Write the response counts. WriteResponseCounts(majors, "majors.csv"); WriteResponseCounts(years, "years.csv"); WriteResponseCounts(oses, "OSes.csv"); WriteResponseCounts(cmdLnExp, "CommandLineExperience.csv"); WriteResponseCounts(genExp, "GeneralExperience.csv"); WriteResponseCounts(cExp, "CExperience.csv"); WriteResponseCounts(mobile, "MobilePlatforms.csv"); WriteResponseCounts(gaming, "GamingPlatforms.csv"); } private static string ParseResponse(string line, string startsWith) { return Regex.Replace(line.Replace(startsWith, string.Empty), "[^A-Z0-9]", string.Empty); } private static void Increment(Dictionary count, string key) { if(!count.ContainsKey(key)) count.Add(key, 1); else count[key] = count[key] + 1; } private static void WriteResponseCounts(Dictionary counts, string outFileName) { using (TextWriter tw = File.CreateText(outFileName)) counts.ToList().ForEach(x => tw.WriteLine(x.Key + "," + x.Value)); } } }