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
package com.walker.openocr.util;
 
import com.walker.openocr.Constants;
import com.walker.openocr.MultipleLine;
import com.walker.openocr.table.CellConfigItem;
import com.walker.openocr.table.ColumnConfigItem;
import com.walker.openocr.table.TableConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class TableConfigUtils {
 
    private static final Logger logger = LoggerFactory.getLogger(TableConfigUtils.class);
 
    public static CellConfigItem acquireCellConfig(String id
            , String name, boolean noneCell, int orderNum, String align, String endFlag
            , MultipleLine multipleLine, String dataType, boolean fullRow, int minValueSize){
        CellConfigItem cell = new CellConfigItem();
        cell.setId(id);
        cell.setName(name);
        cell.setNoneCell(noneCell);
        cell.setOrderNum(orderNum);
        if(align != null && !align.equals("")){
            cell.setAlign(align);
        }
        cell.setEndFlag(endFlag);
        if(multipleLine != null){
            cell.setMultipleLine(multipleLine);
        }
        cell.setDataType(dataType);
        cell.setFullRow(fullRow);
        cell.setMinValueSize(minValueSize);
        return cell;
    }
 
    public static ColumnConfigItem acquireColumnConfig(String id, String name
            , int orderNum, String align, MultipleLine multipleLine, int rowTotal){
        ColumnConfigItem item = new ColumnConfigItem();
        item.setId(id);
        item.setName(name);
        item.setOrderNum(orderNum);
        if(align != null && !align.equals("")){
            item.setAlign(align);
        }
        if(multipleLine != null){
            item.setMultipleLine(multipleLine);
        }
//        item.setDataType();
        item.setRowTotal(rowTotal);
        return item;
    }
 
    public static final boolean isCellConfigItem(CellConfigItem cellConfigItem, String text, TableConfig tableConfig){
        // 1.先检查名字中是否带有冒号,如果存在说明有明确标题名称
        // 2.检查字符串匹配,是否包含配置的名称,如果存在则说明该单元格:标题+内容
        // 3.如果都没有,按照表格配置相似百分比计算
        // 4.以上都没有,说明是其他字段,返回false
        String configName = cellConfigItem.getName();
        if(text.contains(Constants.COLON_EN)){
            String[] titleValue = text.split(Constants.COLON_EN);
            if(titleValue[0].equals(configName)){
                return true;
            }
        }
 
        // 2
        if(text.indexOf(configName) >= 0){
            logger.debug("字符串完全匹配到标题:" + configName + ", text=" + text);
            return true;
        }
 
        // 3
//        float count = 0;
//        float matchSize = 0;
//        for(String s : configName.split("")){
//            count++;
//            if(text.contains(s)){
//                matchSize++;
//            }
//        }
//        float matchPercent = matchSize / count;
        float matchPercent = getSimpleSimilarScore(configName, text);
        if(matchPercent >= tableConfig.getTitleTolerance()){
            logger.debug("根据标题百分比找到:" + configName + ", percent=" + matchPercent + ", text=" + text);
            return true;
        }
 
        return false;
    }
 
    /**
     * 返回给定字符串与对比字符串(configName)的匹配度分值:0~1
     * @param configName 给定的标准字符串
     * @param text 输入要比较的字符串
     * @return
     */
    public static float getSimpleSimilarScore(String configName, String text){
        if(TextUtils.isEmpty(configName) || TextUtils.isEmpty(text)){
            return 0;
        }
        if(text.length() > configName.length()){
            // 如果给定识别字符串比'标准字符串'大,说明明显不匹配
            return 0;
        }
//        if(text.length() < configName.length()){
//            // 如果给定字符串比'标准'字符串少,则肯定不匹配
//            return 0;
//        }
//        float count = text.length();
        float count = configName.length();
        float matchSize = 0;
        for(String s : configName.split(TextUtils.EMPTY_VALUE)){
            if(text.contains(s)){
                matchSize++;
            }
        }
        return  matchSize / count;
    }
}