Creating your own OCR Validation Control plugin

You can create your own OCR Validation plugin.

Conditions:

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

OCR TemplateControlParser interface:

package com.openkm.plugin.ocr.template;

import net.xeoh.plugins.base.Plugin;

import com.openkm.dao.bean.OCRTemplateControlField;

/**
 * OCRTemplateControlParser
 * 
 * 
 */
public interface OCRTemplateControlParser extends Plugin {
	boolean parse(OCRTemplateControlField otcf, String text) throws OCRTemplateException, OCRParserEmptyValueException;
	String getName();
	boolean isPatternRequired();
	String info();
}

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.ocr.template.controlparser;

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

import net.xeoh.plugins.base.annotations.PluginImplementation;

import com.openkm.core.DatabaseException;
import com.openkm.dao.ConfigDAO;
import com.openkm.dao.bean.OCRTemplateControlField;
import com.openkm.ocr.template.OCRParserEmptyValueException;
import com.openkm.ocr.template.OCRTemplateControlParser;
import com.openkm.ocr.template.OCRTemplateException;
import com.openkm.util.AlgorithmUtils;

/**
 * StringParser
 *
 */
@PluginImplementation
public class StringSimilarParser implements OCRTemplateControlParser {
	@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<ConfigDAO.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<ConfigDAO.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() {
		StringBuffer sb = new StringBuffer();
		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;
	}
}