Ejemplo n.º 1
0
        private void theInkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
        {
            var stroke = GetCombinedStore(this.theInkCanvas.Strokes);

            InkAnalyzer theInkAnalyzer = new InkAnalyzer();

            //theInkAnalyzer.AddStrokes(this.theInkCanvas.Strokes, 0x0804);
            //theInkAnalyzer.SetStrokesType(this.theInkCanvas.Strokes, StrokeType.Writing);

            theInkAnalyzer.AddStroke(stroke, 0x0804);
            theInkAnalyzer.SetStrokeType(stroke, StrokeType.Writing);
            AnalysisStatus status = theInkAnalyzer.Analyze();

            if (status.Successful)
            {
                List <StrokeWord> slist = new List <StrokeWord>();
                //textBox1.Text = theInkAnalyzer.GetRecognizedString();
                for (int i = 0; i < theInkAnalyzer.GetAlternates().Count; i++)
                {
                    StrokeWord sw = new StrokeWord();
                    sw.StrokeName = theInkAnalyzer.GetAlternates()[i].RecognizedString;
                    slist.Add(sw);
                }

                this.StrokeWordList = new List <StrokeWord>();
                this.StrokeWordList = slist;
            }
            else
            {
                MessageBox.Show("识别失败");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 识别
        /// </summary>
        /// <param name="strokes">笔迹集合</param>
        /// <returns>候选词数组</returns>
        public string[] Recognize(StrokeCollection strokes)
        {
            if (strokes == null || strokes.Count == 0)
            {
                return(Params.EmptyAlternates);
            }

            var stroke = GetCombinedStore(strokes);

            var analyzer = new InkAnalyzer();

            analyzer.AddStroke(stroke, Params.SimplifiedChineseLanguageId);
            analyzer.SetStrokeType(stroke, StrokeType.Writing);

            var status = analyzer.Analyze();

            if (status.Successful)
            {
                var result = analyzer.GetAlternates()
                             .OfType <AnalysisAlternate>()
                             .Select(x => x.RecognizedString)
                             .ToArray();
                analyzer.Dispose();
                return(result);
            }

            analyzer.Dispose();

            return(Params.EmptyAlternates);
        }
        /// <summary>
        /// 识别
        /// </summary>
        /// <param name="strokes">笔迹集合</param>
        /// <returns>候选词数组</returns>
        public string[] Recognize(StrokeCollection strokes)
        {
            if (strokes == null || strokes.Count == 0)
            {
                return(Constants.EmptyAlternates);
            }

            var analyzer = new InkAnalyzer();

            analyzer.AddStrokes(strokes, Constants.ChsLanguageId);
            analyzer.SetStrokesType(strokes, StrokeType.Writing);

            var status = analyzer.Analyze();

            if (status.Successful)
            {
                return(analyzer.GetAlternates()
                       .OfType <AnalysisAlternate>()
                       .Select(x => x.RecognizedString)
                       .ToArray());
            }

            analyzer.Dispose();

            return(Constants.EmptyAlternates);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 识别(将多个笔画集合成一起识别,提高单字的识别率)
        /// </summary>
        /// <param name="strokes">笔迹集合</param>
        /// <returns>候选词数组</returns>
        public string[] Recognize(StrokeCollection strokes)
        {
            if (strokes == null || strokes.Count == 0)
            {
                return(null);
            }

            var stroke = GetCombinedStore(strokes);

            var analyzer = new InkAnalyzer();

            analyzer.AddStroke(stroke, 0x0804);
            analyzer.SetStrokeType(stroke, StrokeType.Writing);

            var status = analyzer.Analyze();

            if (status.Successful)
            {
                return(analyzer.GetAlternates()
                       .OfType <AnalysisAlternate>()
                       .Select(x => x.RecognizedString)
                       .ToArray());
            }

            analyzer.Dispose();

            return(null);
        }
Ejemplo n.º 5
0
        private void btnAnalyze_Click(object sender, RoutedEventArgs e)
        {
            InkAnalyzer theInkAnalyzer = new InkAnalyzer();

            // キャンバスに描かれた文字を認識するためにアナライザにストロークをセット
            theInkAnalyzer.AddStrokes(InkCanvas1.Strokes);
            theInkAnalyzer.SetStrokesType(InkCanvas1.Strokes, StrokeType.Writing);

            // 文字を解析
            theInkAnalyzer.Analyze();

            // 文字を解析した結果の第1候補を表示する
            MessageBox.Show(theInkAnalyzer.GetRecognizedString());

            // その他の候補を表示する
            AnalysisAlternateCollection alternates = theInkAnalyzer.GetAlternates();

            foreach (var alternate in alternates)
            {
                MessageBox.Show(alternate.RecognizedString);
            }
        }
Ejemplo n.º 6
0
        public void Populate(ICollection <Recognition> recogs, StroqCollection stroqs)
        {
            alternateRec = recogs.Count == 1 ? recogs.Single() : null;
            if (alternateRec != null && !stroqs.All((s) => alternateRec.strokes.Contains(_mrec.Sim[s])))
            {
                alternateRec = null;
            }
            altstroqsRec = stroqs;
            altrecogsRec = recogs;

            _menuShell.Items.Clear();

            MenuItem mi;
            bool     needseparator = false;

            if (recogs.Count == 1)
            {
                /* regular alternates*/
                Recognition rr = recogs.Single();
                for (int i = 0; i < rr.alts.Length; i++)
                {
                    string label;
                    char   c = rr.alts[i].Character;
                    if (c != 0)
                    {
                        label = c.ToString();
                    }
                    else
                    {
                        label = rr.alts[i].Word;
                        if (label == null)
                        {
                            label = rr.alts[i].ToString();
                            label = label.Substring(1, label.Length - 2);
                        }
                    }
                    mi        = new MenuItem();
                    mi.Header = label;
                    if (c != 0)
                    {
                        mi.ToolTip    = Unicode.NameOf(c);
                        mi.FontFamily = new FontFamily(starPadSDK.MathExpr.ExprWPF.EDrawingContext.FontFamilyURIBase,
                                                       starPadSDK.MathExpr.ExprWPF.EDrawingContext.FontFamilyURIRel);
                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= 'α' && c <= 'ω') || c == Unicode.G.GREEK_PHI_SYMBOL)
                        {
                            mi.FontStyle = FontStyles.Italic;
                        }
                    }
                    else
                    {
                        mi.ToolTip = label;
                    }
                    mi.Tag               = i;
                    mi.Click            += ChooseAlternate;
                    mi.PreviewMouseDown += mi_MouseDown;
                    mi.PreviewMouseUp   += mi_MouseUp;
                    if (i == 0)
                    {
                        mi.AllowDrop  = true;
                        mi.DragEnter += mi_DragCheck;
                        mi.DragOver  += mi_DragCheck;
                        mi.Drop      += mi_Drop;
                    }
                    if (rr.curalt == i)
                    {
                        mi.IsChecked = true;
                    }
                    _menuShell.Items.Add(mi);
                }
                needseparator = true;
            }
            if (stroqs.Count > 1 && stroqs.Count != recogs.Count /* FIXME: if a stroke has no recog, this won't work */)
            {
                /* option to split apart and recognize each stroke separately */
                if (needseparator)
                {
                    _menuShell.Items.Add(new Separator());
                    needseparator = false;
                }
                string label = "";
                foreach (Stroq s1 in stroqs)
                {
                    Recognition r = _mrec.Charreco.Classify(_mrec.Sim[s1], true);
                    if (r == null)
                    {
                        continue;
                    }
                    string l;
                    char   c = r.alt.Character;
                    if (c != 0)
                    {
                        l = c.ToString();
                    }
                    else
                    {
                        l = r.alt.Word;
                        if (l == null)
                        {
                            l = r.alt.ToString();
                            l = l.Substring(1, l.Length - 2);
                        }
                    }
                    label += l;
                }
                mi         = new MenuItem();
                mi.Header  = label;
                mi.ToolTip = "split combined symbol into separate symbols";
                mi.Tag     = -1;
                mi.Click  += ChooseAlternate;
                _menuShell.Items.Add(mi);
                needseparator = true;
            }
            if (stroqs.Count > 0)
            {
                /* Interpret everything as a single word */
                if (needseparator)
                {
                    _menuShell.Items.Add(new Separator());
                    needseparator = false;
                }
                InkAnalyzer      ia  = new InkAnalyzer();
                AnalysisHintNode ahn = ia.CreateAnalysisHint();
                ahn.WordMode = true;
                ahn.Location.MakeInfinite();
                foreach (Stroq s in stroqs)
                {
                    ia.AddStroke(s.BackingStroke);
                }
                AnalysisStatus stat = ia.Analyze();
                if (stat.Successful)
                {
                    AnalysisAlternateCollection aac = ia.GetAlternates();
                    for (int i = 0; i < aac.Count; i++)
                    {
                        if (aac[i].AlternateNodes.Count > 1 || !(aac[i].AlternateNodes[0] is InkWordNode))
                        {
                            continue;
                        }
                        mi         = new MenuItem();
                        mi.Header  = aac[i].RecognizedString;
                        mi.ToolTip = "interpret all selected strokes as a single character or word: alternate " + (i + 1);
                        mi.Tag     = aac[i];
                        mi.Click  += ChooseAlternate;
                        if (alternateRec != null)
                        {
                            mi.PreviewMouseDown += mi_MouseDown;
                            mi.PreviewMouseUp   += mi_MouseUp;
                        }
                        if (aac[i].RecognizedString.Length == 1)
                        {
                            char c = aac[i].RecognizedString[0];
                            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= 'α' && c <= 'ω'))
                            {
                                mi.FontStyle = FontStyles.Italic;
                            }
                            mi.ToolTip = (string)mi.ToolTip + " (" + Unicode.NameOf(c) + ")";
                        }
                        _menuShell.Items.Add(mi);
                    }
                }
            }
            _menuShell.InvalidateMeasure(); // odd that I need this
        }