Пример #1
0
        private List <Probable <Sentence> > GoOneStep(Probable <Sentence> swp, int remainingDepth, int limit = int.MaxValue)
        {
            var start   = new Probable <Sentence>(swp.Probability, new Sentence());
            var results = new List <Probable <Sentence> > {
                start
            };

            foreach (var word in swp.Value)
            {
                if (results.Count > limit)
                {
                    // have to return empty here, since we don't have complete sentences
                    return(new List <Probable <Sentence> >());
                }
                if (word.IsNonterminal)
                {
                    results = StepNonterminal(results, word, remainingDepth);
                }
                else
                {
                    results = StepTerminal(results, word);
                }
            }
            return(results);
        }
Пример #2
0
        /// <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());
        }