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 AnagramTreeBag.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
34 namespace AnagramTreeBag
\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 HashSet<TreeBag<char>> anagrams = new HashSet<TreeBag<char>>();
\r
101 foreach (String s in ss)
\r
103 TreeBag<char> anagram = AnagramClass(s);
\r
104 if (!anagrams.Contains(anagram))
\r
106 anagrams.Add(anagram);
\r
112 // Given a sequence of strings, return all non-trivial anagram
\r
115 // Using TreeBag<char> and an unsequenced equalityComparer, this performs as
\r
116 // follows on 1600 MHz Mobile P4 and .Net 2.0 beta 1 (wall-clock
\r
117 // time; number of distinct words):
\r
119 // 50 000 words 2 822 classes 2.4 sec
\r
120 // 100 000 words 5 593 classes 4.6 sec
\r
121 // 200 000 words 11 705 classes 9.6 sec
\r
122 // 300 000 words 20 396 classes 88.2 sec (includes swapping)
\r
123 // 347 165 words 24 428 classes 121.3 sec (includes swapping)
\r
125 // The maximal memory consumption is around 180 MB.
\r
127 public static SCG.IEnumerable<SCG.IEnumerable<String>>
\r
128 AnagramClasses(SCG.IEnumerable<String> ss)
\r
130 IDictionary<TreeBag<char>, TreeSet<String>> classes;
\r
131 classes = new HashDictionary<TreeBag<char>, TreeSet<String>>();
\r
132 foreach (String s in ss)
\r
134 TreeBag<char> anagram = AnagramClass(s);
\r
135 TreeSet<String> anagramClass;
\r
136 if (!classes.Find(anagram, out anagramClass))
\r
137 classes[anagram] = anagramClass = new TreeSet<String>();
\r
138 anagramClass.Add(s);
\r
140 foreach (TreeSet<String> anagramClass in classes.Values)
\r
141 if (anagramClass.Count > 1)
\r
142 yield return anagramClass;
\r
146 // Crude timing utility ----------------------------------------
\r
150 private DateTime start;
\r
154 start = DateTime.Now;
\r
157 public double Check()
\r
159 TimeSpan dur = DateTime.Now - start;
\r
160 return dur.TotalSeconds;
\r