Exemple #1
0
            /// <summary>
            /// Adds all leaving arcs, including 'finished' arc, if
            /// the node is final, from this node into the queue.
            /// </summary>
            public virtual void AddStartPaths(FST.Arc <T> node, T startOutput, bool allowEmptyString, Int32sRef input)
            {
                // De-dup NO_OUTPUT since it must be a singleton:
                if (startOutput.Equals(fst.Outputs.NoOutput))
                {
                    startOutput = fst.Outputs.NoOutput;
                }

                FSTPath <T> path = new FSTPath <T>(startOutput, node, input);

                fst.ReadFirstTargetArc(node, path.Arc, bytesReader);

                //System.out.println("add start paths");

                // Bootstrap: find the min starting arc
                while (true)
                {
                    if (allowEmptyString || path.Arc.Label != FST.END_LABEL)
                    {
                        AddIfCompetitive(path);
                    }
                    if (path.Arc.IsLast)
                    {
                        break;
                    }
                    fst.ReadNextArc(path.Arc, bytesReader);
                }
            }
Exemple #2
0
        /// <summary>
        /// Cache the root node's output arcs starting with completions with the
        /// highest weights.
        /// </summary>
        private static FST.Arc <object>[] CacheRootArcs(FST <object> automaton)
        {
            try
            {
                IList <FST.Arc <object> > rootArcs  = new List <FST.Arc <object> >();
                FST.Arc <object>          arc       = automaton.GetFirstArc(new FST.Arc <object>());
                FST.BytesReader           fstReader = automaton.BytesReader;
                automaton.ReadFirstTargetArc(arc, arc, fstReader);
                while (true)
                {
                    rootArcs.Add((new FST.Arc <object>()).CopyFrom(arc));
                    if (arc.IsLast)
                    {
                        break;
                    }
                    automaton.ReadNextArc(arc, fstReader);
                }

                // we want highest weights first.
                return(rootArcs.Reverse().ToArray());
            }
            catch (IOException e)
            {
                throw new Exception(e.Message, e);
            }
        }
Exemple #3
0
        /// <summary>
        /// Cache the root node's output arcs starting with completions with the
        /// highest weights.
        /// </summary>
        private static FST.Arc <object>[] CacheRootArcs(FST <object> automaton)
        {
            try
            {
                // LUCENENET specific: Using a stack rather than List, as we want the results in reverse
                Stack <FST.Arc <object> > rootArcs  = new Stack <FST.Arc <object> >();
                FST.Arc <object>          arc       = automaton.GetFirstArc(new FST.Arc <object>());
                FST.BytesReader           fstReader = automaton.GetBytesReader();
                automaton.ReadFirstTargetArc(arc, arc, fstReader);
                while (true)
                {
                    rootArcs.Push(new FST.Arc <object>().CopyFrom(arc));
                    if (arc.IsLast)
                    {
                        break;
                    }
                    automaton.ReadNextArc(arc, fstReader);
                }

                // we want highest weights first.
                return(rootArcs.ToArray());
            }
            catch (Exception e) when(e.IsIOException())
            {
                throw RuntimeException.Create(e);
            }
        }
Exemple #4
0
        protected virtual void DoNext()
        {
            //System.out.println("FE: next upto=" + upto);
            if (m_upto == 0)
            {
                //System.out.println("  init");
                m_upto = 1;
                m_fst.ReadFirstTargetArc(GetArc(0), GetArc(1), m_fstReader);
            }
            else
            {
                // pop
                //System.out.println("  check pop curArc target=" + arcs[upto].target + " label=" + arcs[upto].label + " isLast?=" + arcs[upto].isLast());
                while (m_arcs[m_upto].IsLast)
                {
                    m_upto--;
                    if (m_upto == 0)
                    {
                        //System.out.println("  eof");
                        return;
                    }
                }
                m_fst.ReadNextArc(m_arcs[m_upto], m_fstReader);
            }

            PushFirst();
        }
Exemple #5
0
        protected internal virtual void DoNext()
        {
            //System.out.println("FE: next upto=" + upto);
            if (Upto == 0)
            {
                //System.out.println("  init");
                Upto = 1;
                Fst.ReadFirstTargetArc(GetArc(0), GetArc(1), FstReader);
            }
            else
            {
                // pop
                //System.out.println("  check pop curArc target=" + arcs[upto].target + " label=" + arcs[upto].label + " isLast?=" + arcs[upto].isLast());
                while (Arcs[Upto].Last)
                {
                    Upto--;
                    if (Upto == 0)
                    {
                        //System.out.println("  eof");
                        return;
                    }
                }
                Fst.ReadNextArc(Arcs[Upto], FstReader);
            }

            PushFirst();
        }
Exemple #6
0
        private T RandomAcceptedWord(FST <T> fst, IntsRef @in)
        {
            FST.Arc <T> arc = fst.GetFirstArc(new FST.Arc <T>());

            IList <FST.Arc <T> > arcs = new List <FST.Arc <T> >();

            @in.Length = 0;
            @in.Offset = 0;
            T NO_OUTPUT = fst.Outputs.NoOutput;
            T output    = NO_OUTPUT;

            FST.BytesReader fstReader = fst.BytesReader;

            while (true)
            {
                // read all arcs:
                fst.ReadFirstTargetArc(arc, arc, fstReader);
                arcs.Add((new FST.Arc <T>()).CopyFrom(arc));
                while (!arc.Last)
                {
                    fst.ReadNextArc(arc, fstReader);
                    arcs.Add((new FST.Arc <T>()).CopyFrom(arc));
                }

                // pick one
                arc = arcs[Random.Next(arcs.Count)];
                arcs.Clear();

                // accumulate output
                output = fst.Outputs.Add(output, arc.Output);

                // append label
                if (arc.Label == FST <T> .END_LABEL)
                {
                    break;
                }

                if (@in.Ints.Length == @in.Length)
                {
                    @in.Grow(1 + @in.Length);
                }
                @in.Ints[@in.Length++] = arc.Label;
            }

            return(output);
        }
Exemple #7
0
        /// <summary>
        /// Recursive collect lookup results from the automaton subgraph starting at
        /// <paramref name="arc"/>.
        /// </summary>
        /// <param name="num">
        ///          Maximum number of results needed (early termination). </param>
        private bool Collect(IList <Completion> res, int num, int bucket, BytesRef output, FST.Arc <object> arc)
        {
            if (output.Length == output.Bytes.Length)
            {
                output.Bytes = ArrayUtil.Grow(output.Bytes);
            }
            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(output.Offset == 0);
            }
            output.Bytes[output.Length++] = (byte)arc.Label;
            FST.BytesReader fstReader = automaton.GetBytesReader();
            automaton.ReadFirstTargetArc(arc, arc, fstReader);
            while (true)
            {
                if (arc.Label == Lucene.Net.Util.Fst.FST.END_LABEL)
                {
                    res.Add(new Completion(output, bucket));
                    if (res.Count >= num)
                    {
                        return(true);
                    }
                }
                else
                {
                    int save = output.Length;
                    if (Collect(res, num, bucket, output, (new FST.Arc <object>()).CopyFrom(arc)))
                    {
                        return(true);
                    }
                    output.Length = save;
                }

                if (arc.IsLast)
                {
                    break;
                }
                automaton.ReadNextArc(arc, fstReader);
            }
            return(false);
        }