diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/domain/FatchRule.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/domain/FatchRule.java index aa0f5ee..34fb7d1 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/domain/FatchRule.java +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/domain/FatchRule.java @@ -25,5 +25,17 @@ public class FatchRule extends BaseEntity { private Long templateId; private String databaseName; + /** + * 顺序,开始字段重复时,指定抓取第几个 + */ + private Integer startContentRepeatOrder; + /** + * 结束字段重复时指定的顺序,默认1,只能>0填正整数 + */ + private Integer endContentRepeatOrder; + /** + * 抓取方向,默认0,0-从前往后抓取,1-从后往前抓取 + */ + private Integer fatchOrder; } diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/DeptIdentifyServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/DeptIdentifyServiceImpl.java index 29f3e83..eb8201a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/DeptIdentifyServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/DeptIdentifyServiceImpl.java @@ -535,21 +535,11 @@ public class DeptIdentifyServiceImpl implements IDeptIdentifyService { @Override public List getFatchRuleByTemplateid(TemplateManage templateManage) { - TemplateFatchRule templateFatchRule = new TemplateFatchRule(); - templateFatchRule.setTemplateId(templateManage.getId()); - List fatchRules = new ArrayList<>(); - int countTemplateFatchRule = templateFatchRuleMapper.selectTemplateFatchRulecount(templateFatchRule); - if(countTemplateFatchRule!=0){ + FatchRule fatchRule = new FatchRule(); fatchRule.setTemplateId(templateManage.getId()); - fatchRules = fatchRuleMapper.selectFatchRuleList(fatchRule); - return fatchRules; - }else { - FatchRule fatchRule = new FatchRule(); - fatchRule.setIsDefault(1); - fatchRules = fatchRuleMapper.selectFatchRuleListIsDefault(fatchRule); - return fatchRules; - } + return fatchRuleMapper.selectFatchRuleList(fatchRule); + } @Override diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java index 5e4200b..afdaa34 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java @@ -32,20 +32,22 @@ public class OCRUtils { , "委托代理人", "联系电话", "电子邮件", "被申请人", "居民身份证号码", "仲裁请求", "事实和理由"}; // 贷款合同识别字段 private static final String[] contractName = { - "合同编号","甲方(贷款人)" ,"或委托代理人签字:","本合同的初始贷款年利率为","乙方确认有效的电子信箱地址为"}; + "合同编号", "甲方(贷款人)", "或委托代理人签字:", "本合同的初始贷款年利率为", "乙方确认有效的电子信箱地址为"}; // 调解协议识别字段 - private static final String[] accordName = {"金融消费纠纷基本情况","经调解,双方自愿达成如下协议"}; + private static final String[] accordName = {"金融消费纠纷基本情况", "经调解,双方自愿达成如下协议"}; // 授权委托书识别字段 private static final String[] powerAttorneyName = {"职务"}; + /** * pdf识别成文字 + * * @param imageBase64 * @param pageNumber - * @param fatchRules 抓取规则 + * @param fatchRules 抓取规则 * @return */ public static String pdfIdentifyText(String imageBase64, Integer pageNumber, List fatchRules) { - StringBuilder respStr=new StringBuilder(); + StringBuilder respStr = new StringBuilder(); try { // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 @@ -76,17 +78,16 @@ public class OCRUtils { for (TextDetection textDetection : textDetections) { respStr.append(textDetection.getDetectedText()); } - }catch (TencentCloudSDKException e){ + } catch (TencentCloudSDKException e) { throw new ServiceException("ocr识别失败"); } - if(respStr.toString().endsWith(String.valueOf(pageNumber))){ + if (respStr.toString().endsWith(String.valueOf(pageNumber))) { int lastIndexOf = respStr.toString().lastIndexOf(String.valueOf(pageNumber)); - return respStr.toString().substring(0,lastIndexOf); + return respStr.toString().substring(0, lastIndexOf); } return respStr.toString(); - // // //解析数据 // String s = GeneralAccurateOCRResponse.toJsonString(resp); @@ -121,12 +122,13 @@ public class OCRUtils { /** * pdf识别成文字 + * * @param imageBase64 * @param pageNumber - * @param type pdf类型 + * @param type pdf类型 * @return */ - public static String pdfIdentifyText1(String imageBase64, Integer pageNumber,String type) { + public static String pdfIdentifyText1(String imageBase64, Integer pageNumber, String type) { try { // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 @@ -145,13 +147,13 @@ public class OCRUtils { req.setImageBase64(imageBase64); req.setIsPdf(true); req.setPdfPageNumber(pageNumber.longValue()); - if(type.contains("申请书")){ + if (type.contains("申请书")) { req.setItemNames(applicantName); - }else if(type.contains("调解协议")){ + } else if (type.contains("调解协议")) { req.setItemNames(accordName); - }else if(type.contains("合同")){ + } else if (type.contains("合同")) { req.setItemNames(contractName); - }else if(type.contains("授权委托书")){ + } else if (type.contains("授权委托书")) { req.setItemNames(powerAttorneyName); } @@ -190,7 +192,8 @@ public class OCRUtils { } return null; } - public static String pdfConvertBase64(String pathUrl){ + + public static String pdfConvertBase64(String pathUrl) { try { File file = new File(pathUrl); FileInputStream fileInputStream = new FileInputStream(file); @@ -207,33 +210,90 @@ public class OCRUtils { /** * 根据抓取规则获取内容 - * @param ocrText ocr识别的text + * + * @param ocrText ocr识别的text * @param fatchRules 抓取规则 * @return */ public static void fatchRuleGetContent(String ocrText, List fatchRules, Map fatchMap) { - + String text = ocrText; if (CollectionUtil.isNotEmpty(fatchRules)) { for (FatchRule fatchRule : fatchRules) { - if (StrUtil.isEmpty(fatchRule.getStartContent())) { - continue; - } + // 从后往前抓取 + if (fatchRule.getFatchOrder() != null && fatchRule.getFatchOrder() == 1) { +// String reverseText = StrUtil.reverse(ocrText); +// if (StrUtil.isEmpty(fatchRule.getStartContent()) && StrUtil.isEmpty(fatchRule.getEndContent())) { +// fatchMap.put(fatchRule.getColumnName(), trimStr(text)); +// } else if(StrUtil.isEmpty(fatchRule.getStartContent())&&StrUtil.isNotEmpty(fatchRule.getEndContent())){ +// // 开始字段为空,结束字段不为空 +// String endReverseText = StrUtil.reverse(fatchRule.getEndContent()); +// int endContIndex = StrUtil.ordinalIndexOf(reverseText, endReverseText, fatchRule.getEndContentRepeatOrder()); +// if (endContIndex != -1) { +// String substring = text.substring(0, endContIndex); +// fatchMap.put(fatchRule.getColumnName(), trimStr(StrUtil.reverse(substring))); +// } +// +// +// }else if(StrUtil.isNotEmpty(fatchRule.getStartContent())&&StrUtil.isEmpty(fatchRule.getEndContent())){ +// // 开始字段不为空,结束字段为空 +// // 开始字段为空,结束字段不为空 +// String startReverseText = StrUtil.reverse(fatchRule.getStartContent()); +// int startContIndex = StrUtil.ordinalIndexOf(reverseText, startReverseText, fatchRule.getStartContentRepeatOrder()); +// if (startContIndex != -1) { +// String substring = text.substring(0, startContIndex); +// fatchMap.put(fatchRule.getColumnName(), trimStr(StrUtil.reverse(substring))); +// } +// +// }else if(StrUtil.isNotEmpty(fatchRule.getStartContent())&&StrUtil.isNotEmpty(fatchRule.getEndContent())){ +// String startReverseText = StrUtil.reverse(fatchRule.getStartContent()); +// String endReverseText = StrUtil.reverse(fatchRule.getEndContent()); +// int startContIndex = StrUtil.ordinalIndexOf(reverseText, startReverseText, fatchRule.getStartContentRepeatOrder()); +// if (startContIndex != -1) { +// // 结束字段不为空 +// int endContIndex = StrUtil.ordinalIndexOf(reverseText, endReverseText, fatchRule.getEndContentRepeatOrder()); +// if (endContIndex != -1 && (startContIndex + fatchRule.getStartContent().length()) <= text.length()) { +// String substring = text.substring(startContIndex + fatchRule.getStartContent().length(), endContIndex); +// // 去除\n +// fatchMap.put(fatchRule.getColumnName(),trimStr(StrUtil.reverse(substring))); +// } +// } +// } + } else { + // 开始为空结束为空 + if (StrUtil.isEmpty(fatchRule.getStartContent()) && StrUtil.isEmpty(fatchRule.getEndContent())) { + fatchMap.put(fatchRule.getColumnName(), trimStr(text)); + } else if (StrUtil.isNotEmpty(fatchRule.getStartContent())) { + int startContIndex = StrUtil.ordinalIndexOf(text, fatchRule.getStartContent(), fatchRule.getStartContentRepeatOrder()); + if (startContIndex != -1) { + // 开始不为空结束为空 + if (StrUtil.isEmpty(fatchRule.getEndContent())) { + if ((startContIndex + fatchRule.getStartContent().length()) <= text.length()) { + String substring = text.substring(startContIndex + fatchRule.getStartContent().length()); + // 去除\n + fatchMap.put(fatchRule.getColumnName(), trimStr(substring)); + } - if (StrUtil.isNotEmpty(fatchRule.getStartContent()) && StrUtil.isNotEmpty(fatchRule.getEndContent())) { - String s = StringUtils.substringBetween(ocrText, fatchRule.getStartContent(), fatchRule.getEndContent()); - if(StrUtil.isNotEmpty(s)){ - fatchMap.put(fatchRule.getColumnName(), StrUtil.trim(s)); - }else { - fatchMap.put(fatchRule.getColumnName(),""); - } + } else { + // 开始不为空结束不为空 + int endContIndex = StrUtil.ordinalIndexOf(text, fatchRule.getEndContent(), fatchRule.getEndContentRepeatOrder()); + if (endContIndex != -1 && (startContIndex + fatchRule.getStartContent().length()) <= text.length()) { + String substring = text.substring(startContIndex + fatchRule.getStartContent().length(), endContIndex); + // 去除\n + fatchMap.put(fatchRule.getColumnName(), trimStr(substring)); + } - }else if(StrUtil.isNotEmpty(fatchRule.getStartContent()) && StrUtil.isEmpty(fatchRule.getEndContent())){ - String s = StringUtils.substringAfter(ocrText,fatchRule.getStartContent()); - if(StrUtil.isNotEmpty(s)){ - fatchMap.put(fatchRule.getColumnName(), StrUtil.trim(s)); - }else { - fatchMap.put(fatchRule.getColumnName(),""); + } +// + } + + } else if (StrUtil.isEmpty(fatchRule.getStartContent()) && StrUtil.isNotEmpty(fatchRule.getEndContent())) { + // 开始为空结束不为空 + int endContIndex = StrUtil.ordinalIndexOf(text, fatchRule.getEndContent(), fatchRule.getEndContentRepeatOrder()); + if (endContIndex != -1) { + String substring = text.substring(0, endContIndex); + fatchMap.put(fatchRule.getColumnName(), trimStr(substring)); + } } } } @@ -241,4 +301,20 @@ public class OCRUtils { } + + /** + * 去除末尾空格 + * + * @param substring + * @return + */ + private static String trimStr(String substring) { + if (StrUtil.isNotEmpty(substring)) { + return StrUtil.trim(substring); + } else { + return ""; + } + + } + } diff --git a/ruoyi-system/src/main/resources/mapper/wisdomarbitrate/FatchRuleMapper.xml b/ruoyi-system/src/main/resources/mapper/wisdomarbitrate/FatchRuleMapper.xml index 3212156..4cf1072 100644 --- a/ruoyi-system/src/main/resources/mapper/wisdomarbitrate/FatchRuleMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/wisdomarbitrate/FatchRuleMapper.xml @@ -12,15 +12,20 @@ + + +