public double ParseGetProbability(Sentence s) { var earley = new Parsers.Earley.EarleyParser(this); return(earley.ParseGetProbability(s)); }
public SppfNode ParseGetForest(Sentence s) { var earley = new Parsers.Earley.EarleyParser(this); return(earley.ParseGetForest(s)); }
/// <summary> /// Returns all the sentences (with their probabilities) that can be generated up to a certain depth /// </summary> /// <returns></returns> public List <Probable <Sentence> > ProduceToDepth(int depth, int limit = int.MaxValue) { var start = new Sentence { this.Start }; var intermediate = new List <Probable <Sentence> > [depth + 1]; var startSWP = new Probable <Sentence>(1.0, start); intermediate[0] = new List <Probable <Sentence> > { startSWP }; int count = 0; for (int i = 0; i < depth; i++) { if (count >= limit) { break; } var prev = intermediate[i]; var next = new List <Probable <Sentence> >(); intermediate[i + 1] = next; foreach (var swp in prev) { if (!swp.Value.OnlyTerminals()) { var steps = GoOneStep(swp, depth - i, limit); if (steps.Count == 0) { count = limit; } next.AddRange(steps); count += steps.Count; } if (count >= limit) { break; } } } var results = new List <Probable <Sentence> >(); // TODO: terrible :( var resultDict = new Dictionary <string, Probable <Sentence> >(); foreach (var step in intermediate) { if (step == null) { continue; } foreach (var swp in step) { if (!swp.Value.OnlyTerminals()) { continue; } Probable <Sentence> psentence; var stringrep = swp.Value.ToString(); if (!resultDict.TryGetValue(stringrep, out psentence)) { psentence = new Probable <Sentence>(0.0, swp.Value); resultDict[stringrep] = psentence; } psentence.Probability += swp.Probability; // results.Add(swp); } } return(resultDict.Values.ToList()); }
/// <summary> /// Append all the words of the given sentence to the end of this sentence /// </summary> /// <param name="collection"></param> public void AddRange(Sentence collection) { _sentence.AddRange(collection); }
/// <summary> /// Returns whether this grammar accepts the given sentence /// </summary> /// <param name="s"></param> /// <returns></returns> public bool Accepts(Sentence s) { return(Cyk(s) > 0); }
public double Cyk(Sentence s) { var cyk = new Parsers.CYK.CykParser(this); return(cyk.ParseGetProbability(s)); }
public Production(string lhsName, Sentence rhs, double weight = 1.0) : this(Nonterminal.Of(lhsName), rhs, weight) { }