Newer
Older
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
56
57
58
59
60
61
62
63
64
65
66
67
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}");
}
}
}
}
}