예제 #1
0
        private SppfNode ConstructInternalSppf(IEnumerable <Item> successes, Sentence s)
        {
            // var root = new SymbolNode(Grammar.Start, 0, s.Count);
            var root      = new SppfWord(Grammar.Start, 0, s.Count);
            var processed = new HashSet <Item>();
            var nodes     = new Dictionary <SppfNode, SppfNode>();

            nodes[root] = root;

            foreach (var success in successes)
            {
                BuildTree(nodes, processed, root, success);
            }

            return(root);
        }
예제 #2
0
        private void ProcessQ(int i)
        {
            if (i < _a.Count)
            {
                // create an SPPF node v labelled(a_i, i, i + 1)
                var v2 = new SppfWord(_a[i], i, i + 1);

                // while Q ̸= ∅ {
                while (!_Q.IsEmpty)
                {
                    // remove an element, Λ = (B ::= α · a_i β, h, w) say, from Q
                    var Λ = _Q.TakeOne();
                    if (Λ.DecoratedProduction.NextWord != _a[i])
                    {
                        throw new Exception();
                        // continue;
                    }
                    var h  = Λ.StartPosition;
                    var w  = Λ.SppfNode;
                    var β0 = Λ.DecoratedProduction.TailFirst;

                    // let y = MAKE NODE(B ::= α a_i · β, h, i + 1, w, v, V)
                    var productionAdvanced = Λ.DecoratedProduction.Increment();
                    var y = MakeNode(productionAdvanced, h, i + 1, w, v2);

                    var newItem = new EarleyItem(productionAdvanced, h, y);
                    // if β ∈ Σ_N { add (B ::= α a_i · β, h, y) to E_i+1 }
                    if (PrefixInSigma(β0))
                    {
                        _E[i + 1].Add(newItem);
                    }

                    // if β = a_i+1 β′ { add (B ::= α a_i · β, h, y) to Q′ }
                    else
                    {
                        if (i + 1 < _a.Count)
                        {
                            var aNext = _a[i + 1];
                            if (β0 == aNext)
                            {
                                _QPrime.Add(newItem);
                            }
                        }
                    }
                }
            }
        }
예제 #3
0
        internal SppfWord GetOrSet(Word item, int j, int i)
        {
            SppfWord y;
            var      dict = _wordDicts[j];

            if (!dict.TryGetValue(item, out y))
            {
                var newY = new SppfWord(item, j, i);
                dict[item] = newY;
                y          = newY;
            }
            if (i != y.EndPosition)
            {
                throw new Exception(string.Format("Invalid assumption; need to include {0} in hash", nameof(i)));
            }
            return(y);
        }