I'm new to C# and object-oriented programming in general. I have an application which parses a very large text file.
I have two dictionaries:
Dictionary<string, string> parsingDict //key: original value, value: replacement
Dictionary<int, string> Frequency // key: count, value: counted string
I am finding the frequency of each key. I am able to get the desired output which is:
System1 has been replaced with MachineA 5 time(s)
System2 has been replaced with MachineB 7 time(s)
System3 has been replaced with MachineC 10 time(s)
System4 has been replaced with MachineD 19 time(s)
Following is my code:
String[] arrayofLine = File.ReadAllLines(File);
foreach (var replacement in parsingDict.Keys)
{
for (int i = 0; i < arrayofLine.Length; i++)
{
if (arrayofLine[i].Contains(replacement))
{
countr++;
Frequency.Add(countr, Convert.ToString(replacement));
}
}
}
Frequency = Frequency.GroupBy(s => s.Value)
.Select(g => g.First())
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); //Get only the distinct records.
foreach (var freq in Frequency)
{
sbFreq.AppendLine(string.Format("The text {0} was replaced {2} time(s) with {1} \n",
freq.Value, parsingDict[freq.Value],
arrayofLine.Where(x => x.Contains(freq.Value)).Count()));
}
Using String[] arrayofLine = File.ReadAllLines(File);
increases memory utilization.
How can arrayofLine.Where(x => x.Contains(freq.Value)).Count()) be achieve using File.ReadLine as it is memory friendly.