private static void ExecuteTest(Grammar g, List <Sentence> sentences) { CNFGrammar h = g.ToCNF(); var earley = new EarleyParser(g); var cyk = new CykParser(h); foreach (var sentence in sentences) { var p1 = cyk.ParseGetProbability(sentence); var p2 = earley.ParseGetProbability(sentence); Helpers.AssertNear(p1, p2); } }
private bool ProcessOneGrammar() { var(g, terminals) = NextGrammar(); var h = g.ToCNF(); // Console.WriteLine(g.Productions.Count()); var preparedSentences = new List <Sentence>(); for (int length = 0; length <= _maxInputLength; length++) { var combinations = CFGLibTest.Helpers.CombinationsWithRepetition(terminals, length); foreach (var target in combinations) { var sentence = new Sentence(target); preparedSentences.Add(sentence); } } AddRandomSentences(preparedSentences, terminals); var uniquifySentences = new Dictionary <string, Sentence>(); foreach (var sentence in preparedSentences) { uniquifySentences[sentence.AsTerminals()] = sentence; } preparedSentences = uniquifySentences.Values.ToList(); // Console.WriteLine("Parsing sentences..."); EarleyParser earley1; EarleyParser2 earley2; CykParser cyk; try { earley1 = new EarleyParser(g); earley2 = new EarleyParser2(g); cyk = new CykParser(h); } catch (Exception e) { Report(g, e); return(true); } foreach (var sentence in preparedSentences) { try { var sppf1 = earley1.ParseGetForest(sentence); var sppf2 = earley2.ParseGetForest(sentence); if (sppf1 == null && sppf2 != null) { throw new Exception(); } if (sppf2 == null && sppf1 != null) { throw new Exception(); } var p1 = earley1.ProbabilityOfSppf(sppf1); var p2 = earley2.ProbabilityOfSppf(sppf2); var p3 = cyk.ParseGetProbability(sentence); if (!Helpers.IsNear(p1, p2)) { throw new Exception(); } if (!Helpers.IsNear(p1, p3)) { throw new Exception(); } try { TestTraversal.CheckTraversal(g, sentence, sppf1); } catch (TraversalLoopException) { } try { TestTraversal.CheckTraversal(g, sentence, sppf2); } catch (TraversalLoopException) { } } catch (Exception e) { Report(g, e, sentence); return(true); // throw new RandomTestException(e, g, sentence); } } return(false); }
public void RandomParsingTest( int _numGrammars = 10000, int _numNonterminals = 10, int _numTerminals = 5, int _numProductions = 30, int _maxProductionLength = 8, int _maxInputLength = 6, int seed = 0 ) { var printStatus = true; var range = Enumerable.Range(0, _numTerminals); var terminals = new List <Terminal>(range.Select((x) => Terminal.Of("x" + x))); Console.WriteLine("Preparing sentences"); var preparedSentences = new List <Sentence>(); for (int length = 0; length <= _maxInputLength; length++) { var combinations = CFGLibTest.Helpers.CombinationsWithRepetition(terminals, length); foreach (var target in combinations) { var sentence = new Sentence(target); preparedSentences.Add(sentence); } } var randg = new GrammarGenerator(seed); var preparedGrammars = new List <Grammar>(_numGrammars); var preparedGrammarsCNF = new List <CNFGrammar>(_numGrammars); Console.WriteLine("Preparing grammars"); for (int i = 0; i < _numGrammars; i++) { Grammar g = null; while (g == null) { // g = randg.NextCNF(_numNonterminals, _numProductions, terminals); g = randg.NextCFG(_numNonterminals, _numProductions, _maxProductionLength, terminals, true); if (g.Productions.Count() == 0) { g = null; } } // Console.WriteLine("---------------{0}/{1}---------------", i.ToString("D5"), _numGrammars.ToString("D5")); // Console.WriteLine(g.ToCodeString()); var h = g.ToCNF(); //return; // Console.WriteLine(g); // g.PrintProbabilities(2, 3); preparedGrammars.Add(g); preparedGrammarsCNF.Add(h); } Console.WriteLine("starting"); var sw = Stopwatch.StartNew(); int count = 0; for (int grammarIndex = 0; grammarIndex < _numGrammars; grammarIndex++) { if (printStatus) { Console.WriteLine("---------------{0}/{1}---------------", grammarIndex.ToString("D5"), _numGrammars.ToString("D5")); } var g = preparedGrammars[grammarIndex]; var h = preparedGrammarsCNF[grammarIndex]; var earley = new EarleyParser(g); var cyk = new CykParser(h); // Console.WriteLine(g.ToCodeString()); // Console.Write("{0}, ", count); count++; var accepts = 0; foreach (var sentence in preparedSentences) { try { var p1 = earley.ParseGetProbability(sentence); var p2 = cyk.ParseGetProbability(sentence); if (!Helpers.IsNear(p2, p1)) { throw new Exception(); } var accepts1 = p1 > 0; var accepts2 = p2 > 0; if (accepts2) { accepts++; } } catch (Exception) { Report(g, sentence); throw; } } if (printStatus) { Console.WriteLine("Accepted {0} / {1}", accepts, preparedSentences.Count); } } sw.Stop(); Console.WriteLine(); Console.WriteLine("inner Elapsed: {0}s", sw.Elapsed.TotalMilliseconds / 1000.0); // Console.WriteLine("Per CYK: {0}ms", sw.Elapsed.TotalMilliseconds / (_numGrammars * preparedSentences.Count)); }