comparison.py
14 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import re
import time
import numpy as np
from datetime import datetime
from dateutil.relativedelta import relativedelta
from .rmb_lower import rmb_handler
# from .rmb_upper import to_rmb_upper
from pandas._libs import tslib
from pandas._libs.tslibs.nattype import NaTType
from pandas.core.indexes.datetimes import DatetimeIndex
class Comparison:
def __init__(self):
self.CSIBM = 'CSIBM'
self.CSSME = 'CSSME'
self.CSOTH = 'CSOTH'
self.SPLIT_STR = '_'
self.TYPE_MAPPING = (
(r'个体工商户', self.CSIBM),
(r'有限责任公司', self.CSSME),
(r'个人独资企业', self.CSSME),
(r'有限合伙企业', self.CSSME),
(r'股份合作制', self.CSSME),
)
self.RESULT_Y = 'Y'
self.RESULT_N = 'N'
self.RESULT_NA = 'NA'
self.TRANS_MAP = {
' ': '',
'·': '',
}
self.TRANS = str.maketrans(self.TRANS_MAP)
self.re_obj = r'[(\(].*?[\))]'
def build_res(self, result):
if result:
return self.RESULT_Y
else:
return self.RESULT_N
def common_compare(self, input_str, ocr_str, idx, **kwargs):
if not isinstance(ocr_str, str) or not isinstance(input_str, str):
return self.RESULT_NA, ocr_str
if ocr_str == '' or ocr_str.strip() == '':
return self.RESULT_NA, None
return self.build_res(input_str == ocr_str), ocr_str
def company_compare(self, input_str, ocr_str, idx, **kwargs):
if not isinstance(ocr_str, str) or not isinstance(input_str, str):
return self.RESULT_NA, ocr_str
if ocr_str == '' or ocr_str.strip() == '':
return self.RESULT_NA, None
input_tmp = re.sub(self.re_obj, '', input_str).strip()
ocr_tmp = re.sub(self.re_obj, '', ocr_str).strip()
return self.build_res(input_tmp == ocr_tmp), ocr_str
def name_compare(self, input_str, ocr_str, idx, **kwargs):
if not isinstance(ocr_str, str) or not isinstance(input_str, str):
return self.RESULT_NA, ocr_str
if ocr_str == '' or ocr_str.strip() == '':
return self.RESULT_NA, None
if kwargs.get('is_passport'):
input_tmp = input_str.upper().replace(' ', '')
ocr_tmp = ocr_str.upper().replace(' ', '')
if input_tmp.find(ocr_tmp) == -1:
return self.RESULT_N, ocr_str
else:
return self.RESULT_Y, ocr_str
else:
# if re.search(r'[a-zA-Z]]', input_str):
# return self.RESULT_NA, ocr_str
input_s = input_str.translate(self.TRANS)
ocr_s = ocr_str.translate(self.TRANS)
return self.build_res(input_s == ocr_s), ocr_str
def date_compare(self, input_str, ocr_str, idx, **kwargs):
if not isinstance(ocr_str, str) or not isinstance(input_str, str):
return self.RESULT_NA, ocr_str
if ocr_str == '' or ocr_str.strip() == '':
return self.RESULT_NA, None
if kwargs.get('long', False):
if '长期' in ocr_str or '永久' in ocr_str:
if input_str == '2099-12-31' or input_str == '2099-01-01':
return self.RESULT_Y, '2099-12-31'
else:
return self.RESULT_N, '2099-12-31'
if kwargs.get('ocr_split', False):
if '至' in ocr_str:
ocr_str = ocr_str.split('至')[-1]
elif '-' in ocr_str:
ocr_str = ocr_str.split('-')[-1]
if kwargs.get('ocr_replace', False):
ocr_str = ocr_str.replace('年', '-').replace('月', '-').replace('日', '')
if kwargs.get('input_replace') is not None:
input_str = input_str.replace('-', kwargs.get('input_replace'))
try:
ocr_output = datetime.strptime(ocr_str, '%Y{0}%m{0}%d'.format(
kwargs.get('input_replace'))).strftime('%Y-%m-%d')
except Exception as e:
ocr_output = None
else:
try:
ocr_output = datetime.strptime(ocr_str, '%Y-%m-%d').strftime('%Y-%m-%d')
except Exception as e:
ocr_output = None
return self.build_res(input_str == ocr_str), ocr_output
def mvi_special(self, amount_lower_str, amount_upper_str, bhsj_str, zzsse_str):
# 不含税价, 增值税税额
try:
if float(amount_lower_str) != rmb_handler.to_rmb_lower(amount_upper_str):
return self.RESULT_N
except Exception:
return self.RESULT_N
else:
return self.SPLIT_STR.join([bhsj_str, zzsse_str, self.RESULT_Y])
def rmb_compare(self, input_str, ocr_str, idx, **kwargs):
if not isinstance(ocr_str, str) or not isinstance(input_str, str):
return self.RESULT_NA, None
if ocr_str == '' or ocr_str.strip() == '':
return self.RESULT_NA, None
try:
ocr_lower = rmb_handler.to_rmb_lower(ocr_str)
res = self.build_res(float(input_str) == ocr_lower)
# input_rmb_upper = to_rmb_upper(float(input_str))
# res = self.build_res(input_rmb_upper == ocr_str)
except Exception as e:
return self.RESULT_N, None
else:
if res == self.RESULT_Y:
return res, input_str
else:
return res, ocr_lower
# return res, None
def type_compare(self, input_str, ocr_str, idx, **kwargs):
if not isinstance(ocr_str, str) or not isinstance(input_str, str):
return self.RESULT_NA, ocr_str
if ocr_str == '' or ocr_str.strip() == '':
return self.RESULT_NA, None
for map_tuple in self.TYPE_MAPPING:
if re.search(map_tuple[0], ocr_str) is not None:
compare_str = map_tuple[1]
break
else:
compare_str = self.CSOTH
return self.build_res(input_str == compare_str), compare_str
def se_name_compare(self, input_str, ocr_str, **kwargs):
if kwargs.get('is_passport'):
input_tmp = input_str.upper().replace(' ', '')
ocr_tmp = ocr_str.upper().replace(' ', '')
if input_tmp.find(ocr_tmp) == -1:
return self.RESULT_N
else:
if ocr_str.strip() == '':
return self.RESULT_N
else:
return self.RESULT_Y
else:
# if re.search(r'[a-zA-Z]]', input_str):
# return self.RESULT_NA, ocr_str
input_s = input_str.translate(self.TRANS)
ocr_s = ocr_str.translate(self.TRANS)
return self.build_res(input_s == ocr_s)
def ca_name_compare(self, input_str, ocr_str, **kwargs):
if kwargs.get('is_passport'):
input_tmp = input_str.upper().replace(' ', '')
ocr_tmp = ocr_str.upper().replace(' ', '')
if input_tmp.find(ocr_tmp) == -1:
return self.RESULT_N
else:
if ocr_str.strip() == '':
return self.RESULT_N
else:
return self.RESULT_Y
else:
# if re.search(r'[a-zA-Z]]', input_str):
# return self.RESULT_NA, ocr_str
input_s = input_str.translate(self.TRANS)
ocr_s = ocr_str.translate(self.TRANS)
return self.build_res(input_s == ocr_s)
def se_common_compare(self, input_str, ocr_str, **kwargs):
if kwargs.get('remove_space', False):
input_str = input_str.replace(' ', '')
return self.build_res(input_str == ocr_str)
def ca_common_compare(self, input_str, ocr_str, **kwargs):
return self.build_res(input_str == ocr_str)
@staticmethod
def is_after_today(ocr_str):
dt_array, _ = tslib.array_to_datetime(
np.array([ocr_str, ], copy=False, dtype=np.object_),
errors="coerce",
utc=False,
dayfirst=False,
yearfirst=False,
require_iso8601=True,
)
dti = DatetimeIndex(dt_array, tz=None, name=None)
ts = dti[0]
if isinstance(ts, NaTType) or ts.date() < (datetime.today() + relativedelta(days=8)).date():
return False
else:
return True
def se_date_compare(self, input_str, ocr_str, **kwargs):
if kwargs.get('long', False):
if '长期' in ocr_str or '永久' in ocr_str or '***' in ocr_str or '至今' in ocr_str or '年—月—日' in ocr_str or '年 月 日' in ocr_str:
if kwargs.get('today', False) or input_str in ['2099-12-31', '2099-01-01', '2999-12-31', '2999-01-01']:
return self.RESULT_Y
else:
return self.RESULT_N
if kwargs.get('ocr_split', False):
if '至' in ocr_str:
ocr_str = ocr_str.split('至')[-1]
elif '-' in ocr_str:
ocr_str = ocr_str.split('-')[-1]
if kwargs.get('ocr_replace', False):
ocr_str = ocr_str.replace('年', '-').replace('月', '-').replace('日', '')
if kwargs.get('input_replace') is not None:
input_str = input_str.replace('-', kwargs.get('input_replace'))
if kwargs.get('today', False):
return self.build_res(self.is_after_today(ocr_str))
else:
return self.build_res(input_str == ocr_str)
def ca_date_compare(self, input_str, ocr_str, **kwargs):
if kwargs.get('long', False):
if '长期' in ocr_str or '永久' in ocr_str:
if input_str in ['2099-12-31', '2099-01-01']:
return self.RESULT_Y
else:
return self.RESULT_N
if kwargs.get('ocr_split', False):
if '至' in ocr_str:
ocr_str = ocr_str.split('至')[-1]
elif '-' in ocr_str:
ocr_str = ocr_str.split('-')[-1]
if kwargs.get('ocr_replace', False):
ocr_str = ocr_str.replace('年', '-').replace('月', '-').replace('日', '')
if kwargs.get('input_replace') is not None:
input_str = input_str.replace('-', kwargs.get('input_replace'))
return self.build_res(input_str == ocr_str)
def se_contain_compare(self, input_str, ocr_str, **kwargs):
if ocr_str.find(input_str) == -1:
return self.RESULT_N
else:
if ocr_str.strip() == '':
return self.RESULT_N
else:
return self.RESULT_Y
def se_contain_compare_2(self, input_str, ocr_str, **kwargs):
if input_str.find(ocr_str) == -1:
return self.RESULT_N
else:
if ocr_str.strip() == '':
return self.RESULT_N
else:
return self.RESULT_Y
def se_both_contain_compare(self, input_str, ocr_str, **kwargs):
if ocr_str.find(input_str) == -1 and input_str.find(ocr_str) == -1:
return self.RESULT_N
else:
if ocr_str.strip() == '':
return self.RESULT_N
else:
return self.RESULT_Y
def se_amount_compare(self, input_str, ocr_str, **kwargs):
if input_str == ocr_str:
return self.RESULT_Y
else:
try:
float_input = float(input_str)
float_ocr = float(ocr_str)
except Exception as e:
return self.RESULT_N
else:
return self.build_res(float_ocr == float_input)
def se_company_compare(self, input_str, ocr_str, **kwargs):
input_tmp = re.sub(self.re_obj, '', input_str).strip()
ocr_tmp = re.sub(self.re_obj, '', ocr_str).strip()
return self.build_res(input_tmp == ocr_tmp)
def ca_company_compare(self, input_str, ocr_str, **kwargs):
input_tmp = re.sub(self.re_obj, '', input_str).strip()
ocr_tmp = re.sub(self.re_obj, '', ocr_str).strip()
return self.build_res(input_tmp == ocr_tmp)
def se_rmb_compare(self, input_str, ocr_str, **kwargs):
try:
ocr_lower = rmb_handler.to_rmb_lower(ocr_str)
res = self.build_res(float(input_str) == ocr_lower)
# input_rmb_upper = to_rmb_upper(float(input_str))
# res = self.build_res(input_rmb_upper == ocr_str)
except Exception as e:
return self.RESULT_N
else:
return res
def ca_rmb_compare(self, input_str, ocr_str, **kwargs):
try:
ocr_lower = rmb_handler.to_rmb_lower(ocr_str)
res = self.build_res(float(input_str) == ocr_lower)
# input_rmb_upper = to_rmb_upper(float(input_str))
# res = self.build_res(input_rmb_upper == ocr_str)
except Exception as e:
return self.RESULT_N
else:
return res
def se_type_compare(self, input_str, ocr_str, **kwargs):
for map_tuple in self.TYPE_MAPPING:
if re.search(map_tuple[0], ocr_str) is not None:
compare_str = map_tuple[1]
break
else:
compare_str = self.CSOTH
return self.build_res(input_str == compare_str)
def ca_type_compare(self, input_str, ocr_str, **kwargs):
for map_tuple in self.TYPE_MAPPING:
if re.search(map_tuple[0], ocr_str) is not None:
compare_str = map_tuple[1]
break
else:
compare_str = self.CSOTH
return self.build_res(input_str == compare_str)
def se_date_compare_2(self, input_str, ocr_str, **kwargs):
try:
input_date = datetime.strptime(input_str, "%Y-%m-%d").date()
if kwargs.get('three_month', False):
three_month_date = (datetime.today() - relativedelta(months=3)).date()
compare_date = max(input_date, three_month_date)
else:
compare_date = input_date
ocr_date = datetime.strptime(ocr_str, "%Y-%m-%d").date()
except Exception as e:
return self.RESULT_N
else:
return self.build_res(compare_date <= ocr_date)
cp = Comparison()