shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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 = "]";
}