Creating your own OCR Validation Control plugin

You can create your own OCR Validation plugin.

Conditions:

  • The new OCR Validation Control class must implement the "OCRTemplateControlParser" interface.
  • The new OCR Validation Control class must be declared under the package "com.openkm.plugin.ocr.template.controlparser".
  • The new OCR Validation Control class must be annotated with "@PluginImplementation".
  • The new OCR Validation Control class must extend of "BasePlugin".

OCR Validation Control interface:

package com.openkm.plugin.ocr.template;

import com.openkm.db.bean.OCRTemplateControlField;
import net.xeoh.plugins.base.Plugin;

/**
 * OCRTemplateControlParser
 */
public interface OCRTemplateControlParser extends Plugin {

    boolean parse(OCRTemplateControlField otcf, String text) throws OCRTemplateException, OCRParserEmptyValueException;

    String getName();

    boolean isPatternRequired();

    String info();
}

The new class must be loaded into the package com.openkm.plugin.ocr.template.controlparser because application plugins system will try to load from there.

Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.

More information at: Register a new plugin.

Methods description

MethodTypeDescription

parse(OCRTemplateControlField otcf, String text)

boolean

When returns true indicates that the field complies with the conditions.

More information about OCR Template stages at: OCR templates.

getName()

String

Sets the name that will be shown in the administrator user interface selector list.

isPatternRequired()

boolean

When returns true indicates that the pattern field is required.

It will show an error - missing pattern - on the administrator user interface if a pattern is required and the user is not set.

info()

String

Description of the implemented logic.

This description is shown in the administrator user interface.

Example:

StringSimilarParser class:

package com.openkm.plugin.ocr.template.controlparser;

import com.openkm.core.DatabaseException;
import com.openkm.db.bean.OCRTemplateControlField;
import com.openkm.db.service.ConfigSrv;
import com.openkm.plugin.BasePlugin;
import com.openkm.plugin.ocr.template.OCRParserEmptyValueException;
import com.openkm.plugin.ocr.template.OCRTemplateControlParser;
import com.openkm.plugin.ocr.template.OCRTemplateException;
import com.openkm.util.AlgorithmUtils;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * StringParser
 */
@PluginImplementation
public class StringSimilarParser extends BasePlugin implements OCRTemplateControlParser {

    @Autowired
    private ConfigSrv configSrv;

    @Override
    public boolean parse(OCRTemplateControlField otf, String text) throws OCRTemplateException, OCRParserEmptyValueException {
        if (text == null || text.equals("")) {
            return false;
        } else {
            if (otf.getPattern() == null || otf.getPattern().equals("")) {
                int distance = AlgorithmUtils.computeLevenshteinDistance(text, otf.getValue());
                try {
                    return distance < configSrv.getInteger("ocr.control.similar.distance", 5);
                } catch (DatabaseException e) {
                    throw new OCRTemplateException(e);
                }
            } else {
                Pattern pattern = Pattern.compile("(" + otf.getPattern() + ")", Pattern.UNICODE_CASE);
                Matcher matcher = pattern.matcher(text);
                if (matcher.find() && matcher.groupCount() == 1) {
                    if (otf.getValue().equals("")) {
                        return true;
                    } else {
                        int distance = AlgorithmUtils.computeLevenshteinDistance(matcher.group(), otf.getValue());
                        try {
                            return distance < configSrv.getInteger("ocr.control.similar.distance", 5);
                        } catch (DatabaseException e) {
                            throw new OCRTemplateException(e);
                        }
                    }
                } else {
                    return false;
                }
            }
        }
    }

    @Override
    public String getName() {
        return "Similar String";
    }

    @Override
    public boolean isPatternRequired() {
        return false;
    }

    @Override
    public String info() {
        StringBuilder sb = new StringBuilder();
        sb.append("Empty pattern case: true if extracted text similar field value.\n");
        sb.append("Pattern with value field empty: true if pattern found.\n");
        sb.append("Pattern with value field: true if pattern found similar value.\n");
        return null;
    }
}