Skip to content
Snippets Groups Projects
Program.cs 2.01 KiB
Newer Older
Vojtěch Moravec's avatar
Vojtěch Moravec committed
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HistoPreprocessor
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage HistPreprocessor.exe [directory] [output]");
                return;
            }

            DirectoryInfo inputFolder = new DirectoryInfo(args[0]);
            if (!inputFolder.Exists)
            {
                Console.WriteLine("Directory doesn't exist.");
                return;
            }

            string outputFile = args[1];
            Dictionary<int, long> values = new Dictionary<int, long>();

            long count = 0;
            foreach (FileInfo file in inputFolder.EnumerateFiles("*.histo"))
            {
                ++count;
                using (StreamReader reader = new StreamReader(file.FullName))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        string[] lineValues = line.Split(';');


                        int value = int.Parse(lineValues[0]);
                        int valueCount = int.Parse(lineValues[1]);

                        if (!values.ContainsKey(value))
                        {
                            values.Add(value, 0);
                        }
                        values[value] += valueCount;
                    }
                }
            }


            using (StreamWriter writer = new StreamWriter(outputFile, false))
            {
                foreach (var item in values.OrderBy(x => x.Key))
                {
                    double freq = (double)item.Value / (double)count;
                    string freqStr = Math.Round(freq, 3).ToString(CultureInfo.InvariantCulture);
                    writer.WriteLine($"{item.Key};{freqStr}");
                }
            }
        }
    }
}