Ejemplo n.º 1
0
        /// <summary>
        /// Parse given input string in the concrete grammar.
        /// </summary>
        /// <param name="str">Input string to be parsed.</param>
        /// <param name="cat">Category (Type) to parse.</param>
        /// <param name="heuristics">Heuristic (see the GF C runtime docs).</param>
        /// <param name="Callback1">Callback function.</param>
        /// <param name="Callback2">Callback function.</param>
        /// <returns>Enumerates pairs of (abstract grammar) expressions and corresponding probability.</returns>
        public IEnumerable <Tuple <Expr, float> > Parse(string str, Type cat = null, double?heuristics = null,
                                                        Action Callback1     = null, Action Callback2  = null)
        {
            var parse_pool = new NativeGU.NativeMemoryPool();
            var exn        = new NativeGU.NativeExceptionContext(parse_pool);

            cat = cat ?? PGF.StartCat;

            using (var nativeStr = new Native.NativeString(str))
            {
                var result_pool = new NativeGU.NativeMemoryPool();
                var callbackMap = Native.pgf_new_callbacks_map(this.Ptr, parse_pool.Ptr);

                var iterator = Native.pgf_parse_with_heuristics(this.Ptr, cat.Ptr,
                                                                nativeStr.Ptr, heuristics ?? -1, callbackMap,
                                                                exn.Ptr, parse_pool.Ptr, result_pool.Ptr);

                if (iterator == IntPtr.Zero || exn.IsRaised)
                {
                    throw new ParseError();
                }
                else
                {
                    foreach (var ptr in NativeGU.IteratorToIEnumerable(iterator, parse_pool.Ptr))
                    {
                        var exprProb = (Native.PgfExprProb)Marshal.PtrToStructure(ptr, typeof(Native.PgfExprProb));
                        yield return(Tuple.Create(Expr.FromPtr(exprProb.expr, result_pool),
                                                  exprProb.prob));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get all possible linearization for an expression (see <see cref="Linearize(Expression)"/>).
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public IEnumerable <string> LinearizeAll(Expr e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var cts = Native.pgf_lzr_concretize(Ptr, e.Ptr, exn.Ptr, tmp_pool.Ptr);

            if (exn.IsRaised || cts == IntPtr.Zero)
            {
                throw new PGFError("Could not linearize the abstract tree.");
            }

            return(NativeGU.IteratorToIEnumerable(cts, tmp_pool.Ptr).Select(LinearizeCnc));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns an enumerable over the set of all expression in
        /// the given category. The expressions are enumerated in decreasing
        /// probability order.
        /// </summary>
        /// <param name="cat">the start category.</param>
        /// <returns></returns>
        public IEnumerable <Expr> GenerateAll(Type cat = null)
        {
            cat = cat ?? StartCat;
            var    tmp_pool    = new NativeGU.NativeMemoryPool();
            var    exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var    result_pool = new NativeGU.NativeMemoryPool();
            IntPtr ptr         = IntPtr.Zero;
            var    iterator    = Native.pgf_generate_all(this._ptr, cat.Ptr, exn.Ptr, tmp_pool.Ptr, result_pool.Ptr);

            return(NativeGU.IteratorToIEnumerable(iterator, tmp_pool.Ptr).Select(p =>
            {
                var exprProb = Marshal.PtrToStructure <Native.PgfExprProb>(ptr);
                return Expr.FromPtr(exprProb.expr, result_pool);
            }));
        }