Bringing C5 1.0 into the main branch.
[mono.git] / mcs / class / Mono.C5 / current / UserGuideExamples / AnagramTreeBag.cs
1 /*\r
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
9  \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
12  \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
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: anagrams 2004-08-08, 2004-11-16\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll AnagramTreeBag.cs \r
26 \r
27 using System;\r
28 using System.IO;                        // StreamReader, TextReader\r
29 using System.Text;                      // Encoding\r
30 using System.Text.RegularExpressions;   // Regex\r
31 using C5;\r
32 using SCG = System.Collections.Generic;\r
33 \r
34 namespace AnagramTreeBag\r
35 {\r
36   class MyTest\r
37   {\r
38     public static void Main(String[] args)\r
39     {\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
44       else\r
45         ss = args;\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
51       int count = 0;\r
52       foreach (SCG.IEnumerable<String> anagramClass in classes)\r
53       {\r
54         count++;\r
55         // foreach (String s in anagramClass)\r
56         //   Console.Write(s + " ");\r
57         // Console.WriteLine();\r
58       }\r
59       Console.WriteLine("{0} non-trivial anagram classes", count);\r
60       Console.WriteLine(t.Check());\r
61     }\r
62 \r
63     // Read words at most n words from a file\r
64 \r
65     public static SCG.IEnumerable<String> ReadFileWords(String filename, int n)\r
66     {\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
70       {\r
71         for (String line = rd.ReadLine(); line != null; line = rd.ReadLine())\r
72         {\r
73           foreach (String s in delim.Split(line))\r
74             if (s != "")\r
75               yield return s.ToLower();\r
76           if (--n == 0)\r
77             yield break;\r
78         }\r
79       }\r
80     }\r
81 \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
86 \r
87     public static TreeBag<char> AnagramClass(String s)\r
88     {\r
89       TreeBag<char> anagram = new TreeBag<char>(Comparer<char>.Default, EqualityComparer<char>.Default);\r
90       foreach (char c in s)\r
91         anagram.Add(c);\r
92       return anagram;\r
93     }\r
94 \r
95     // Given a sequence of strings, return only the first member of each\r
96     // anagram class.\r
97 \r
98     public static SCG.IEnumerable<String> FirstAnagramOnly(SCG.IEnumerable<String> ss)\r
99     {\r
100       HashSet<TreeBag<char>> anagrams = new HashSet<TreeBag<char>>();\r
101       foreach (String s in ss)\r
102       {\r
103         TreeBag<char> anagram = AnagramClass(s);\r
104         if (!anagrams.Contains(anagram))\r
105         {\r
106           anagrams.Add(anagram);\r
107           yield return s;\r
108         }\r
109       }\r
110     }\r
111 \r
112     // Given a sequence of strings, return all non-trivial anagram\r
113     // classes.  \r
114 \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
118 \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
124 \r
125     // The maximal memory consumption is around 180 MB.\r
126 \r
127     public static SCG.IEnumerable<SCG.IEnumerable<String>>\r
128       AnagramClasses(SCG.IEnumerable<String> ss)\r
129     {\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
133       {\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
139       }\r
140       foreach (TreeSet<String> anagramClass in classes.Values)\r
141         if (anagramClass.Count > 1)\r
142           yield return anagramClass;\r
143     }\r
144   }\r
145 \r
146 // Crude timing utility ----------------------------------------\r
147 \r
148   public class Timer\r
149   {\r
150     private DateTime start;\r
151 \r
152     public Timer()\r
153     {\r
154       start = DateTime.Now;\r
155     }\r
156 \r
157     public double Check()\r
158     {\r
159       TimeSpan dur = DateTime.Now - start;\r
160       return dur.TotalSeconds;\r
161     }\r
162   }\r
163 }\r