Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        private void Form1_Shown(object sender, EventArgs e)
        {
            System.IO.Stream           wordlist_stream;
            System.Reflection.Assembly thisExe;
            thisExe         = System.Reflection.Assembly.GetExecutingAssembly();
            wordlist_stream =
                thisExe.GetManifestResourceStream("Anagrams.words");
            System.Diagnostics.Trace.Assert(wordlist_stream != null,
                                            "Uh oh, can't find word list inside myself!");
            toolStripStatusLabel1.Text = "Compiling dictionary ...";
            ProgressBar.Value          = 0;
            ProgressBar.Maximum        = (int)wordlist_stream.Length;
            listView1_Resize(sender, e);
            using (StreamReader sr = new StreamReader(wordlist_stream))
            {
                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 = sr.ReadLine()) != null)
                {
                    // TODO -- filter out nonletters.  Thus "god's"
                    // should become "gods".  And since both of those
                    // are likely to appear, we need to ensure that we
                    // only store one.
                    line = line.ToLower();
                    if (!acceptable(line))
                    {
                        continue;
                    }
                    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 + 1); // the +1 is for the line ending character, I'd guess.

                    Application.DoEvents();
                }

                // Now convert the hash table, which isn't useful for
                // actually generating anagrams, into a list, which is.

                dictionary = new List <bag_and_anagrams>();
                foreach (DictionaryEntry de in stringlists_by_bag)
                {
                    dictionary.Add(new bag_and_anagrams((Bag)de.Key, (strings)de.Value));
                }

                // 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[dictionary.Count];
                dictionary.CopyTo(sort_me);
                Array.Sort(sort_me);
                dictionary.Clear();
                dictionary.InsertRange(0, sort_me);
            }
            toolStripStatusLabel1.Text = "Compiling dictionary ... done.";
            listView1.Enabled          = true;
            input.Enabled = true;
            input.Focus();
        }