[16157] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[16193] | 3 | using System.Diagnostics;
|
---|
[16157] | 4 | using System.Linq;
|
---|
[16193] | 5 | using System.Security.Cryptography;
|
---|
[15714] | 6 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
|
---|
| 7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[16193] | 8 | using HierarchicalFormatter = HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.SymbolicExpressionTreeHierarchicalFormatter;
|
---|
[15714] | 9 |
|
---|
| 10 | namespace Test {
|
---|
| 11 | [TestClass]
|
---|
| 12 | public class TreeHashingTest {
|
---|
| 13 |
|
---|
| 14 | private Grammar grammar;
|
---|
| 15 | private TerminalSymbol varA;
|
---|
| 16 | private TerminalSymbol varB;
|
---|
| 17 | private TerminalSymbol varC;
|
---|
[15849] | 18 | private TerminalSymbol c;
|
---|
[15714] | 19 |
|
---|
[16193] | 20 | Func<Grammar, SymbolList, int> ComputeHash = (grammar, sentence) => grammar.ComputeHash(null, sentence);
|
---|
| 21 |
|
---|
[15714] | 22 | [TestInitialize]
|
---|
| 23 | public void InitTest() {
|
---|
| 24 | grammar = new Grammar(new[] { "a", "b", "c" });
|
---|
| 25 |
|
---|
[15834] | 26 | varA = grammar.VarTerminals.First(s => s.StringRepresentation == "a");
|
---|
| 27 | varB = grammar.VarTerminals.First(s => s.StringRepresentation == "b");
|
---|
| 28 | varC = grammar.VarTerminals.First(s => s.StringRepresentation == "c");
|
---|
[15849] | 29 | c = grammar.Const;
|
---|
[15714] | 30 | }
|
---|
| 31 |
|
---|
| 32 | [TestMethod]
|
---|
| 33 | [TestCategory("TreeHashing")]
|
---|
| 34 | public void SimpleEqualityAddition() {
|
---|
[16026] | 35 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
|
---|
| 36 | SymbolList s2 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
|
---|
[15714] | 37 |
|
---|
[16193] | 38 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 39 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15714] | 40 |
|
---|
| 41 | Assert.AreEqual(hash1, hash2);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | [TestMethod]
|
---|
| 45 | [TestCategory("TreeHashing")]
|
---|
| 46 | public void SimpleInequalityAddition() {
|
---|
[16026] | 47 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
|
---|
| 48 | SymbolList s2 = new SymbolList(new[] { varB, varB, grammar.Addition, varB, grammar.Addition });
|
---|
[15714] | 49 |
|
---|
[16193] | 50 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 51 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15714] | 52 |
|
---|
| 53 | Assert.AreNotEqual(hash1, hash2);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [TestMethod]
|
---|
| 57 | [TestCategory("TreeHashing")]
|
---|
| 58 | public void CommutativityAddition() {
|
---|
[16026] | 59 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition });
|
---|
| 60 | SymbolList s2 = new SymbolList(new[] { varB, varA, grammar.Addition });
|
---|
[15714] | 61 |
|
---|
[16193] | 62 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 63 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15714] | 64 |
|
---|
| 65 | Assert.AreEqual(hash1, hash2);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | [TestMethod]
|
---|
| 69 | [TestCategory("TreeHashing")]
|
---|
| 70 | public void AssociativityAddition() {
|
---|
[16026] | 71 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varA, grammar.Addition });
|
---|
| 72 | SymbolList s2 = new SymbolList(new[] { varA, varB, varA, grammar.Addition, grammar.Addition });
|
---|
[15714] | 73 |
|
---|
[16193] | 74 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 75 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15714] | 76 |
|
---|
| 77 | Assert.AreEqual(hash1, hash2);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | [TestMethod]
|
---|
| 81 | [TestCategory("TreeHashing")]
|
---|
| 82 | public void RepeatedAddition() {
|
---|
[16026] | 83 | SymbolList s1 = new SymbolList(new[] { varA, varA, grammar.Addition, varA, grammar.Addition });
|
---|
| 84 | SymbolList s2 = new SymbolList(new[] { varA });
|
---|
[15714] | 85 |
|
---|
[16193] | 86 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 87 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15714] | 88 |
|
---|
| 89 | Assert.AreEqual(hash1, hash2);
|
---|
| 90 | }
|
---|
[15725] | 91 |
|
---|
| 92 | [TestMethod]
|
---|
| 93 | [TestCategory("TreeHashing")]
|
---|
| 94 | public void ComplexInequality() {
|
---|
[16026] | 95 | SymbolList s1 = new SymbolList(new[] { varA, varA, varA, grammar.Multiplication, grammar.Multiplication });
|
---|
| 96 | SymbolList s2 = new SymbolList(new[] { varA, varA, varA, grammar.Multiplication, grammar.Addition });
|
---|
[15725] | 97 |
|
---|
[16193] | 98 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 99 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15725] | 100 |
|
---|
| 101 | Assert.AreNotEqual(hash1, hash2);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[15746] | 104 | [TestMethod]
|
---|
| 105 | [TestCategory("TreeHashing")]
|
---|
| 106 | public void NonterminalHashing() {
|
---|
[16026] | 107 | SymbolList s1 = new SymbolList(new Symbol[] { varA, varA, grammar.Expr, grammar.Addition, grammar.Addition });
|
---|
| 108 | SymbolList s2 = new SymbolList(new Symbol[] { varA, grammar.Expr, grammar.Addition });
|
---|
[15725] | 109 |
|
---|
[16193] | 110 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 111 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15746] | 112 |
|
---|
| 113 | Assert.AreEqual(hash1, hash2);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[15784] | 116 | [TestMethod]
|
---|
| 117 | [TestCategory("TreeHashing")]
|
---|
| 118 | public void InverseFactorCancelationSimple() {
|
---|
[15791] | 119 | // 1/a * b * a * a
|
---|
[16026] | 120 | SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication });
|
---|
[15791] | 121 | // a * b
|
---|
[16026] | 122 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Multiplication });
|
---|
[15746] | 123 |
|
---|
[16193] | 124 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 125 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15784] | 126 |
|
---|
| 127 | Assert.AreEqual(hash1, hash2);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | [TestMethod]
|
---|
| 131 | [TestCategory("TreeHashing")]
|
---|
| 132 | public void InverseFactorCancelationComplex() {
|
---|
[16026] | 133 | SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Sin, varA, varA, grammar.Multiplication, varA, grammar.Addition, grammar.Sin, grammar.Addition });
|
---|
| 134 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varA, varA, grammar.Multiplication, grammar.Addition, grammar.Sin, varA, grammar.Inv, varA, grammar.Sin, varA, grammar.Multiplication, grammar.Multiplication, grammar.Addition });
|
---|
[15784] | 135 |
|
---|
[16193] | 136 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 137 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15784] | 138 |
|
---|
[16193] | 139 | Console.WriteLine(s1);
|
---|
| 140 | Console.WriteLine(PrintTree(s1));
|
---|
| 141 | Console.WriteLine(grammar.Simplify(null, s1));
|
---|
| 142 | Console.WriteLine(hash1);
|
---|
| 143 | Console.WriteLine();
|
---|
| 144 | Console.WriteLine(s2);
|
---|
| 145 | Console.WriteLine(PrintTree(s2));
|
---|
| 146 | Console.WriteLine(grammar.Simplify(null, s2));
|
---|
| 147 | Console.WriteLine(hash2);
|
---|
| 148 |
|
---|
| 149 |
|
---|
[15784] | 150 | Assert.AreEqual(hash1, hash2);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[15849] | 153 | // Constants
|
---|
| 154 | [TestMethod]
|
---|
| 155 | [TestCategory("TreeHashing")]
|
---|
| 156 | public void SimpleConst() {
|
---|
[16056] | 157 | SymbolList s1 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition });
|
---|
[16026] | 158 | SymbolList s2 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, varA, grammar.Multiplication, grammar.Addition, c, grammar.Addition });
|
---|
[15849] | 159 |
|
---|
[16193] | 160 | int hash1 = ComputeHash(grammar, s1);
|
---|
| 161 | int hash2 = ComputeHash(grammar, s2);
|
---|
[15849] | 162 |
|
---|
| 163 | Assert.AreEqual(hash1, hash2);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[15791] | 166 | [TestMethod]
|
---|
| 167 | [TestCategory("TreeHashing")]
|
---|
[16157] | 168 | public void EnumerateGrammarTest() {
|
---|
| 169 | //const int nvars = 1;
|
---|
| 170 | //var variables = Enumerable.Range(1, nvars).Select(x => $"x{x}").ToArray();
|
---|
[16193] | 171 | var variables = new[] { "a", "b" };
|
---|
[16157] | 172 | var grammar = new Grammar(variables, Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>());
|
---|
[15791] | 173 |
|
---|
[16159] | 174 | Func<SymbolList, int> hash = s => grammar.Hasher.CalcHashCode(s);
|
---|
[16193] | 175 | int length = 100;
|
---|
| 176 | int depth = 20;
|
---|
[15791] | 177 |
|
---|
[16193] | 178 | //List<SymbolList> sentences = EnumerateGrammarBreadth(grammar, length: length, hashPhrases: false).ToList();
|
---|
| 179 | //Console.WriteLine("Breadth: {0} generated, {1} distinct sentences", sentences.Count, sentences.GroupBy(hash).Count());
|
---|
[16157] | 180 |
|
---|
[16193] | 181 | //var sentences = EnumerateGrammarBreadth(grammar, length: length, hashPhrases: true).ToList();
|
---|
| 182 | //Console.WriteLine("Breadth (hashed): {0} generated, {1} distinct sentences", sentences.Count, sentences.GroupBy(hash).Count());
|
---|
[16157] | 183 |
|
---|
[16193] | 184 | var sentences = EnumerateGrammarDepth(grammar, length: length, depth: depth, hashPhrases: false).ToList();
|
---|
[16159] | 185 | Console.WriteLine("Depth: {0} generated, {1} distinct sentences", sentences.Count, sentences.GroupBy(hash).Count());
|
---|
[16157] | 186 |
|
---|
[16193] | 187 | sentences = EnumerateGrammarDepth(grammar, length: length, depth: depth, hashPhrases: true).ToList();
|
---|
[16159] | 188 | Console.WriteLine("Depth (hashed): {0} generated, {1} distinct sentences", sentences.Count, sentences.GroupBy(hash).Count());
|
---|
[15791] | 189 | }
|
---|
| 190 |
|
---|
[16193] | 191 | [TestMethod]
|
---|
| 192 | [TestCategory("TreeHashing")]
|
---|
| 193 | public void HashExtensionsTest() {
|
---|
| 194 | var variables = new[] { "x", "y", "z" };
|
---|
| 195 | var rules = Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>();
|
---|
| 196 | var grammar = new Grammar(variables, rules);
|
---|
| 197 | var add = grammar.Addition;
|
---|
| 198 | var mul = grammar.Multiplication;
|
---|
| 199 | var exp = grammar.Exp;
|
---|
| 200 | var log = grammar.Log;
|
---|
| 201 | var inv = grammar.Inv;
|
---|
| 202 | var sin = grammar.Sin;
|
---|
| 203 |
|
---|
| 204 | var c = grammar.Const;
|
---|
| 205 | var x = grammar.VarTerminals.Single(v => v.StringRepresentation == "x");
|
---|
| 206 | var y = grammar.VarTerminals.Single(v => v.StringRepresentation == "y");
|
---|
| 207 | var z = grammar.VarTerminals.Single(v => v.StringRepresentation == "z");
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | var ha = SHA512.Create();
|
---|
| 211 |
|
---|
| 212 | var sentences = new[] {
|
---|
| 213 | new SymbolList(c, c, x, mul, c, add, inv, mul, c, add),
|
---|
| 214 | new SymbolList(c, c, x, mul, c, add, log, mul, c, add),
|
---|
| 215 | new SymbolList(x, x, add),
|
---|
| 216 | new SymbolList(x, x, add, x, add),
|
---|
| 217 | new SymbolList(x, x, add, y, add),
|
---|
| 218 | new SymbolList(x, x, add, x, add, x, add),
|
---|
| 219 | new SymbolList(x, x, add, y, add, x, add),
|
---|
| 220 | new SymbolList(x, y, mul, x, y, mul, add),
|
---|
| 221 | new SymbolList(x, y, mul, y, x, mul, add),
|
---|
| 222 | new SymbolList(x, y, mul, z, y, mul, add),
|
---|
| 223 | new SymbolList(x, x, add, y, mul),
|
---|
| 224 | new SymbolList(x, x, add, y, mul, y, mul),
|
---|
| 225 | new SymbolList(x, x, inv, mul),
|
---|
| 226 | new SymbolList(x, inv, x, inv, mul, x, mul, x, mul),
|
---|
| 227 | new SymbolList(c, x, x, x, x, x, x, mul, mul, mul, mul, mul, mul, c, x, mul, c, add, add),
|
---|
| 228 | new SymbolList(c, x, x, x, x, x, x, mul, mul, mul, mul, mul, mul, c, x, mul, c, add, add),
|
---|
| 229 | new SymbolList(c, x, mul, c, x, x, x, x, x, x, mul, mul, mul, mul, mul, mul, c, add, add),
|
---|
| 230 | new SymbolList(c, x, x, x, x, mul, mul, mul, mul, c, x, mul, c, x, mul, c, add, add, add),
|
---|
| 231 | new SymbolList(c, x, mul, c, x, x, x, x, mul, mul, mul, mul, c, x, mul, c, add, add, add)
|
---|
| 232 | };
|
---|
| 233 |
|
---|
| 234 | foreach (var sentence in sentences) {
|
---|
| 235 | var simplified = grammar.Simplify(ha, sentence);
|
---|
| 236 | Console.WriteLine($"{sentence} -> {simplified} {grammar.Hasher.CalcHashCode(sentence)} {grammar.ComputeHash(ha, sentence)}");
|
---|
| 237 | Console.WriteLine();
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | string PrintTree(SymbolList s) {
|
---|
| 242 | var t = grammar.ParseSymbolicExpressionTree(s);
|
---|
| 243 | return HierarchicalFormatter.Format(t.Root.GetSubtree(0).GetSubtree(0));
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | [TestMethod]
|
---|
| 247 | [TestCategory("TreeHashing")]
|
---|
| 248 | public void HashCollisionsTest() {
|
---|
| 249 | var variables = new[] { "x", "y", "z", "w" };
|
---|
| 250 | //var rules = Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>().Except(new[] { GrammarRule.InverseTerm, GrammarRule.Exponentiation, GrammarRule.Logarithm, GrammarRule.Sine });
|
---|
| 251 | var rules = Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>();
|
---|
| 252 | var grammar = new Grammar(variables, rules);
|
---|
| 253 |
|
---|
| 254 | int maxLength = 20, maxDepth = int.MaxValue;
|
---|
| 255 | var sentences = EnumerateGrammarDepth(grammar, length: maxLength, depth: maxDepth, hashPhrases: false).ToList();
|
---|
| 256 | var count = sentences.Count;
|
---|
| 257 |
|
---|
| 258 | var sw = new Stopwatch();
|
---|
| 259 | sw.Start();
|
---|
| 260 | var hashes = sentences.Select(grammar.Hasher.CalcHashCode).ToList();
|
---|
| 261 | sw.Stop();
|
---|
| 262 | Console.WriteLine($"Old hash: {sentences.Count} ({hashes.Distinct().Count()}) hashed in {sw.ElapsedMilliseconds / 1000.0} seconds ({1000d * sentences.Count / sw.ElapsedMilliseconds} hashes/s)");
|
---|
| 263 |
|
---|
| 264 | var ha = SHA512.Create();
|
---|
| 265 | sw.Restart();
|
---|
| 266 | var hashes_new = sentences.Select(x => grammar.ComputeHash(ha, x)).ToList();
|
---|
| 267 | sw.Stop();
|
---|
| 268 | Console.WriteLine($"New hash: {sentences.Count} ({hashes_new.Distinct().Count()}) hashed in {sw.ElapsedMilliseconds / 1000.0} seconds ({1000.0 * sentences.Count / sw.ElapsedMilliseconds} hashes/s)");
|
---|
| 269 |
|
---|
| 270 | var distinct = Enumerable.Range(0, count).GroupBy(x => hashes_new[x]).Select(g => g.OrderBy(x => x).First()).ToList();
|
---|
| 271 | var collisions = distinct.ToLookup(x => hashes[x], x => Tuple.Create(hashes_new[x], sentences[x]));
|
---|
| 272 |
|
---|
| 273 | foreach (var pair in collisions) {
|
---|
| 274 | if (pair.Count() > 1) {
|
---|
| 275 | Console.WriteLine(pair.Key);
|
---|
| 276 | foreach (var t in pair) {
|
---|
| 277 | Console.WriteLine($"\t{t}");
|
---|
| 278 | Console.WriteLine($"\t{grammar.ToInfixString(t.Item2)}");
|
---|
| 279 | var simplified = grammar.Simplify(ha, t.Item2);
|
---|
| 280 | Console.WriteLine($"\t{simplified}");
|
---|
| 281 | Console.Write($"\t");
|
---|
| 282 | PrintTree(t.Item2);
|
---|
| 283 | Console.WriteLine();
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | [TestMethod]
|
---|
| 290 | [TestCategory("TreeHashing")]
|
---|
| 291 | public void HashPerformance() {
|
---|
| 292 | var nvars = 4;
|
---|
| 293 | var variables = Enumerable.Range(0, nvars).Select(x => $"X{x}").ToArray();
|
---|
| 294 | //var rules = Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>().Except(new[] { GrammarRule.InverseTerm, GrammarRule.Exponentiation, GrammarRule.Logarithm, GrammarRule.Sine });
|
---|
| 295 | var rules = Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>();
|
---|
| 296 | var grammar = new Grammar(variables, rules);
|
---|
| 297 |
|
---|
| 298 | int maxLength = 20, maxDepth = int.MaxValue;
|
---|
| 299 | var sentences = EnumerateGrammarDepth(grammar, length: maxLength, depth: maxDepth, hashPhrases: false).ToList();
|
---|
| 300 | var count = sentences.Count;
|
---|
| 301 |
|
---|
| 302 | var ha = SHA512.Create();
|
---|
| 303 |
|
---|
| 304 | var sw = new Stopwatch();
|
---|
| 305 | sw.Start();
|
---|
| 306 | var hashes = sentences.Select(x => grammar.ComputeHash(ha, x)).ToList();
|
---|
| 307 | sw.Stop();
|
---|
| 308 |
|
---|
| 309 | Console.WriteLine($"New: {sentences.Count} ({hashes.Distinct().Count()}) hashed in {sw.ElapsedMilliseconds / 1000.0} seconds ({1000d * sentences.Count / sw.ElapsedMilliseconds} hashes/s)");
|
---|
| 310 |
|
---|
| 311 | sw.Restart();
|
---|
| 312 | var hashes_old = sentences.Select(x => grammar.Hasher.CalcHashCode(x)).ToList();
|
---|
| 313 | sw.Stop();
|
---|
| 314 |
|
---|
| 315 | Console.WriteLine($"Old: {sentences.Count} ({hashes_old.Distinct().Count()}) hashed in {sw.ElapsedMilliseconds / 1000.0} seconds ({1000d * sentences.Count / sw.ElapsedMilliseconds} hashes/s)");
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | #region enumerate the grammar
|
---|
[16157] | 319 | private static IEnumerable<SymbolList> EnumerateGrammarBreadth(Grammar grammar, int length, bool hashPhrases = true) {
|
---|
| 320 | var phrases = new Queue<SymbolList>();
|
---|
| 321 | phrases.Enqueue(new SymbolList(grammar.StartSymbol));
|
---|
| 322 | var sentences = new List<SymbolList>();
|
---|
| 323 | var archive = new HashSet<int>();
|
---|
[15791] | 324 |
|
---|
[16157] | 325 | while (phrases.Any()) {
|
---|
| 326 | var phrase = phrases.Dequeue();
|
---|
[15791] | 327 |
|
---|
[16157] | 328 | if (phrase.Count > length)
|
---|
| 329 | continue;
|
---|
| 330 |
|
---|
| 331 | if (phrase.IsSentence()) {
|
---|
| 332 | sentences.Add(phrase);
|
---|
| 333 | continue;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | if (hashPhrases && !archive.Add(grammar.Hasher.CalcHashCode(phrase))) {
|
---|
| 337 | continue;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | var idx = phrase.NextNonterminalIndex();
|
---|
| 341 | var productions = grammar.Productions[phrase[idx]];
|
---|
| 342 | var derived = productions.Select(p => phrase.DerivePhrase(idx, p)).Where(p => p.Count <= length);
|
---|
| 343 | foreach (var d in derived)
|
---|
| 344 | phrases.Enqueue(d);
|
---|
| 345 | }
|
---|
| 346 | return sentences;
|
---|
[15791] | 347 | }
|
---|
| 348 |
|
---|
[16193] | 349 | private static IEnumerable<SymbolList> EnumerateGrammarDepth(Grammar grammar, int length, int depth, bool hashPhrases = true) {
|
---|
| 350 | return Expand(new SymbolList(grammar.StartSymbol), grammar, length, 0, depth, hashPhrases ? new HashSet<int>() : null);
|
---|
[16157] | 351 | }
|
---|
[15791] | 352 |
|
---|
[16193] | 353 | private static IEnumerable<SymbolList> Expand(SymbolList phrase, Grammar grammar, int maxLength, int depth, int maxDepth, HashSet<int> visited) {
|
---|
| 354 | if (maxDepth < depth || maxLength < phrase.Count) {
|
---|
[16157] | 355 | yield break;
|
---|
| 356 | }
|
---|
[15791] | 357 |
|
---|
[16157] | 358 | if (phrase.IsSentence()) {
|
---|
| 359 | yield return phrase;
|
---|
| 360 | yield break;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[16193] | 363 | if (visited != null && !visited.Add(HashExtensions.ComputeHash(grammar, null, phrase))) {
|
---|
[16157] | 364 | yield break;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | var i = phrase.NextNonterminalIndex();
|
---|
| 368 | var productions = grammar.Productions[phrase[i]];
|
---|
| 369 |
|
---|
[16193] | 370 | foreach (var s in productions.SelectMany(p => Expand(phrase.DerivePhrase(i, p), grammar, maxLength, depth + 1, maxDepth, visited)))
|
---|
[16157] | 371 | yield return s;
|
---|
| 372 | }
|
---|
[16193] | 373 | #endregion
|
---|
[15714] | 374 | }
|
---|
| 375 | }
|
---|