2 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
\r
3 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
4 of this software and associated documentation files (the "Software"), to deal
\r
5 in the Software without restriction, including without limitation the rights
\r
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
7 copies of the Software, and to permit persons to whom the Software is
\r
8 furnished to do so, subject to the following conditions:
\r
10 The above copyright notice and this permission notice shall be included in
\r
11 all copies or substantial portions of the Software.
\r
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
22 // C5 example: anagrams 2004-08-08, 2004-11-16
\r
25 // csc /r:C5.dll Anagrams.cs
\r
28 using System.IO; // StreamReader, TextReader
\r
29 using System.Text; // Encoding
\r
30 using System.Text.RegularExpressions; // Regex
\r
32 using SCG = System.Collections.Generic;
\r
38 public static void Main(String[] args)
\r
40 Console.OutputEncoding = Encoding.GetEncoding("iso-8859-1");
\r
41 SCG.IEnumerable<String> ss;
\r
42 if (args.Length == 2)
\r
43 ss = ReadFileWords(args[0], int.Parse(args[1]));
\r
46 // foreach (String s in FirstAnagramOnly(ss))
\r
47 // Console.WriteLine(s);
\r
48 // Console.WriteLine("===");
\r
49 Timer t = new Timer();
\r
50 SCG.IEnumerable<SCG.IEnumerable<String>> classes = AnagramClasses(ss);
\r
52 foreach (SCG.IEnumerable<String> anagramClass in classes)
\r
55 // foreach (String s in anagramClass)
\r
56 // Console.Write(s + " ");
\r
57 // Console.WriteLine();
\r
59 Console.WriteLine("{0} non-trivial anagram classes", count);
\r
60 Console.WriteLine(t.Check());
\r
63 // Read words at most n words from a file
\r
65 public static SCG.IEnumerable<String> ReadFileWords(String filename, int n)
\r
67 Regex delim = new Regex("[^a-zæøåA-ZÆØÅ0-9-]+");
\r
68 Encoding enc = Encoding.GetEncoding("iso-8859-1");
\r
69 using (TextReader rd = new StreamReader(filename, enc))
\r
71 for (String line = rd.ReadLine(); line != null; line = rd.ReadLine())
\r
73 foreach (String s in delim.Split(line))
\r
75 yield return s.ToLower();
\r
82 // From an anagram point of view, a word is just a bag of
\r
83 // characters. So an anagram class is represented as TreeBag<char>
\r
84 // which permits fast equality comparison -- we shall use them as
\r
85 // elements of hash sets or keys in hash maps.
\r
87 public static TreeBag<char> AnagramClass(String s)
\r
89 TreeBag<char> anagram = new TreeBag<char>(Comparer<char>.Default, EqualityComparer<char>.Default);
\r
90 foreach (char c in s)
\r
95 // Given a sequence of strings, return only the first member of each
\r
98 public static SCG.IEnumerable<String> FirstAnagramOnly(SCG.IEnumerable<String> ss)
\r
100 SCG.IEqualityComparer<TreeBag<char>> tbh
\r
101 = UnsequencedCollectionEqualityComparer<TreeBag<char>, char>.Default;
\r
102 HashSet<TreeBag<char>> anagrams = new HashSet<TreeBag<char>>(tbh);
\r
103 foreach (String s in ss)
\r
105 TreeBag<char> anagram = AnagramClass(s);
\r
106 if (!anagrams.Contains(anagram))
\r
108 anagrams.Add(anagram);
\r
114 // Given a sequence of strings, return all non-trivial anagram
\r
115 // classes. Should use a *sequenced* equalityComparer on a TreeBag<char>,
\r
116 // obviously: after all, characters can be sorted by ASCII code. On
\r
117 // 347 000 distinct Danish words this takes 70 cpu seconds, 180 MB
\r
118 // memory, and 263 wall-clock seconds (due to swapping).
\r
120 // Using a TreeBag<char> and a sequenced equalityComparer takes 82 cpu seconds
\r
121 // and 180 MB RAM to find the 26,058 anagram classes among 347,000
\r
124 // Using an unsequenced equalityComparer on TreeBag<char> or HashBag<char>
\r
125 // makes it criminally slow: at least 1200 cpu seconds. This must
\r
126 // be because many bags get the same hash code, so that there are
\r
127 // many collisions. But exactly how the unsequenced equalityComparer works is
\r
128 // not clear ... or is it because unsequenced equality is slow?
\r
130 public static SCG.IEnumerable<SCG.IEnumerable<String>> AnagramClasses(SCG.IEnumerable<String> ss)
\r
133 IDictionary<TreeBag<char>, TreeSet<String>> classes;
\r
136 SCG.IEqualityComparer<TreeBag<char>> unsequencedTreeBagEqualityComparer
\r
137 = UnsequencedCollectionEqualityComparer<TreeBag<char>, char>.Default;
\r
138 classes = new HashDictionary<TreeBag<char>, TreeSet<String>>(unsequencedTreeBagEqualityComparer);
\r
142 SCG.IEqualityComparer<TreeBag<char>> sequencedTreeBagEqualityComparer
\r
143 = SequencedCollectionEqualityComparer<TreeBag<char>, char>.Default;
\r
144 classes = new HashDictionary<TreeBag<char>, TreeSet<String>>(sequencedTreeBagEqualityComparer);
\r
146 foreach (String s in ss)
\r
148 TreeBag<char> anagram = AnagramClass(s);
\r
149 TreeSet<String> anagramClass;
\r
150 if (!classes.Find(anagram, out anagramClass))
\r
151 classes[anagram] = anagramClass = new TreeSet<String>();
\r
152 anagramClass.Add(s);
\r
154 foreach (TreeSet<String> anagramClass in classes.Values)
\r
155 if (anagramClass.Count > 1)
\r
156 yield return anagramClass;
\r
160 // Crude timing utility ----------------------------------------
\r
164 private DateTime start;
\r
168 start = DateTime.Now;
\r
171 public double Check()
\r
173 TimeSpan dur = DateTime.Now - start;
\r
174 return dur.TotalSeconds;
\r