This cromulent code should embiggen your knowledge.
public class Replacement
{
public string Word { get; set; }
public List<string> Replacements { get; set; }
}
If you want to process the file without reading it all in memory then you can do something like this
StreamReader sr = new StreamReader(@"c:\words.txt");
string word = string.Empty;
string lastWord = string.Empty;
string line = string.Empty;
char[] sep = new char[]{'|'};
Replacement r = null;
while ((line = sr.ReadLine()) != null)
{
string[] values = line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
if (values.Length == 2)
{
lastWord = word;
word = values[0];
string replacement = values[1];
if (word != lastWord)
{
Process(r);
r = new Replacement();
r.Replacements = new List<string>();
r.Word = word;
r.Replacements.Add(replacement);
}
else
{
r.Replacements.Add(replacement);
}
}
}
Process(r);
public static void Process(Replacement replacement)
{
if (replacement == null)
{
return;
}
Console.WriteLine("Possible replacements for {0}", replacement.Word);
int i = 0;
foreach(string r in replacement.Replacements)
{
Console.WriteLine("{0}: {1}", ++i, r);
}
}
If you want to just create the dictionary from the file but keep the whole dictionary in memory then
StreamReader sr = new StreamReader(@"c:\words.txt");
string word = string.Empty;
string lastWord = string.Empty;
string line = string.Empty;
char[] sep = new char[]{'|'};
Dictionary<string, Replacement> data = new Dictionary<string, Replacement>();
Replacement r = null;
while ((line = sr.ReadLine()) != null)
{
string[] values = line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
if (values.Length == 2)
{
lastWord = word;
word = values[0];
string replacement = values[1];
if (word != lastWord)
{
if (r != null)
{
data.Add(r.Word, r);
}
r = new Replacement();
r.Replacements = new List<string>();
r.Word = word;
r.Replacements.Add(replacement);
}
else
{
r.Replacements.Add(replacement);
}
}
}
data.Add(r.Word, r);
foreach (string key in data.Keys)
{
Process(data[key]);
}