private void MainForm_Load(object sender, EventArgs e) { string filename = DICTIONARY_FOLDER + "/" + "quran-words.txt"; using (StreamReader reader = File.OpenText(filename)) { ResultLabel.Text = "Compiling dictionary ..."; ProgressBar.Minimum = 0; ProgressBar.Maximum = (int)reader.BaseStream.Length; ProgressBar.Value = 0; ListView_Resize(sender, e); try { String line; // Read and display lines from the file until the end of the file is reached. int linesRead = 0; Hashtable stringlists_by_bag = new Hashtable(); while ((line = reader.ReadLine()) != null) { line = line.ToLower(); Bag aBag = new Bag(line); if (!stringlists_by_bag.ContainsKey(aBag)) { strings l = new strings(); l.Add(line); stringlists_by_bag.Add(aBag, l); } else { strings l = (strings)stringlists_by_bag[aBag]; if (!l.Contains(line)) { l.Add(line); } } linesRead++; ProgressBar.Increment((line.Length + 2) * 2); // the +1 is for the line ending character, I'd guess. // the *2 is to deal with unicode characters Application.DoEvents(); } // Now convert the hash table, which isn't useful for // actually generating anagrams, into a list, which is. m_dictionary = new List <bag_and_anagrams>(); foreach (DictionaryEntry de in stringlists_by_bag) { m_dictionary.Add(new bag_and_anagrams((Bag)de.Key, (strings)de.Value)); } m_dictionary.Sort(); // Now just for amusement, sort the list so that the biggest bags // come first. This might make more interesting anagrams appear first. bag_and_anagrams[] sort_me = new bag_and_anagrams[m_dictionary.Count]; m_dictionary.CopyTo(sort_me); Array.Sort(sort_me); m_dictionary.Clear(); m_dictionary.InsertRange(0, sort_me); } catch (Exception ex) { throw new Exception("Dictionary: " + ex.Message); } ResultLabel.Text = "Ready."; ListView.Enabled = true; LettersTextBox.Enabled = true; LettersTextBox.Focus(); TypeUniqueLettersToolStripMenuItem_Click(sender, e); } //Bag.Test(); }
private void GenerateAnagrams(object sender, EventArgs e) { LettersTextBox.Enabled = false; Bag LettersTextBox_bag = new Bag(LettersTextBox.Text); ListView.Items.Clear(); TypeToolStripMenuItem.Enabled = false; RunToolStripMenuItem.Enabled = false; ViewToolStripMenuItem.Enabled = false; m_start_time = DateTime.Now; ElapsedTimeLabel.Text = "00:00:00"; Timer.Enabled = true; ProgressBar.Minimum = 0; ProgressBar.Value = 0; Anagrams.anagrams(LettersTextBox_bag, m_dictionary, 0, // bottom of main loop delegate() { ProgressBar.PerformStep(); Application.DoEvents(); }, // done pruning delegate(uint recursion_level, List <bag_and_anagrams> pruned_dict) { if (recursion_level == 0) { ProgressBar.Maximum = pruned_dict.Count; Application.DoEvents(); } }, // found a top-level anagram delegate(strings words) { string display_me = ""; foreach (string s in words) { if (display_me.Length > 0) { display_me += " "; } display_me += s; } ListView.Items.Add(display_me); ListView.EnsureVisible(ListView.Items.Count - 1); ResultLabel.Text = ListView.Items.Count.ToString() + " sentences"; if (ListView.Items.Count % 1000 == 0) { Application.DoEvents(); } }); Timer.Enabled = false; ResultLabel.Text = String.Format("{0} sentences.", ListView.Items.Count); if (ListView.Items.Count > 0) { ListView.EnsureVisible(0); } LettersTextBox.Enabled = true; LettersTextBox.Focus(); ListView.Columns[0].Text = "Click here to sort"; TypeToolStripMenuItem.Enabled = true; RunToolStripMenuItem.Enabled = true; ViewToolStripMenuItem.Enabled = true; SaveResults(); }
private void GenerateAnagrams() { this.Cursor = Cursors.WaitCursor; try { LettersTextBox.Enabled = false; ListView.Items.Clear(); ListView.Enabled = false; InitialsToolStripMenuItem.Enabled = false; RunToolStripMenuItem.Enabled = false; SaveToolStripMenuItem.Enabled = false; m_start_time = DateTime.Now; ElapsedTimeLabel.Text = "00:00:00"; Timer.Enabled = true; ProgressBar.Minimum = 0; ProgressBar.Value = 0; string filename = "Data" + "/" + "dictionary.txt"; if (File.Exists(filename)) { sentences = Anagrams.GenerateAnagrams(filename, LettersTextBox.Text); if (sentences != null) { ProgressBar.Minimum = 0; ProgressBar.Maximum = sentences.Count; for (int i = 0; i < sentences.Count; i++) { ListView.Items.Add(sentences[i]); ListView.EnsureVisible(ListView.Items.Count - 1); ResultLabel.Text = ListView.Items.Count.ToString() + " / " + sentences.Count; if (ListView.Items.Count % 1000 == 0) { Application.DoEvents(); } ProgressBar.Value = i + 1; } } ResultLabel.Text = String.Format("{0} sentences.", sentences.Count); if (ListView.Items.Count > 0) { ListView.EnsureVisible(0); } LettersTextBox.Enabled = true; LettersTextBox.Focus(); ListView.Enabled = true; if (ListView.Columns.Count > 0) { ListView.Columns[0].Text = "Click to sort"; } InitialsToolStripMenuItem.Enabled = true; RunToolStripMenuItem.Enabled = true; SaveToolStripMenuItem.Enabled = true; SaveResults(); Timer.Enabled = false; } } finally { this.Cursor = Cursors.Default; } }