CFGLib.Parsers.CYK.CykParser.ParseGetProbability C# (CSharp) Method

ParseGetProbability() public method

Returns the probability that this grammar generated the given sentence
public ParseGetProbability ( Sentence s ) : double
s Sentence
return double
		public override double ParseGetProbability(Sentence s) {
			if (s.Count == 0) {
				return _grammar.ProbabilityNull;
			}

			var nonterminals_R = _grammar.GetNonterminals();
			var RToJ = BuildRToJ(nonterminals_R);

			var P = new double[s.Count, s.Count, nonterminals_R.Count];
			var shouldPunt = CykFillInBase(s, P, RToJ);
			if (shouldPunt) {
				return 0.0;
			}

			var localProductionList = BuildLocalCYKProductionList(RToJ);

			for (int i = 2; i <= s.Count; i++) {
				for (int j = 1; j <= s.Count - i + 1; j++) {
					for (int k = 1; k <= i - 1; k++) {
						foreach (var production in localProductionList) {
							var A = production.A;
							var B = production.B;
							var C = production.C;
							var probThis = production.Probability;

							var pleft = P[k - 1, j - 1, B];
							var pright = P[i - k - 1, j + k - 1, C];
							P[i - 1, j - 1, A] += pleft * pright * probThis;
						}
					}
				}
			}

			return P[s.Count - 1, 0, RToJ[_grammar.Start]];
		}

Usage Example

Exemplo n.º 1
0
		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);
			}
		}
All Usage Examples Of CFGLib.Parsers.CYK.CykParser::ParseGetProbability