Dela via


Tablet PC: Using WordLists and Coerce mode with the RecognizerContext

In my last post, I demonstrated setting the Factoid property on a RecognizerContext object. Setting the Factoid provides contextual information for the handwriting recognizers. One of the special Factoids is the “WordList” Factoid. The WordList Factoid is used in conjunction with the WordList property on the RecognizerContext object. The WordList property is essentially a collection of strings which specify possible recognition results. A typical scenario would be a field on your application’s form where you expect your end user to input only a known set of values.

Simple example of using the WordList factoid:

// Instantiate a RecognizerContext object

RecognizerContext context = new RecognizerContext();

// Set the Factoid to "WordList"

context.Factoid = Factoid.WordList;

// Instantiate a new WordList, add some strings

WordList list = new WordList();

list.Add("Car");

list.Add("Truck");

// Set the WordList property on the RecognizerContext

context.WordList = list;

When recognition is performed, the recognizers will try to use “Car” and “Truck” for the recognition result. But what if you want to force the recognizers to use your WordList? In this case you want to set the Coerce flag on the RecognizerContext.RecognitionFlags property. If you set that flag, the recognizers will return a result from your WordList, or a null RecognitionResult if there are no entries in your WordList that can be used.

In the following example code, I’ve updated the basic ink application sample with a wordlist of people, possibly coworkers. If the collected ink can be recognized as one of the WordList items, the result is shown. If the collected ink can not be interpreted as one of the word list items, the user is prompted. Since I've set the Coerce flag in RecognitionFlags, the recognition result will be either nothing (null) or one of the items from the WordList.

//

// Basic Ink enabled Windows Forms application with

// handwriting recognition using RecognizerContext

// Gavin Gear - https://blogs.msdn.com/gavingear

// 08/2006

//

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.Ink; // The managed Tablet PC API

namespace BasicInkApplication

{

    public partial class BasicInkApplication : Form

    {

        // The InkOverlay that we'll attach to our Form

        private InkOverlay inkOverlay;

        public BasicInkApplication()

        {

            InitializeComponent();

            // Create an InkOverlay object that's attached to the Form

            this.inkOverlay = new InkOverlay(this);

            // Enable the InkOverlay (default is Enabled == false)

            this.inkOverlay.Enabled = true;

            // The InkOverlay needs to be disposed due to unmanaged resources

            // used by the InkOverlay

            this.FormClosing += new FormClosingEventHandler(BasicInkApplication_FormClosing);

        }

        void BasicInkApplication_FormClosing(object sender, FormClosingEventArgs e)

        {

            this.inkOverlay.Dispose();

      }

        private void buttonRecognize_Click(object sender, EventArgs e)

        {

            // Should check for strokes existing here before proceeding...

            // Instantiate a RecognizerContext object

            RecognizerContext context = new RecognizerContext();

            string [] names = {

                "Michael Scott",

                "Pam Beesly",

                "Jim Halpert",

                "Roy Anderson",

                "Dwight Schrute",

                "Ryan Howard",

                "Angela Martin",

                "Oscar Martinez",

                "Kevin Malone",

                "Stanley Hudson",

                "Phyllis Lapin",

                "Meredith Palmer",

                "Kelly Kapoor",

                "Creed Bratton",

                "Darryl Philbin",

                "Jan Levinson",

                "Todd Packer"

                };

            // Add the strings from names to the WordList property of the

            // RecognizerContext

            WordList list = new WordList();

            foreach (string s in names)

            {

                list.Add(s);

            }

            context.WordList = list;

            context.Factoid = Factoid.WordList;

            // Force the RecognizerContext to use the WordList for the

            // recognition results

            context.RecognitionFlags = RecognitionModes.Coerce;

            // Add the strokes collected by our InkOverlay

            context.Strokes = this.inkOverlay.Ink.Strokes;

            // Perform recognition, pass a RecognitionStatus object

            // that will give the status of the recognition

            RecognitionStatus status;

            RecognitionResult result = context.Recognize(out status);

            if (result != null)

            {

                MessageBox.Show(result.TopString);

            }

            else

            {

                string message ="Could not Coerce to the following wordlist: " +

                               Environment.NewLine;

     foreach(string s in names)

                {

                    message += s + Environment.NewLine;

                }

                MessageBox.Show(message);

            }

            context.Dispose(); // Free the unmanaged resources

        }

    }

}

**Note that on non-Tablet PC OS platforms, you'll need to install the recognizer pack, otherwise the RecognizerContext object will throw an exception when it is instantiated. You can download the recognizer pack from the following location:

https://www.microsoft.com/downloads/details.aspx?FamilyID=080184dd-5e92-4464-b907-10762e9f918b&DisplayLang=en

For more information of Factoids see the msdn article:

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tpcsdk10/lonestar/microsoft.ink/Classes/recognizercontext/Properties/factoid.asp

Using WordLists in your application can be a great way to customize the handwriting recognition to create a better end-user experience for your Ink enabled application. Dynamically populating WordLists would be a great way to implement such a solution, since you can use up to date data from a file, database, or other source.

See Ya-

Gavin

Comments