예제 #1
0
        // given a list of words and a list of anagrams, make more
        // anagrams by combining the two.
        private static anagrams combine(strings ws, anagrams ans)
        {
            anagrams rv = new anagrams();

            foreach (strings a in ans)
            {
                foreach (string word in ws)
                {
                    strings bigger_anagram = new strings();
                    bigger_anagram.InsertRange(0, a);
                    bigger_anagram.Add(word);
                    rv.Add(bigger_anagram);
                }
            }

            return(rv);
        }
예제 #2
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();
        }
예제 #3
0
        public static anagrams anagrams(Bag bag,
                                        List <bag_and_anagrams> dictionary,
                                        uint recursion_level,
                                        bottom_of_main_loop bottom,
                                        done_pruning done_pruning_callback,
                                        found_anagram success_callback)
        {
            anagrams rv = new anagrams();
            List <bag_and_anagrams> pruned = prune(bag,
                                                   dictionary,
                                                   done_pruning_callback,
                                                   recursion_level);
            int pruned_initial_size = pruned.Count;

            while (pruned.Count > 0)
            {
                bag_and_anagrams entry = pruned[0];
                Bag this_bag           = entry.b;
                Bag diff = bag.subtract(this_bag);
                if (diff != null)
                {
                    if (diff.empty())
                    {
                        foreach (string w in entry.words)
                        {
                            strings loner = new strings();
                            loner.Add(w);
                            rv.Add(loner);
                            if (recursion_level == 0)
                            {
                                success_callback(loner);
                            }
                        }
                    }
                    else
                    {
                        anagrams from_smaller = anagrams(diff, pruned, recursion_level + 1,
                                                         bottom,
                                                         done_pruning_callback,
                                                         success_callback);
                        anagrams combined = combine(entry.words, from_smaller);
                        foreach (strings an in combined)
                        {
                            rv.Add(an);
                            if (recursion_level == 0)
                            {
                                success_callback(an);
                            }
                        }
                    }
                }
                pruned.RemoveAt(0);
                if (recursion_level == 0)
                {
                    bottom();
                }

                Application.DoEvents();
            }
            return(rv);
        }
예제 #4
0
파일: Form1.cs 프로젝트: offby1/anagrams
        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();
        }
예제 #5
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();
        }