Example #1
0
        public Words Convert(WordGraph.T wg)
        {
            RunViterbiFwd(wg);
            var words = RunViterbiBwd(wg);

            return(words);
        }
Example #2
0
        // calculate node probs and set prev flags
        void RunViterbiFwd(WordGraph.T wg)
        {
            foreach (var p in wg.EndLocMap.Skip(1) /* skips BOS */)
            {
                var endIdx = p.Key;

                foreach (var nd in p.Value)
                {
                    var wordLen = nd.Word.Chars == Const.EOS ? 1 : nd.Word.Reading.Length;
                    var begIdx  = endIdx - wordLen + 1;
                    var prevs   = wg.EndLocMap[begIdx - 1];

                    // find prev node w/ the best value
                    WordGraph.Node bestNd  = null;
                    double         bestVal = double.MinValue;

                    foreach (var prev in prevs)
                    {
                        var curr = nd;
                        var prob = CalcProb(curr.Word, prev.Word);
                        var val  = prev.Val + prob;
                        if (val > bestVal)
                        {
                            bestVal = val;
                            bestNd  = prev;
                        }
                    }
                    nd.Prev = bestNd;
                    nd.Val  = bestVal;
                }
            }
        }
Example #3
0
        Words RunViterbiBwd(WordGraph.T wg)
        {
            var nd = wg.Eos;
            var xs = new LinkedList <Word>();

            while (nd != wg.Bos)
            {
                xs.AddFirst(nd.Word);
                nd = nd.Prev;
            }
            xs.RemoveLast();
            return(new Words {
                Xs = xs.ToArray()
            });
        }
Example #4
0
 public Result <Words> ConvertByBigram(WordGraph.T wg)
 {
     return(Try(() => bigram.Convert(wg)));
 }