private WordGraphConfidences ComputeWordGraphConfidences(WordGraph wordGraph)
        {
            double normalizationFactor = LogSpace.Zero;
            var    backwardProbs       = new double[wordGraph.Arcs.Count];

            for (int i = wordGraph.Arcs.Count - 1; i >= 0; i--)
            {
                WordGraphArc arc = wordGraph.Arcs[i];
                double       sum = LogSpace.One;
                if (!wordGraph.FinalStates.Contains(arc.NextState))
                {
                    sum = LogSpace.Zero;
                    foreach (int nextArcIndex in wordGraph.GetNextArcIndices(arc.NextState))
                    {
                        WordGraphArc nextArc = wordGraph.Arcs[nextArcIndex];
                        sum = LogSpace.Add(sum, LogSpace.Multiple(nextArc.Score, backwardProbs[nextArcIndex]));
                    }
                }
                backwardProbs[i] = sum;
                if (arc.PrevState == WordGraph.InitialState)
                {
                    normalizationFactor = LogSpace.Add(normalizationFactor,
                                                       LogSpace.Multiple(arc.Score, backwardProbs[i]));
                }
            }

            var rawWpps      = new Dictionary <string, Dictionary <int, double> >();
            var forwardProbs = new (double Prob, int Index)[wordGraph.Arcs.Count];
Beispiel #2
0
        private void ComputePrevScores(int state, out double[] prevScores, out int[] stateBestPrevArcs)
        {
            if (IsEmpty)
            {
                prevScores        = new double[0];
                stateBestPrevArcs = new int[0];
                return;
            }

            prevScores        = Enumerable.Repeat(LogSpace.Zero, StateCount).ToArray();
            stateBestPrevArcs = new int[StateCount];

            if (state == InitialState)
            {
                prevScores[InitialState] = InitialStateScore;
            }
            else
            {
                prevScores[state] = 0;
            }

            var accessibleStates = new HashSet <int> {
                state
            };

            for (int arcIndex = 0; arcIndex < Arcs.Count; arcIndex++)
            {
                WordGraphArc arc = Arcs[arcIndex];
                if (accessibleStates.Contains(arc.PrevState))
                {
                    double score = LogSpace.Multiple(arc.Score, prevScores[arc.PrevState]);
                    if (score > prevScores[arc.NextState])
                    {
                        prevScores[arc.NextState]        = score;
                        stateBestPrevArcs[arc.NextState] = arcIndex;
                    }
                    accessibleStates.Add(arc.NextState);
                }
                else
                {
                    if (!accessibleStates.Contains(arc.NextState))
                    {
                        prevScores[arc.NextState] = LogSpace.Zero;
                    }
                }
            }
        }
Beispiel #3
0
        public IEnumerable <double> ComputeRestScores()
        {
            double[] restScores = Enumerable.Repeat(LogSpace.Zero, StateCount).ToArray();

            foreach (int state in _finalStates)
            {
                restScores[state] = InitialStateScore;
            }

            for (int i = Arcs.Count - 1; i >= 0; i--)
            {
                WordGraphArc arc   = Arcs[i];
                double       score = LogSpace.Multiple(arc.Score, restScores[arc.NextState]);
                if (score > restScores[arc.PrevState])
                {
                    restScores[arc.PrevState] = score;
                }
            }

            return(restScores);
        }