package com.walker.openocr;
|
|
import com.walker.openocr.idcard.IdCardConfig;
|
import com.walker.openocr.idcard.IdCardObject;
|
import com.walker.openocr.idcard.IdCardTextResolver;
|
import com.walker.openocr.table.TableConfig;
|
import com.walker.openocr.table.TableConfigLoader;
|
import com.walker.openocr.table.TableObject;
|
import com.walker.openocr.table.TableTextResolver;
|
import com.walker.openocr.table.config.FileConfigLoader;
|
import com.walker.openocr.util.FileReaderUtils;
|
import com.walker.openocr.util.TableObjectUtils;
|
import com.walker.openocr.util.TextUtils;
|
import com.walker.openocr.vehicle.CarTypeValueParser;
|
import com.walker.openocr.vehicle.IdValueParser;
|
import com.walker.openocr.vehicle.PlateValueParser;
|
import com.walker.openocr.vehicle.VehicleLicenseResolver;
|
import org.junit.Test;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class TableDataTest {
|
|
private String folder = "D:/dev_tools/workspace_2022/walker-dependencies/walker-ml-openocr/src/test/resources";
|
|
// @Test
|
public void testValueParser(){
|
// String input = "云A853L5";
|
// String input = "豫VL120";
|
// PlateValueParser parser = new PlateValueParser();
|
// System.out.println("pser value = " + parser.getValue(input));
|
|
// String input = "LSVWY4180KN246094";
|
// String input = "LSVAK29C8L2969237";
|
// IdValueParser idValueParser = new IdValueParser();
|
// System.out.println(idValueParser.getValue(input));
|
|
// String input = "小型轿车";
|
String input = "车辆类型小型新车";
|
CarTypeValueParser carTypeValueParser = new CarTypeValueParser();
|
System.out.println(carTypeValueParser.getValue(input));
|
}
|
|
// @Test
|
public void testVehicleLicense(){
|
// String filePath = folder + "/demo_vehicle_01.txt";
|
String filePath = folder + "/demo_vehicle_02.txt";
|
List<TextBlock> textBlocks = this.acquireTextBlockList(filePath);
|
|
FileConfigLoader vehicleConfig = new FileConfigLoader();
|
TableConfig tableConfig = vehicleConfig.load(folder + "/vehicle_license_config.txt");
|
|
List<TableConfig> tableConfigs = new ArrayList<>(4);
|
tableConfigs.add(tableConfig);
|
|
VehicleLicenseResolver vehicleLicenseResolver = new VehicleLicenseResolver();
|
vehicleLicenseResolver.resolve(textBlocks, tableConfigs);
|
}
|
|
// @Test
|
public void testFirstMoneyNumber(){
|
String input = "总金额为12,006,599.01";
|
// String input = "保险费合计(人民币大写):壹佰贰拾元整 (Y:120.00元)其中救助基金(1.00%)";
|
// String input = "RMB284.32元(不含税保费:268.23元,税额:16.09元)(大写)人民币贰佰捌拾";
|
String money = TextUtils.parseFirstMoneyNumber(input);
|
System.out.println(money);
|
}
|
|
// @Test
|
public void testStringDateDouble(){
|
String input = "自2022年3月14日00:00时起至2023年3月13日24:00时止";
|
// String input = "保险期间自2022年08月25日00时00分起至2023年08月24日24时00分止";
|
String[] doubleDateNumber = TextUtils.parseDoubleDateWithNumber(input);
|
System.out.println(doubleDateNumber[0] + ", " + doubleDateNumber[1]);
|
}
|
|
// @Test
|
public void TestIdCard(){
|
// String filePath = folder + "/idcard_01.txt";
|
String filePath = folder + "/idcard_02.txt";
|
List<TextBlock> textBlocks = this.acquireTextBlockList(filePath);
|
|
List<IdCardConfig> tableConfigs = new ArrayList<>(4);
|
tableConfigs.add(new IdCardConfig());
|
|
IdCardTextResolver idCardTextResolver = new IdCardTextResolver();
|
IdCardObject idCardObject = idCardTextResolver.resolve(textBlocks, tableConfigs);
|
System.out.println(idCardObject);
|
}
|
|
// @Test
|
public void TestLoadFile(){
|
// TableConfigLoader dadiTableConfigTest = new FileConfigLoader();
|
// TableConfig tableConfig = dadiTableConfigTest.load(folder + "/table_config_dadi.txt");
|
// System.out.println("table title = " + tableConfig.getTableTitle());
|
|
String filePath = folder + "/demo_table_dadi.txt";
|
// String filePath = folder + "/demo_table_pinan.txt";
|
List<TextBlock> textBlocks = this.acquireTextBlockList(filePath);
|
// for(TextBlock textBlock : textBlocks){
|
// System.out.println(textBlock);
|
// }
|
|
TableConfig dadiTableConfig = TestTableConfigUtils.generateDadiConfig();
|
TableConfig pinganTableConfig = TestTableConfigUtils.generatePinganConfig();
|
List<TableConfig> tableConfigs = new ArrayList<>(4);
|
tableConfigs.add(dadiTableConfig);
|
tableConfigs.add(pinganTableConfig);
|
|
TextResolver<TableObject, TableConfig> textResolver = new TableTextResolver();
|
textResolver.resolve(textBlocks, tableConfigs);
|
|
}
|
|
private List<TextBlock> acquireTextBlockList(String filePath){
|
List<String> lines = FileReaderUtils.getFileLines(filePath);
|
List<TextBlock> textBlocks = new ArrayList<>(32);
|
if(lines != null && lines.size() > 0){
|
int blockSize = lines.size() / 4;
|
System.out.println("block size = " + blockSize);
|
int j = 0;
|
String text = null;
|
String start = null;
|
String end = null;
|
String score = null;
|
TextBlock textBlock = null;
|
for(int i=0; i<lines.size(); i++){
|
if(j == 0){
|
text = lines.get(i);
|
j++;
|
continue;
|
}
|
if(j == 1){
|
start = lines.get(i);
|
j++;
|
continue;
|
}
|
if(j == 2){
|
end = lines.get(i);
|
j++;
|
continue;
|
}
|
if(j == 3){
|
score = lines.get(i);
|
textBlock = this.acquireOneTextBlock(text, start, end, score);
|
textBlocks.add(textBlock);
|
j = 0;
|
continue;
|
}
|
}
|
}
|
return textBlocks;
|
}
|
|
private TextBlock acquireOneTextBlock(String text, String start, String end, String score){
|
text = TableObjectUtils.formatText(text);
|
TextBlock textBlock = new TextBlock();
|
textBlock.setText(text);
|
textBlock.setStartPosition(this.acquirePosition(start));
|
textBlock.setEndPosition(this.acquirePosition(end));
|
textBlock.setScore(Float.parseFloat(score));
|
return textBlock;
|
}
|
|
private float[] acquirePosition(String position){
|
position = position.replaceFirst(LEFT_BRACE, "").replaceFirst(RIGHT_BRACE, "");
|
String[] xyStr = position.split(",");
|
return new float[]{Float.parseFloat(xyStr[0].trim()), Float.parseFloat(xyStr[1].trim())};
|
}
|
|
private static final String LEFT_BRACE = "\\[";
|
private static final String RIGHT_BRACE = "]";
|
}
|