9bab1769 by 周伟奇

fix wsc

1 parent 8cf3f917
...@@ -31,7 +31,6 @@ class Finder: ...@@ -31,7 +31,6 @@ class Finder:
31 31
32 def __init__(self, ocr_results=None): 32 def __init__(self, ocr_results=None):
33 self.ocr_results = ocr_results 33 self.ocr_results = ocr_results
34
35 self.init_result = { 34 self.init_result = {
36 "合同编号列表": [], 35 "合同编号列表": [],
37 "经销商名称_Page3": "", 36 "经销商名称_Page3": "",
...@@ -49,10 +48,8 @@ class Finder: ...@@ -49,10 +48,8 @@ class Finder:
49 "其他约定与条件英文": "", 48 "其他约定与条件英文": "",
50 "其他约定与条件中文": "", 49 "其他约定与条件中文": "",
51 } 50 }
52
53 def get_line(self, ocr_results, key_string): 51 def get_line(self, ocr_results, key_string):
54 # 根据指定关键词, 找出与关键词同处一行的字符 52 # 根据指定关键词, 找出与关键词同处一行的字符
55
56 top, bottom = -1, -1 53 top, bottom = -1, -1
57 # 首先找到这个关键词所在的 Bbox 54 # 首先找到这个关键词所在的 Bbox
58 for key in ocr_results: 55 for key in ocr_results:
...@@ -60,21 +57,18 @@ class Finder: ...@@ -60,21 +57,18 @@ class Finder:
60 if key_string in text: 57 if key_string in text:
61 top, bottom = min(bbox[1::2]), max(bbox[1::2]) 58 top, bottom = min(bbox[1::2]), max(bbox[1::2])
62 break 59 break
63
64 line_text = [] 60 line_text = []
65 # 然后找到一行 61 # 然后找到一行
66 for key in ocr_results: 62 for key in ocr_results:
67 bbox, text = ocr_results[key] 63 bbox, text = ocr_results[key]
68 if top < np.mean(bbox[1::2]) < bottom: 64 if top < np.mean(bbox[1::2]) < bottom:
69 line_text.append([bbox, text]) 65 line_text.append([bbox, text])
70
71 # 从左到右排序 66 # 从左到右排序
72 lines = '' 67 lines = ''
73 if len(line_text) > 0: 68 if len(line_text) > 0:
74 line_text = sorted(line_text, key=lambda x: x[0][0], reverse=False) 69 line_text = sorted(line_text, key=lambda x: x[0][0], reverse=False)
75 lines = ''.join([i[1] for i in line_text]) 70 lines = ''.join([i[1] for i in line_text])
76 return lines 71 return lines
77
78 def page_predict(self, ocr_results, page_template): 72 def page_predict(self, ocr_results, page_template):
79 classes = [] 73 classes = []
80 for pno in ocr_results: 74 for pno in ocr_results:
...@@ -84,12 +78,10 @@ class Finder: ...@@ -84,12 +78,10 @@ class Finder:
84 ocr_texts += text 78 ocr_texts += text
85 pattern = re.compile("[^\u4e00-\u9fa5]") # 匹配不是中文的其他字符 79 pattern = re.compile("[^\u4e00-\u9fa5]") # 匹配不是中文的其他字符
86 ocr_texts = pattern.sub('', ocr_texts) 80 ocr_texts = pattern.sub('', ocr_texts)
87
88 score = fuzz.ratio(page_template, ocr_texts)/100. 81 score = fuzz.ratio(page_template, ocr_texts)/100.
89 classes.append([pno, score]) 82 classes.append([pno, score])
90 pred = sorted(classes, key=lambda x: x[1], reverse=True)[0] 83 pred = sorted(classes, key=lambda x: x[1], reverse=True)[0]
91 return pred 84 return pred
92
93 def get_top_key(self, ocr_results, key_string): # 加入过滤词典 85 def get_top_key(self, ocr_results, key_string): # 加入过滤词典
94 """找到与 key_string 最匹配的字段的 key 86 """找到与 key_string 最匹配的字段的 key
95 """ 87 """
...@@ -98,7 +90,6 @@ class Finder: ...@@ -98,7 +90,6 @@ class Finder:
98 ratio_list = [[fuzz.ratio(key_string, ocr_results[key][1]), key] for key in ocr_results] 90 ratio_list = [[fuzz.ratio(key_string, ocr_results[key][1]), key] for key in ocr_results]
99 top_key = sorted(ratio_list, key=lambda x: x[0])[-1] 91 top_key = sorted(ratio_list, key=lambda x: x[0])[-1]
100 return top_key 92 return top_key
101
102 def get_top_iou(self, ocr_results, poly): 93 def get_top_iou(self, ocr_results, poly):
103 """求最大IoU 94 """求最大IoU
104 """ 95 """
...@@ -117,12 +108,10 @@ class Finder: ...@@ -117,12 +108,10 @@ class Finder:
117 return -1, -1 108 return -1, -1
118 top_iou = sorted(iou_list, key=lambda x: x[0])[-1] 109 top_iou = sorted(iou_list, key=lambda x: x[0])[-1]
119 return top_iou 110 return top_iou
120
121 def get_key_value(self, ocr_results, key_string): 111 def get_key_value(self, ocr_results, key_string):
122 """根据 key 查找 value 112 """根据 key 查找 value
123 """ 113 """
124 value = '' 114 value = ''
125
126 tmp_ocr_results = {} 115 tmp_ocr_results = {}
127 for key in ocr_results: 116 for key in ocr_results:
128 bbox, text = ocr_results[key] 117 bbox, text = ocr_results[key]
...@@ -131,7 +120,6 @@ class Finder: ...@@ -131,7 +120,6 @@ class Finder:
131 pattern = re.compile("[^\u4e00-\u9fa5]") # 匹配不是中文的其他字符 120 pattern = re.compile("[^\u4e00-\u9fa5]") # 匹配不是中文的其他字符
132 text = pattern.sub('', text) 121 text = pattern.sub('', text)
133 tmp_ocr_results[key] = [bbox, text] 122 tmp_ocr_results[key] = [bbox, text]
134
135 # 先根据 key_string 找到 key 的位置所在, 再判断该位置是否包含 value 123 # 先根据 key_string 找到 key 的位置所在, 再判断该位置是否包含 value
136 # 若不包含 value, 则往右边一个单位查找 value 124 # 若不包含 value, 则往右边一个单位查找 value
137 ratio, key = self.get_top_key(tmp_ocr_results, key_string) 125 ratio, key = self.get_top_key(tmp_ocr_results, key_string)
...@@ -151,7 +139,6 @@ class Finder: ...@@ -151,7 +139,6 @@ class Finder:
151 else: 139 else:
152 value = words 140 value = words
153 return value 141 return value
154
155 def get_contract_No(self): 142 def get_contract_No(self):
156 """提取左上角的合同编号字段 143 """提取左上角的合同编号字段
157 """ 144 """
...@@ -162,22 +149,21 @@ class Finder: ...@@ -162,22 +149,21 @@ class Finder:
162 contract_No = self.get_key_value(self.ocr_results[pno], '合同编号') 149 contract_No = self.get_key_value(self.ocr_results[pno], '合同编号')
163 else: 150 else:
164 contract_No = '' 151 contract_No = ''
152 # 临时解决 S 识别成 8 的问题
153 # TODO!!!
165 contract_No_list.append(contract_No) 154 contract_No_list.append(contract_No)
166 return contract_No_list 155 return contract_No_list
167
168 def get_info_in_page_3(self): 156 def get_info_in_page_3(self):
169 """提取第三页上的经销商名称,和经销商统一社会信用代码或公司注册号 157 """提取第三页上的经销商名称,和经销商统一社会信用代码或公司注册号
170 """ 158 """
171 dealer_name = '' 159 dealer_name = ''
172 dealer_No = '' 160 dealer_No = ''
173
174 template = r"""合同编号宝马汽车金融中国有限公司甲方宝马汽车金融中国有限公司地址中国北京市朝阳区东三环北路霞光里号佳程 161 template = r"""合同编号宝马汽车金融中国有限公司甲方宝马汽车金融中国有限公司地址中国北京市朝阳区东三环北路霞光里号佳程
175 广场座层乙方统一社会信用代码或公司注册号地址鉴于甲方是一家依照中国法律合法组建和存续的汽车金融公司愿意 162 广场座层乙方统一社会信用代码或公司注册号地址鉴于甲方是一家依照中国法律合法组建和存续的汽车金融公司愿意
176 为宝马中国汽车贸易有限公司以下简称宝马中国及华晨宝马汽车有限公司以下简称华晨宝马在中国大陆的宝马集团经 163 为宝马中国汽车贸易有限公司以下简称宝马中国及华晨宝马汽车有限公司以下简称华晨宝马在中国大陆的宝马集团经
177 销商提供汽车批售融资服务乙方是一家依据中国法律合法组建和存续与宝马中国和或华晨宝马签署了授权销售合同具 164 销商提供汽车批售融资服务乙方是一家依据中国法律合法组建和存续与宝马中国和或华晨宝马签署了授权销售合同具
178 有专营进口和或国产宝马集团产品合法资格的企业本着自愿平等互惠互利的原则甲乙双方经充分协商签署本综合授信 165 有专营进口和或国产宝马集团产品合法资格的企业本着自愿平等互惠互利的原则甲乙双方经充分协商签署本综合授信
179 额度合同本合同达成如下条款综合授信额度合同版本""".replace(" ", "").replace("\n", "") 166 额度合同本合同达成如下条款综合授信额度合同版本""".replace(" ", "").replace("\n", "")
180
181 # 首先找到第三页纸, 我们阈值设为0.5 167 # 首先找到第三页纸, 我们阈值设为0.5
182 pno, score = self.page_predict(self.ocr_results, template) 168 pno, score = self.page_predict(self.ocr_results, template)
183 if score > 0.5: 169 if score > 0.5:
...@@ -189,18 +175,15 @@ class Finder: ...@@ -189,18 +175,15 @@ class Finder:
189 if '乙方:' in text: 175 if '乙方:' in text:
190 words = text.split(':')[-1].replace('【', '[').replace('{', '[').replace('】', ']') 176 words = text.split(':')[-1].replace('【', '[').replace('{', '[').replace('】', ']')
191 dealer_name = words 177 dealer_name = words
192
193 words = self.get_key_value(self.ocr_results[pno], '统一社会信用代码或公司注册号') 178 words = self.get_key_value(self.ocr_results[pno], '统一社会信用代码或公司注册号')
194 dealer_No = words.replace('O', '0') 179 dealer_No = words.replace('O', '0')
195 return dealer_name, dealer_No 180 return dealer_name, dealer_No
196
197 def get_info_in_page_38(self): 181 def get_info_in_page_38(self):
198 """提取第38页上的经销商名称 182 """提取第38页上的经销商名称
199 """ 183 """
200 dealer_name = '' 184 dealer_name = ''
201 template = r"""宝马汽车金融中国有限公司合同编号签署页甲方宝马汽车金融中国有限公司盖章姓名姓名职务职务日期乙方汽车销售服务 185 template = r"""宝马汽车金融中国有限公司合同编号签署页甲方宝马汽车金融中国有限公司盖章姓名姓名职务职务日期乙方汽车销售服务
202 有限公司盖章姓名姓名职务职务日期综合授信额度合同版本""".replace(" ", "").replace("\n", "") 186 有限公司盖章姓名姓名职务职务日期综合授信额度合同版本""".replace(" ", "").replace("\n", "")
203
204 # 首先找到第38页纸, 我们阈值设为0.5 187 # 首先找到第38页纸, 我们阈值设为0.5
205 pno, score = self.page_predict(self.ocr_results, template) 188 pno, score = self.page_predict(self.ocr_results, template)
206 if score > 0.5: 189 if score > 0.5:
...@@ -212,7 +195,6 @@ class Finder: ...@@ -212,7 +195,6 @@ class Finder:
212 words = re.sub(r'[(())盖章《]', "", words) 195 words = re.sub(r'[(())盖章《]', "", words)
213 dealer_name = words 196 dealer_name = words
214 return dealer_name 197 return dealer_name
215
216 def get_guarantor(self): 198 def get_guarantor(self):
217 """提取第10页上保证人段落,所见即所得 199 """提取第10页上保证人段落,所见即所得
218 """ 200 """
...@@ -222,14 +204,12 @@ class Finder: ...@@ -222,14 +204,12 @@ class Finder:
222 for key in self.ocr_results[pno]: 204 for key in self.ocr_results[pno]:
223 bbox, text = self.ocr_results[pno][key] 205 bbox, text = self.ocr_results[pno][key]
224 all_texts += text 206 all_texts += text
225
226 searchObj = re.search( r'保证人\[(.*?)\]与甲方', all_texts) 207 searchObj = re.search( r'保证人\[(.*?)\]与甲方', all_texts)
227 if searchObj: 208 if searchObj:
228 words = f'[{searchObj.group(1)}]' 209 words = f'[{searchObj.group(1)}]'
229 words = words.replace('【', '[').replace('】', ']').replace(',', ',').replace('(', '(').replace(')', ')') 210 words = words.replace('【', '[').replace('】', ']').replace(',', ',').replace('(', '(').replace(')', ')')
230 guarantor = words 211 guarantor = words
231 return guarantor 212 return guarantor
232
233 def get_info_in_page_39(self): 213 def get_info_in_page_39(self):
234 """提取综合授信合同上的一些字段 214 """提取综合授信合同上的一些字段
235 """ 215 """
...@@ -242,7 +222,6 @@ class Finder: ...@@ -242,7 +222,6 @@ class Finder:
242 term_end_chn = '' 222 term_end_chn = ''
243 deposit_eng = '' 223 deposit_eng = ''
244 deposit_chn = '' 224 deposit_chn = ''
245
246 template = r"""合同编号中国有限公司宝马汽车金融综合授信额度合同附件确认函综合授信额度金额本合同项下的综合授信额度为人民币 225 template = r"""合同编号中国有限公司宝马汽车金融综合授信额度合同附件确认函综合授信额度金额本合同项下的综合授信额度为人民币
247 大写综合授信额度下面各个业务的授信额度将由甲方以授信额度通知函的方式时不时的通知乙方本合同项下的综合授信额 226 大写综合授信额度下面各个业务的授信额度将由甲方以授信额度通知函的方式时不时的通知乙方本合同项下的综合授信额
248 度可以由甲方根据乙方的信用和财务状况自行决定随时调整本合同项下的综合授信额度应为在本确认函第条的期间内双方 227 度可以由甲方根据乙方的信用和财务状况自行决定随时调整本合同项下的综合授信额度应为在本确认函第条的期间内双方
...@@ -250,7 +229,6 @@ class Finder: ...@@ -250,7 +229,6 @@ class Finder:
250 综合授信额度期限从至或者由甲方向乙方通过书面形式在授信额度通知函中沟通的更短期间保证金甲方对乙方的最低保证 229 综合授信额度期限从至或者由甲方向乙方通过书面形式在授信额度通知函中沟通的更短期间保证金甲方对乙方的最低保证
251 金要求为综合授信额度的实际执行的保证金比例以甲方不时另行书面通知根据最新的经销商融资或保证金相关政策或活动 230 金要求为综合授信额度的实际执行的保证金比例以甲方不时另行书面通知根据最新的经销商融资或保证金相关政策或活动
252 为准综合授信额度合同版本""".replace(" ", "").replace("\n", "") 231 为准综合授信额度合同版本""".replace(" ", "").replace("\n", "")
253
254 # 首先找到综合授信合同第一面, 我们阈值设为0.5 232 # 首先找到综合授信合同第一面, 我们阈值设为0.5
255 pno, score = self.page_predict(self.ocr_results, template) 233 pno, score = self.page_predict(self.ocr_results, template)
256 if score > 0.5: 234 if score > 0.5:
...@@ -264,7 +242,6 @@ class Finder: ...@@ -264,7 +242,6 @@ class Finder:
264 if searchObj: 242 if searchObj:
265 words = searchObj.group() 243 words = searchObj.group()
266 amount_eng = words 244 amount_eng = words
267
268 lines = self.get_line(self.ocr_results[pno], '人民币') 245 lines = self.get_line(self.ocr_results[pno], '人民币')
269 searchObj = re.search( r'大写(.*?)综合', lines) 246 searchObj = re.search( r'大写(.*?)综合', lines)
270 if searchObj: 247 if searchObj:
...@@ -290,7 +267,6 @@ class Finder: ...@@ -290,7 +267,6 @@ class Finder:
290 if searchEnd: 267 if searchEnd:
291 words = searchEnd.group() 268 words = searchEnd.group()
292 term_end_eng = words 269 term_end_eng = words
293
294 lines = self.get_line(self.ocr_results[pno], '至') 270 lines = self.get_line(self.ocr_results[pno], '至')
295 if len(lines) > 0: 271 if len(lines) > 0:
296 start, end = lines.split('至') 272 start, end = lines.split('至')
...@@ -308,55 +284,44 @@ class Finder: ...@@ -308,55 +284,44 @@ class Finder:
308 if searchObj: 284 if searchObj:
309 words = searchObj.group(1) 285 words = searchObj.group(1)
310 deposit_eng = f'{words}%' 286 deposit_eng = f'{words}%'
311
312 lines = self.get_line(self.ocr_results[pno], '授信额度的') 287 lines = self.get_line(self.ocr_results[pno], '授信额度的')
313 searchObj = re.search( r'授信额度的([0-9]+)', lines.replace('O', '0')) 288 searchObj = re.search( r'授信额度的([0-9]+)', lines.replace('O', '0').replace('_', ''))
314 if searchObj: 289 if searchObj:
315 words = searchObj.group(1) 290 words = searchObj.group(1)
316 deposit_chn = f'{words}%' 291 deposit_chn = f'{words}%'
317
318 return amount_eng, amount_chn, term_start_eng, term_end_eng, \ 292 return amount_eng, amount_chn, term_start_eng, term_end_eng, \
319 term_start_chn, term_end_chn, deposit_eng, deposit_chn 293 term_start_chn, term_end_chn, deposit_eng, deposit_chn
320
321 def get_other_arrangements_and_conditions(self): 294 def get_other_arrangements_and_conditions(self):
322 """获取其它约定与条件文本段落 295 """获取其它约定与条件文本段落
323 """ 296 """
324 other_arrangements_and_conditions_eng = '' 297 other_arrangements_and_conditions_eng = ''
325 other_arrangements_and_conditions_chn = '' 298 other_arrangements_and_conditions_chn = ''
326
327 all_texts = '' 299 all_texts = ''
328 for pno in self.ocr_results: 300 for pno in self.ocr_results:
329 for key in self.ocr_results[pno]: 301 for key in self.ocr_results[pno]:
330 all_texts += self.ocr_results[pno][key][1] 302 all_texts += self.ocr_results[pno][key][1]
331 303 searchObj = re.search(r'Conditions:(.*?)其他', all_texts, re.I)
332 searchObj = re.search(r'Conditions:(.*?)其他约定与条件', all_texts, re.I)
333 if searchObj: 304 if searchObj:
334 words = searchObj.group(1) 305 words = searchObj.group(1)
335 pattern = re.compile("[\u4e00-\u9fa5]") # 去除中文字符 306 pattern = re.compile("[\u4e00-\u9fa5]") # 去除中文字符
336 words = pattern.sub('', words) 307 words = pattern.sub('', words)
337 other_arrangements_and_conditions_eng = words 308 other_arrangements_and_conditions_eng = words
338
339 searchObj = re.search(r'条件:(.*?)General', all_texts, re.I) 309 searchObj = re.search(r'条件:(.*?)General', all_texts, re.I)
340 if searchObj: 310 if searchObj:
341 words = searchObj.group(1) 311 words = searchObj.group(1)
342 other_arrangements_and_conditions_chn = words 312 other_arrangements_and_conditions_chn = words
343 return other_arrangements_and_conditions_eng, other_arrangements_and_conditions_chn 313 return other_arrangements_and_conditions_eng, other_arrangements_and_conditions_chn
344
345 def get_info(self): 314 def get_info(self):
346 # 按照文档页码返回一个合同编号列表,依次表示每一页上识别到的合同编号 315 # 按照文档页码返回一个合同编号列表,依次表示每一页上识别到的合同编号
347 contract_No_list = self.get_contract_No() 316 contract_No_list = self.get_contract_No()
348 self.init_result["合同编号列表"] = contract_No_list 317 self.init_result["合同编号列表"] = contract_No_list
349
350 dealer_name, dealer_No = self.get_info_in_page_3() 318 dealer_name, dealer_No = self.get_info_in_page_3()
351 self.init_result["经销商名称_Page3"] = dealer_name 319 self.init_result["经销商名称_Page3"] = dealer_name
352 self.init_result["经销商统一社会信用代码或公司注册号"] = dealer_No 320 self.init_result["经销商统一社会信用代码或公司注册号"] = dealer_No
353
354 dealer_name = self.get_info_in_page_38() 321 dealer_name = self.get_info_in_page_38()
355 self.init_result["经销商名称_Page38"] = dealer_name 322 self.init_result["经销商名称_Page38"] = dealer_name
356
357 guarantor = self.get_guarantor() 323 guarantor = self.get_guarantor()
358 self.init_result["保证人"] = guarantor 324 self.init_result["保证人"] = guarantor
359
360 amount_eng, amount_chn, term_start_eng, term_end_eng, \ 325 amount_eng, amount_chn, term_start_eng, term_end_eng, \
361 term_start_chn, term_end_chn, deposit_eng, deposit_chn = self.get_info_in_page_39() 326 term_start_chn, term_end_chn, deposit_eng, deposit_chn = self.get_info_in_page_39()
362 self.init_result["综合授信额度金额英文"] = amount_eng 327 self.init_result["综合授信额度金额英文"] = amount_eng
...@@ -367,7 +332,6 @@ class Finder: ...@@ -367,7 +332,6 @@ class Finder:
367 self.init_result["综合授信额度期限截止日期中文"] = term_end_chn 332 self.init_result["综合授信额度期限截止日期中文"] = term_end_chn
368 self.init_result["保证金比例英文"] = deposit_eng 333 self.init_result["保证金比例英文"] = deposit_eng
369 self.init_result["保证金比例中文"] = deposit_chn 334 self.init_result["保证金比例中文"] = deposit_chn
370
371 words_eng, words_chn = self.get_other_arrangements_and_conditions() 335 words_eng, words_chn = self.get_other_arrangements_and_conditions()
372 self.init_result["其他约定与条件英文"] = words_eng 336 self.init_result["其他约定与条件英文"] = words_eng
373 self.init_result["其他约定与条件中文"] = words_chn 337 self.init_result["其他约定与条件中文"] = words_chn
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!