예제 #1
0
        private string[] Regconize()         // REM 识别 ptinfos 中的点
        {
            List <System.Drawing.Point> listpts = new List <System.Drawing.Point>();

            foreach (var i in ptinfos)
            {
                listpts.Add(i.ToDrawingPoint());
            }
            // REM 每一笔转换成stroke
            Stroke stroke = ink.CreateStroke(listpts.ToArray());

            // REM 添加到识别器的上下文
            recognizectx.Strokes.Add(stroke);
            RecognitionStatus recognizestatus = new RecognitionStatus();
            RecognitionResult recognizeresult = recognizectx.Recognize(out recognizestatus);
            // REM 识别器的所有选择
            RecognitionAlternates recognizealternates = recognizeresult.GetAlternatesFromSelection();
            // REM 列出识别器所识别出的内容
            List <string> result = new List <string>();

            for (var i = 0; i <= recognizealternates.Count - 1; i++)
            {
                string text = recognizealternates[i].ToString();
                // Console.WriteLine(text)
                result.Add(text);
            }
            return(result.ToArray());
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="strokes"></param>
        internal String recognize(Strokes strokes, out RecognitionAlternates alternates)
        {
            RecognizerContext rc = new RecognizerContext();

            rc.Strokes = strokes;

            try {
                RecognitionStatus status;
                RecognitionResult result;
                result = rc.Recognize(out status);
                if (status == RecognitionStatus.NoError)
                {
                    RecognitionAlternates alternatives = result.GetAlternatesFromSelection();
                    alternates = alternatives;
                    return(result.TopString);
                }
                else
                {
                    Console.WriteLine("Error in recognition.");
                }
            }
            catch {
                Console.WriteLine("Exception in recognition.");
            }
            alternates = null;
            return(null);
        }
예제 #3
0
 // Listbox code to place all the possible results into the list
 private void textBox1_MouseUp(object sender, MouseEventArgs e)
 {
     listBox1.Items.Clear();
     if (textBox1.SelectionLength > 0)
     {
         RecognitionAlternates alternates = recognitionResult.GetAlternatesFromSelection(textBox1.SelectionStart, textBox1.SelectionLength);
         foreach (RecognitionAlternate alternate in alternates)
         {
             listBox1.Items.Add(alternate);
         }
         //inkOverlay.Selection = alternates.Strokes;
     }
 }
예제 #4
0
        private void rct_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs e)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.Invoke(DispatcherPriority.Send, new RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternates), sender, e);

                return;
            }


            if (RecognitionStatus.NoError == e.RecognitionStatus)
            {
                this.panelChoose.Children.Clear();
                RecognitionAlternates allAlternates = e.Result.GetAlternatesFromSelection();
                // show each of the other alternates
                int    i       = 1;
                double width   = 56;
                double height  = 56;
                bool   isFirst = true;
                foreach (RecognitionAlternate oneAlternate in allAlternates)
                {
                    Label lbl = new Label();
                    lbl.Name     = "lbl" + i;
                    lbl.Tag      = oneAlternate.ToString();
                    lbl.Content  = lbl.Tag.ToString();
                    lbl.Width    = width;
                    lbl.Height   = height;
                    lbl.FontSize = 24F;
                    if (isFirst)
                    {
                        lbl.Width    = 172;
                        lbl.Height   = 172;
                        lbl.FontSize = 35F;
                        isFirst      = false;
                    }
                    lbl.FontFamily                 = new System.Windows.Media.FontFamily("微软雅黑");
                    lbl.BorderThickness            = new Thickness(1);
                    lbl.Margin                     = new Thickness(1, 1, 1, 1);
                    lbl.HorizontalContentAlignment = HorizontalAlignment.Center;
                    lbl.VerticalContentAlignment   = VerticalAlignment.Center;
                    lbl.BorderBrush                = border.BorderBrush;// System.Windows.Media.Brushes.Gray;
                    lbl.MouseLeftButtonDown       += new System.Windows.Input.MouseButtonEventHandler(lbl_MouseLeftButtonDown);
                    this.panelChoose.Children.Add(lbl);
                    ++i;
                }
            }
        }
