예제 #1
0
        ///////////////////////////////////////////////////////////////////////////////

        static void recursiveCheckVerified(IEngine engine, IField field)
        {
            if (field.Value != null)
            {
                assert(!field.Value.NeedsVerification);
                if (field.Value.Type == FieldValueTypeEnum.FVT_Text)
                {
                    IText       text = field.Value.AsText;
                    ICharParams p    = engine.CreateCharParams();
                    for (int i = 0; i < text.Length; i++)
                    {
                        text.GetCharParams(i, p);
                        assert(!p.NeedsVerification);
                    }
                }
            }
            recursiveCheckVerified(engine, field.Instances);
            recursiveCheckVerified(engine, field.Children);
        }
        public static void Using_recognition_variants_and_extended_character_info(IEngine engine, IDocument document, string fieldName)
        {
            Console.WriteLine("Enable recognition variants...");
            engine.EnableRecognitionVariants(true);

            Console.WriteLine("Find the field of interest...");
            IField field = findField(document, fieldName);

            Console.WriteLine("Show word variants...");
            // (1) You might want to use word variants if you know how to choose the correct result
            // and this knowledge cannot be communicated to the engine (in the form of a regular expression
            // or dictionary) or if you decide that you can do the selection better.
            // EXAMPLES: (1) a field containing some code with a checksum (2) a field that can be looked up
            // in a database (3) a field that can be crosschecked with another field in the same document
            IText text = field.Value.AsText;
            IRecognizedWordInfo wordInfo = engine.CreateRecognizedWordInfo();

            // Get the recognition info for the first word. Thus we'll know the number of available variants
            text.GetRecognizedWord(0, -1, wordInfo);
            for (int i = 0; i < wordInfo.RecognitionVariantsCount; i++)
            {
                // Get the specified recognition variant for the word
                text.GetRecognizedWord(0, i, wordInfo);
                Console.WriteLine(wordInfo.Text);
            }
            Console.WriteLine("Ended ...");

            Console.WriteLine("Show char variants for each word variant...");
            // (2) You can use a more advanced approach and build your own hypotheses from variants
            // for individual characters. This approach can be very computationally intensive, so use it with care.
            // Use word/char variant confidence to limit the number of built hypotheses
            IRecognizedCharacterInfo charInfo = engine.CreateRecognizedCharacterInfo();

            for (int k = 0; k < wordInfo.RecognitionVariantsCount; k++)
            {
                // For each variant of the first word
                text.GetRecognizedWord(0, k, wordInfo);
                Console.WriteLine(wordInfo.Text);
                for (int i = 0; i < wordInfo.Text.Length; i++)
                {
                    // Get the recognition info for the first character (the number of available variants)
                    wordInfo.GetRecognizedCharacter(i, -1, charInfo);
                    string charVars = "";
                    for (int j = 0; j < charInfo.RecognitionVariantsCount; j++)
                    {
                        // Get the specified recognition variant for the character. The variant may contain
                        // more than one character if the geometry in the specified position can be interpreted
                        // as several merged characters. For example, something which looks like poorly printed 'U'
                        // can actually be a pair of merged 'I'-s or 'I' + 'J'
                        wordInfo.GetRecognizedCharacter(i, j, charInfo);
                        if (charInfo.CharConfidence > 50)
                        {
                            charVars += charInfo.Character.PadRight(4, ' ');
                        }
                    }
                    Console.WriteLine(charVars);
                }
                Console.WriteLine("Ended");
            }
            Console.WriteLine("Ended");

            Console.WriteLine("Linking text in the field to recognition variants...");
            // (3) You can find corresponding recognition word and character variants for each character in the text
            // even if the text has been modified. This can be helpful in building your own advanced verification tools
            // where users can choose variants for words while typing or from a list
            ICharParams charParams = engine.CreateCharParams();

            for (int i = 0; i < text.Length; i++)
            {
                text.GetCharParams(i, charParams);
                Console.WriteLine(string.Format("'{0}' is char number {1} in word number {2}", text.Text[i],
                                                charParams.RecognizedWordCharacterIndex, charParams.RecognizedWordIndex));
            }
            Console.WriteLine("");

            Console.WriteLine("Obtaining extended char information for characters in text...");
            // (4) You can obtain extended char info for a character in the text without getting involved deeply in
            // the recognition variants API by means of the shortcut method GetCharParamsEx
            for (int i = 0; i < text.Length; i++)
            {
                text.GetCharParamsEx(i, charParams, charInfo);
                Console.WriteLine(string.Format("'{0}' serif probability is {1}", charInfo.Character, charInfo.SerifProbability));
            }
            Console.WriteLine("");

            // Restore the initial state of the engine. It is optional and should not be done if you always
            // use recognition variants. Doing so will reset some internal caches and if done repeatedly
            // might affect performance
            engine.EnableRecognitionVariants(false);
        }