Пример #1
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);
            }
        }
Пример #2
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
        }