예제 #5
0
        public String Recognizer(String strokesStr, int count)
        {
            List <List <int[]> > strokes = new List <List <int[]> >();
            var array = Regex.Split(strokesStr, ",eb,");

            foreach (var item in array)
            {
                var stroke = new List <int[]>();
                var array2 = item.Split(',');
                for (var i = 0; i < array2.Length; i = i + 2)
                {
                    int[] point = new int[2];
                    point[0] = int.Parse(array2[i]);
                    point[1] = int.Parse(array2[i + 1]);
                    stroke.Add(point);
                }
                strokes.Add(stroke);
            }

            RecognizerContext recognizerContext = new Recognizers().GetDefaultRecognizer().CreateRecognizerContext();
            Ink ink = new Ink();

            recognizerContext.Strokes = ink.CreateStrokes();
            foreach (List <int[]> stroke in strokes)
            {
                Point[] points = new Point[stroke.Count];
                for (int i = 0; i < stroke.Count; i++)
                {
                    points[i] = new Point(stroke[i][0], stroke[i][1]);
                }
                recognizerContext.Strokes.Add(ink.CreateStroke(points));
            }
            RecognitionStatus recognitionStatus = RecognitionStatus.NoError;
            RecognitionResult recognitionResult = recognizerContext.Recognize(out recognitionStatus);
            var text = "";

            if (recognitionStatus == RecognitionStatus.NoError)
            {
                RecognitionAlternates alts = recognitionResult.GetAlternatesFromSelection();
                for (int i = 0; i < alts.Count && i < count; i++)
                {
                    RecognitionAlternate alt = alts[i];
                    text += alt.ToString() + " ";
                }
            }
            return(text.Trim());
        }
예제 #6
0
        private string[] recognize()
        {
            List <string> results = new List <string>(10);

            this.recognizerContext.Strokes = this.ink.Strokes;
            RecognitionStatus status;
            RecognitionResult result = this.recognizerContext.Recognize(out status);

            if (status == RecognitionStatus.NoError)
            {
                RecognitionAlternates alts = result.GetAlternatesFromSelection(0, -1, 10);
                foreach (var alt in alts)
                {
                    results.Add(alt.ToString());
                }
            }
            return(results.ToArray());
        }
        /// <summary>
        /// display the alternatives of each strokes collection in listboxes, shown below
        /// the recognized textbox.
        /// </summary>
        /// <param name="alternates">list of alternatives</param>
        /// <param name="x">x coordinate of corresponding textbox</param>
        /// <param name="y">y coordinate of corresponding textbox</param>
        private void showAlternative(RecognitionAlternates alternates, int x, int y, string name)
        {
            try
            {
                ListBox list = new ListBox();
                Controls.Add(list);
                // list.BringToFront();

                if (alternates != null)
                {
                    // if I change the location here, I must also change it in findTextbox
                    for (int i = 0; i < alternates.Count; i++)
                    {
                        list.Items.Add(alternates[i]);
                        if (list.Items.Count == 3)
                        {
                            list.Size = list.PreferredSize;
                        }
                    }
                }
                else
                {
                    list.Items.Add("No alternatives");
                    list.Size = list.PreferredSize;
                }

                list.ScrollAlwaysVisible = true;
                list.IntegralHeight      = true;
                list.AllowDrop           = false;
                list.Name = name;

                list.Enabled = true;
                list.Hide();

                alternateList.Add(list); // keep track of all the listboxes created at one time
            }
            catch (Exception list)
            {
                debugExceptionMessage("list", list);
            }
        }
예제 #8
0
        /// <summary>
        /// 笔记识别
        /// </summary>
        /// <param name="strokes"></param>
        /// <returns></returns>
        public List <string> Recognize(StrokeCollection strokes)
        {
            List <string> lstr = new List <string>();

            using (MemoryStream ms = new MemoryStream())
            {
                strokes.Save(ms);
                var ink = new Ink();
                ink.Load(ms.ToArray());
                using (RecognizerContext context = new RecognizerContext())
                {
                    if (ink.Strokes.Count > 0)
                    {
                        context.Strokes = ink.Strokes;
                        RecognitionStatus status;

                        var result = context.Recognize(out status);

                        if (status == RecognitionStatus.NoError)
                        {
                            //lstr.Add(result.TopString);//最可能的识别

                            //获取所有备选词
                            RecognitionAlternates alternates = result.GetAlternatesFromSelection(0, result.TopString.Length, 10);
                            foreach (RecognitionAlternate alternate in alternates)
                            {
                                lstr.Add(alternate.ToString());
                            }
                        }
                        //else
                        //    lstr.Add("识别失败");
                    }
                    //else
                    //    lstr.Add("没有侦测到签名");
                }
                return(lstr);
            }
        }
예제 #9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strokes"></param>
        internal String recognize(Strokes strokes, out RecognitionAlternates alternates)
        {
            RecognizerContext rc = new RecognizerContext();
            rc.Strokes = strokes;

            try {
                RecognitionStatus status;
                RecognitionResult result;
                result = rc.Recognize(out status);
                if (status == RecognitionStatus.NoError) {
                    RecognitionAlternates alternatives = result.GetAlternatesFromSelection();
                    alternates = alternatives;
                    return result.TopString;
                }
                else {
                    Console.WriteLine("Error in recognition.");
                }
            }
            catch {
                Console.WriteLine("Exception in recognition.");
            }
            alternates = null;
            return null;
        }
