The goal of this code is to save a list of 250000 words with a number that indicates its frequency. The 250000 are sorted alphabetically but the frequency is unsorted. The idea is that a user must insert capitalized words, spaces or numbers after each word to lookup in the saved 250000-word list.
The max number of inputs are 1000 word+space+frequency pair per line.
The frequency domain is \$1 <= frequency <= 1000000\$. Every input must result in an output containing a list of the most relevant words \$>=\$ word-frequency entered and also ordered alphabetically in descending order.
How can I finish this code in a more efficient way?
public static void Main()
{
{
var dic = new Dictionary<string, int>();
int counter = 0;
StreamReader sr = new StreamReader("C:\\dicti.txt");
while (true)
{
string line = sr.ReadLine(); // To read lines
string[] ln;
if (line == null) break; // There is no more lines
try
{
ln = line.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
string a = ln[0];
int b = Convert.ToInt32(ln[1]);
dic.Add(a, b);
}
catch (IndexOutOfRangeException) { break; }
}
string[] ln2;
string am, word;
int bm;
do
{
//counter++;
do
{
word = Console.ReadLine();
ln2 = word.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
am = ln2[0];
bm = Convert.ToInt32(ln2[1]);
} while (!(am.Length >= 2 && bm >= 1 && bm <= 1000000));
if (true)
{
var aj = (dic.Where(x => x.Value >= bm)
.Where(x => x.Key.StartsWith(am))
.OrderByDescending(d => d.Value).Take(2));
foreach (var p in aj)
{
Console.WriteLine("{0} ", p.Key);
}
}
} while (counter < 1001);
}
}
while(true)
is not my style, but I understand it's purpose, butif(true)
?? \$\endgroup\$ – jb. Jan 7 '12 at 22:34