示例#1
0
        /// <summary>n
        /// Recognizes a collection of Polyline objects into text. Words are recognized with alternatives, eash weighted with probability.
        /// </summary>
        /// <param name="strokes">Strokes to recognize</param>
        /// <returns></returns>
        public string RecognizeStrokes(List <List <Point> > strokes, bool bLearn)
        {
            WritePadAPI.HWR_Reset(WritePadAPI.getRecoHandle());

            if (InkData != IntPtr.Zero)
            {
                WritePadAPI.INK_Erase(InkData);
                InkData = IntPtr.Zero;
            }
            InkData = WritePadAPI.INK_InitData();

            foreach (var polyline in strokes)
            {
                WritePadAPI.AddStroke(InkData, polyline);
            }

            var res = "";
            var resultStringList = new List <string>();
            var wordList         = new List <List <WordAlternative> >();
            var defaultResultPtr = WritePadAPI.recognizeInkData(InkData, 0);
            var defaultResult    = Marshal.PtrToStringUni(defaultResultPtr);

            resultStringList.Add(defaultResult);
            var wordCount = WritePadAPI.HWR_GetResultWordCount(WritePadAPI.getRecoHandle());

            for (var i = 0; i < wordCount; i++)
            {
                var wordAlternativesList = new List <WordAlternative>();
                var altCount             = WritePadAPI.HWR_GetResultAlternativeCount(WritePadAPI.getRecoHandle(), i);
                for (var j = 0; j < altCount; j++)
                {
                    var wordPtr = WritePadAPI.getResultWord(i, j);
                    var word    = Marshal.PtrToStringUni(wordPtr);
                    if (word == "<--->")
                    {
                        word = "*Error*";
                    }
                    if (string.IsNullOrEmpty(word))
                    {
                        continue;
                    }
                    uint flags  = WritePadAPI.HWR_GetRecognitionFlags(WritePadAPI.getRecoHandle());
                    var  weight = WritePadAPI.HWR_GetResultWeight(WritePadAPI.getRecoHandle(), i, j);
                    if (weight == 0)
                    {
                        continue;
                    }
                    if (j == 0 && bLearn && weight > 75 && 0 != (flags & WritePadAPI.FLAG_ANALYZER))
                    {
                        // if learner is enabled, learn default word(s) when the Return gesture is used
                        WritePadAPI.recoLearnWord(word, weight);
                    }
                    if (wordAlternativesList.All(x => x.Word != word))
                    {
                        wordAlternativesList.Add(new WordAlternative
                        {
                            Word   = word,
                            Weight = weight
                        }
                                                 );
                    }
                    while (resultStringList.Count < j + 2)
                    {
                        var emptyStr = "";
                        for (int k = 0; k < i; k++)
                        {
                            emptyStr += "\t";
                        }
                        resultStringList.Add(emptyStr);
                    }
                    if (resultStringList[j + 1].Length > 0)
                    {
                        resultStringList[j + 1] += "\t\t";
                    }
                    resultStringList[j + 1] += word + "\t[" + weight + "%]";
                }
                wordList.Add(wordAlternativesList);
            }

            foreach (var line in resultStringList)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                if (res.Length > 0)
                {
                    res += Environment.NewLine;
                }
                res += line;
            }

            return(res);
        }