예제 #10
0
        /// <summary>
        ///
        ///
        /// </summary>
        public void talkToClient()
        {
            while (true)
            {
                NetworkStream networkStream = new NetworkStream(client);
                StreamWriter  streamWriter  = new StreamWriter(networkStream);
                StreamReader  streamReader  = new StreamReader(networkStream);

                // blocking call to wait for input
                string line = streamReader.ReadLine();

                String shortMessage;
                if (line.Length > 20)
                {
                    shortMessage = line.Substring(0, 20) + "...";
                }
                else
                {
                    shortMessage = line;
                }
                log("Read from Client " + id + ": " + shortMessage);

                // get the command from the client
                Match match = Regex.Match(line, @"\[\[(.*?)\]\].*");
                if (match.Success)
                {
                    String command = match.Groups[1].ToString().ToLower();
                    log("Command: " + command);

                    switch (command)
                    {
                    case "exit":
                        log("Disconnecting from Client " + id + ".");
                        client.Close();
                        return;

                    case "quitserver":
                        log("Disconnecting from Client " + id + ".");
                        client.Close();
                        log("Exiting the Server.");
                        gui.exit();
                        return;

                    case "topten":
                        // return nothing if there was no last call...
                        if (alternatives == null)
                        {
                            // do nothing
                        }
                        else
                        {
                            // return the top ten list of the last call...
                            log("Here is the complete list of alternates: ");
                            if (alternatives != null)
                            {
                                for (int i = 0; i < alternatives.Count; i++)
                                {
                                    log(alternatives[i].ToString() + "  [" + alternatives[i].Confidence + "]");
                                    streamWriter.WriteLine(alternatives[i].ToString());
                                }
                                streamWriter.WriteLine("[[endofalternatives]]");
                                streamWriter.Flush();
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    // assume it's just some XML
                    log("XML data received...");
                    Recognizer rec     = new Recognizer();
                    Strokes    strokes = rec.getStrokesFromXML(line);
                    alternatives = null;
                    String topResult = rec.recognize(strokes, out alternatives);
                    log("The top result is: " + topResult);

                    if (topResult != null)
                    {
                        // respond with some ASCII
                        streamWriter.WriteLine(topResult);
                        streamWriter.Flush();
                    }
                    else
                    {
                        log("Invalid Input Data");
                    }
                }
            }
        }
예제 #11
0
        /// <summary>
        /// 
        /// 
        /// </summary>
        public void talkToClient()
        {
            while (true) {
                NetworkStream networkStream = new NetworkStream(client);
                StreamWriter streamWriter = new StreamWriter(networkStream);
                StreamReader streamReader = new StreamReader(networkStream);

                // blocking call to wait for input
                string line = streamReader.ReadLine();

                String shortMessage;
                if (line.Length > 20) {
                    shortMessage = line.Substring(0, 20) + "...";
                }
                else {
                    shortMessage = line;
                }
                log("Read from Client " + id + ": " + shortMessage);

                // get the command from the client
                Match match = Regex.Match(line, @"\[\[(.*?)\]\].*");
                if (match.Success) {
                    String command = match.Groups[1].ToString().ToLower();
                    log("Command: " + command);

                    switch (command) {
                        case "exit":
                            log("Disconnecting from Client " + id + ".");
                            client.Close();
                            return;
                        case "quitserver":
                            log("Disconnecting from Client " + id + ".");
                            client.Close();
                            log("Exiting the Server.");
                            gui.exit();
                            return;
                        case "topten":
                            // return nothing if there was no last call...
                            if (alternatives == null) {
                                // do nothing
                            }
                            else {
                                // return the top ten list of the last call...
                                log("Here is the complete list of alternates: ");
                                if (alternatives != null) {
                                    for (int i = 0; i < alternatives.Count; i++) {
                                        log(alternatives[i].ToString() + "  [" + alternatives[i].Confidence + "]");
                                        streamWriter.WriteLine(alternatives[i].ToString());
                                    }
                                    streamWriter.WriteLine("[[endofalternatives]]");
                                    streamWriter.Flush();
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
                else {
                    // assume it's just some XML
                    log("XML data received...");
                    Recognizer rec = new Recognizer();
                    Strokes strokes = rec.getStrokesFromXML(line);
                    alternatives = null;
                    String topResult = rec.recognize(strokes, out alternatives);
                    log("The top result is: " + topResult);

                    if (topResult != null) {
                        // respond with some ASCII
                        streamWriter.WriteLine(topResult);
                        streamWriter.Flush();
                    }
                    else {
                        log("Invalid Input Data");
                    }
                }
            }
        }