public Process ( |
||
outputFile | The output file to write data to. | |
targetField | int | |
countPer | int | The desired count per class. |
Résultat | void |
public void Process(FileInfo outputFile, int targetField,
int countPer)
{
ValidateAnalyzed();
StreamWriter tw = PrepareOutputFile(outputFile);
_counts = new Dictionary<String, Int32>();
var csv = new ReadCSV(InputFilename.ToString(),
ExpectInputHeaders, InputFormat);
ResetStatus();
while (csv.Next() && !ShouldStop())
{
var row = new LoadedRow(csv);
UpdateStatus(false);
String key = row.Data[targetField];
int count;
if (!_counts.ContainsKey(key))
{
count = 0;
}
else
{
count = _counts[key];
}
if (count < countPer)
{
WriteRow(tw, row);
count++;
}
_counts[key] = count;
}
ReportDone(false);
csv.Close();
tw.Close();
}
}
public void TestBalanceCSVHeaders() { GenerateTestFile(true); var norm = new BalanceCSV(); norm.Analyze(InputName, true, CSVFormat.English); norm.Process(OutputName, 1, 2); var tr = new StreamReader(OutputName.ToString()); Assert.AreEqual("\"a\",\"b\"", tr.ReadLine()); Assert.AreEqual("one,1", tr.ReadLine()); Assert.AreEqual("two,1", tr.ReadLine()); Assert.AreEqual("four,2", tr.ReadLine()); Assert.AreEqual("five,2", tr.ReadLine()); Assert.AreEqual("six,3", tr.ReadLine()); Assert.AreEqual(2, norm.Counts["1"]); Assert.AreEqual(2, norm.Counts["2"]); Assert.AreEqual(1, norm.Counts["3"]); tr.Close(); InputName.Delete(); OutputName.Delete(); }