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 in 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 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 the application plugin system will try to load classes from there.
Method descriptions
Section titled “Method descriptions”| Method | Type | Description |
|---|---|---|
| parse(OCRTemplateControlField otcf, String text) | boolean | When it returns true, it 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’s user interface selector list. |
| isPatternRequired() | boolean | When it returns true, it indicates that the pattern field is required. It will show an error - ‘missing pattern’ - in the administrator user interface if a pattern is required and the user has not set one. |
| info() | String | Description of the implemented logic. This description is shown in the administrator user interface. |
Example:
Section titled “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 */@PluginImplementationpublic class StringSimilarParser extends BasePlugin implements OCRTemplateControlParser {
@Autowired private ConfigSrv configSrv;
@Override public boolean parse(OCRTemplateControlField otcf, String text) throws OCRTemplateException, OCRParserEmptyValueException { if (text == null || text.equals("")) { return false; } else { if (otcf.getPattern() == null || otcf.getPattern().equals("")) { int distance = AlgorithmUtils.computeLevenshteinDistance(text, otcf.getValue()); try { return distance < configSrv.getInteger("ocr.control.similar.distance", 5); } catch (DatabaseException e) { throw new OCRTemplateException(e); } } else { Pattern pattern = Pattern.compile("(" + otcf.getPattern() + ")", Pattern.UNICODE_CASE); Matcher matcher = pattern.matcher(text); if (matcher.find() && matcher.groupCount() == 1) { if (otcf.getValue().equals("")) { return true; } else { int distance = AlgorithmUtils.computeLevenshteinDistance(matcher.group(), otcf.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; }}