pyxllib 0.3.96__py3-none-any.whl → 0.3.197__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyxllib/algo/geo.py +12 -0
- pyxllib/algo/intervals.py +1 -1
- pyxllib/algo/matcher.py +78 -0
- pyxllib/algo/pupil.py +187 -19
- pyxllib/algo/specialist.py +2 -1
- pyxllib/algo/stat.py +38 -2
- {pyxlpr → pyxllib/autogui}/__init__.py +1 -1
- pyxllib/autogui/activewin.py +246 -0
- pyxllib/autogui/all.py +9 -0
- pyxllib/{ext/autogui → autogui}/autogui.py +40 -11
- pyxllib/autogui/uiautolib.py +362 -0
- pyxllib/autogui/wechat.py +827 -0
- pyxllib/autogui/wechat_msg.py +421 -0
- pyxllib/autogui/wxautolib.py +84 -0
- pyxllib/cv/slidercaptcha.py +137 -0
- pyxllib/data/echarts.py +123 -12
- pyxllib/data/jsonlib.py +89 -0
- pyxllib/data/pglib.py +514 -30
- pyxllib/data/sqlite.py +231 -4
- pyxllib/ext/JLineViewer.py +14 -1
- pyxllib/ext/drissionlib.py +277 -0
- pyxllib/ext/kq5034lib.py +0 -1594
- pyxllib/ext/robustprocfile.py +497 -0
- pyxllib/ext/unixlib.py +6 -5
- pyxllib/ext/utools.py +108 -95
- pyxllib/ext/webhook.py +32 -14
- pyxllib/ext/wjxlib.py +88 -0
- pyxllib/ext/wpsapi.py +124 -0
- pyxllib/ext/xlwork.py +9 -0
- pyxllib/ext/yuquelib.py +1003 -71
- pyxllib/file/docxlib.py +1 -1
- pyxllib/file/libreoffice.py +165 -0
- pyxllib/file/movielib.py +9 -0
- pyxllib/file/packlib/__init__.py +112 -75
- pyxllib/file/pdflib.py +1 -1
- pyxllib/file/pupil.py +1 -1
- pyxllib/file/specialist/dirlib.py +1 -1
- pyxllib/file/specialist/download.py +10 -3
- pyxllib/file/specialist/filelib.py +266 -55
- pyxllib/file/xlsxlib.py +205 -50
- pyxllib/file/xlsyncfile.py +341 -0
- pyxllib/prog/cachetools.py +64 -0
- pyxllib/prog/filelock.py +42 -0
- pyxllib/prog/multiprogs.py +940 -0
- pyxllib/prog/newbie.py +9 -2
- pyxllib/prog/pupil.py +129 -60
- pyxllib/prog/specialist/__init__.py +176 -2
- pyxllib/prog/specialist/bc.py +5 -2
- pyxllib/prog/specialist/browser.py +11 -2
- pyxllib/prog/specialist/datetime.py +68 -0
- pyxllib/prog/specialist/tictoc.py +12 -13
- pyxllib/prog/specialist/xllog.py +5 -5
- pyxllib/prog/xlosenv.py +7 -0
- pyxllib/text/airscript.js +744 -0
- pyxllib/text/charclasslib.py +17 -5
- pyxllib/text/jiebalib.py +6 -3
- pyxllib/text/jinjalib.py +32 -0
- pyxllib/text/jsa_ai_prompt.md +271 -0
- pyxllib/text/jscode.py +159 -4
- pyxllib/text/nestenv.py +1 -1
- pyxllib/text/newbie.py +12 -0
- pyxllib/text/pupil/common.py +26 -0
- pyxllib/text/specialist/ptag.py +2 -2
- pyxllib/text/templates/echart_base.html +11 -0
- pyxllib/text/templates/highlight_code.html +17 -0
- pyxllib/text/templates/latex_editor.html +103 -0
- pyxllib/text/xmllib.py +76 -14
- pyxllib/xl.py +2 -1
- pyxllib-0.3.197.dist-info/METADATA +48 -0
- pyxllib-0.3.197.dist-info/RECORD +126 -0
- {pyxllib-0.3.96.dist-info → pyxllib-0.3.197.dist-info}/WHEEL +1 -2
- pyxllib/ext/autogui/__init__.py +0 -8
- pyxllib-0.3.96.dist-info/METADATA +0 -51
- pyxllib-0.3.96.dist-info/RECORD +0 -333
- pyxllib-0.3.96.dist-info/top_level.txt +0 -2
- pyxlpr/ai/__init__.py +0 -5
- pyxlpr/ai/clientlib.py +0 -1281
- pyxlpr/ai/specialist.py +0 -286
- pyxlpr/ai/torch_app.py +0 -172
- pyxlpr/ai/xlpaddle.py +0 -655
- pyxlpr/ai/xltorch.py +0 -705
- pyxlpr/data/__init__.py +0 -11
- pyxlpr/data/coco.py +0 -1325
- pyxlpr/data/datacls.py +0 -365
- pyxlpr/data/datasets.py +0 -200
- pyxlpr/data/gptlib.py +0 -1291
- pyxlpr/data/icdar/__init__.py +0 -96
- pyxlpr/data/icdar/deteval.py +0 -377
- pyxlpr/data/icdar/icdar2013.py +0 -341
- pyxlpr/data/icdar/iou.py +0 -340
- pyxlpr/data/icdar/rrc_evaluation_funcs_1_1.py +0 -463
- pyxlpr/data/imtextline.py +0 -473
- pyxlpr/data/labelme.py +0 -866
- pyxlpr/data/removeline.py +0 -179
- pyxlpr/data/specialist.py +0 -57
- pyxlpr/eval/__init__.py +0 -85
- pyxlpr/paddleocr.py +0 -776
- pyxlpr/ppocr/__init__.py +0 -15
- pyxlpr/ppocr/configs/rec/multi_language/generate_multi_language_configs.py +0 -226
- pyxlpr/ppocr/data/__init__.py +0 -135
- pyxlpr/ppocr/data/imaug/ColorJitter.py +0 -26
- pyxlpr/ppocr/data/imaug/__init__.py +0 -67
- pyxlpr/ppocr/data/imaug/copy_paste.py +0 -170
- pyxlpr/ppocr/data/imaug/east_process.py +0 -437
- pyxlpr/ppocr/data/imaug/gen_table_mask.py +0 -244
- pyxlpr/ppocr/data/imaug/iaa_augment.py +0 -114
- pyxlpr/ppocr/data/imaug/label_ops.py +0 -789
- pyxlpr/ppocr/data/imaug/make_border_map.py +0 -184
- pyxlpr/ppocr/data/imaug/make_pse_gt.py +0 -106
- pyxlpr/ppocr/data/imaug/make_shrink_map.py +0 -126
- pyxlpr/ppocr/data/imaug/operators.py +0 -433
- pyxlpr/ppocr/data/imaug/pg_process.py +0 -906
- pyxlpr/ppocr/data/imaug/randaugment.py +0 -143
- pyxlpr/ppocr/data/imaug/random_crop_data.py +0 -239
- pyxlpr/ppocr/data/imaug/rec_img_aug.py +0 -533
- pyxlpr/ppocr/data/imaug/sast_process.py +0 -777
- pyxlpr/ppocr/data/imaug/text_image_aug/__init__.py +0 -17
- pyxlpr/ppocr/data/imaug/text_image_aug/augment.py +0 -120
- pyxlpr/ppocr/data/imaug/text_image_aug/warp_mls.py +0 -168
- pyxlpr/ppocr/data/lmdb_dataset.py +0 -115
- pyxlpr/ppocr/data/pgnet_dataset.py +0 -104
- pyxlpr/ppocr/data/pubtab_dataset.py +0 -107
- pyxlpr/ppocr/data/simple_dataset.py +0 -372
- pyxlpr/ppocr/losses/__init__.py +0 -61
- pyxlpr/ppocr/losses/ace_loss.py +0 -52
- pyxlpr/ppocr/losses/basic_loss.py +0 -135
- pyxlpr/ppocr/losses/center_loss.py +0 -88
- pyxlpr/ppocr/losses/cls_loss.py +0 -30
- pyxlpr/ppocr/losses/combined_loss.py +0 -67
- pyxlpr/ppocr/losses/det_basic_loss.py +0 -208
- pyxlpr/ppocr/losses/det_db_loss.py +0 -80
- pyxlpr/ppocr/losses/det_east_loss.py +0 -63
- pyxlpr/ppocr/losses/det_pse_loss.py +0 -149
- pyxlpr/ppocr/losses/det_sast_loss.py +0 -121
- pyxlpr/ppocr/losses/distillation_loss.py +0 -272
- pyxlpr/ppocr/losses/e2e_pg_loss.py +0 -140
- pyxlpr/ppocr/losses/kie_sdmgr_loss.py +0 -113
- pyxlpr/ppocr/losses/rec_aster_loss.py +0 -99
- pyxlpr/ppocr/losses/rec_att_loss.py +0 -39
- pyxlpr/ppocr/losses/rec_ctc_loss.py +0 -44
- pyxlpr/ppocr/losses/rec_enhanced_ctc_loss.py +0 -70
- pyxlpr/ppocr/losses/rec_nrtr_loss.py +0 -30
- pyxlpr/ppocr/losses/rec_sar_loss.py +0 -28
- pyxlpr/ppocr/losses/rec_srn_loss.py +0 -47
- pyxlpr/ppocr/losses/table_att_loss.py +0 -109
- pyxlpr/ppocr/metrics/__init__.py +0 -44
- pyxlpr/ppocr/metrics/cls_metric.py +0 -45
- pyxlpr/ppocr/metrics/det_metric.py +0 -82
- pyxlpr/ppocr/metrics/distillation_metric.py +0 -73
- pyxlpr/ppocr/metrics/e2e_metric.py +0 -86
- pyxlpr/ppocr/metrics/eval_det_iou.py +0 -274
- pyxlpr/ppocr/metrics/kie_metric.py +0 -70
- pyxlpr/ppocr/metrics/rec_metric.py +0 -75
- pyxlpr/ppocr/metrics/table_metric.py +0 -50
- pyxlpr/ppocr/modeling/architectures/__init__.py +0 -32
- pyxlpr/ppocr/modeling/architectures/base_model.py +0 -88
- pyxlpr/ppocr/modeling/architectures/distillation_model.py +0 -60
- pyxlpr/ppocr/modeling/backbones/__init__.py +0 -54
- pyxlpr/ppocr/modeling/backbones/det_mobilenet_v3.py +0 -268
- pyxlpr/ppocr/modeling/backbones/det_resnet_vd.py +0 -246
- pyxlpr/ppocr/modeling/backbones/det_resnet_vd_sast.py +0 -285
- pyxlpr/ppocr/modeling/backbones/e2e_resnet_vd_pg.py +0 -265
- pyxlpr/ppocr/modeling/backbones/kie_unet_sdmgr.py +0 -186
- pyxlpr/ppocr/modeling/backbones/rec_mobilenet_v3.py +0 -138
- pyxlpr/ppocr/modeling/backbones/rec_mv1_enhance.py +0 -258
- pyxlpr/ppocr/modeling/backbones/rec_nrtr_mtb.py +0 -48
- pyxlpr/ppocr/modeling/backbones/rec_resnet_31.py +0 -210
- pyxlpr/ppocr/modeling/backbones/rec_resnet_aster.py +0 -143
- pyxlpr/ppocr/modeling/backbones/rec_resnet_fpn.py +0 -307
- pyxlpr/ppocr/modeling/backbones/rec_resnet_vd.py +0 -286
- pyxlpr/ppocr/modeling/heads/__init__.py +0 -54
- pyxlpr/ppocr/modeling/heads/cls_head.py +0 -52
- pyxlpr/ppocr/modeling/heads/det_db_head.py +0 -118
- pyxlpr/ppocr/modeling/heads/det_east_head.py +0 -121
- pyxlpr/ppocr/modeling/heads/det_pse_head.py +0 -37
- pyxlpr/ppocr/modeling/heads/det_sast_head.py +0 -128
- pyxlpr/ppocr/modeling/heads/e2e_pg_head.py +0 -253
- pyxlpr/ppocr/modeling/heads/kie_sdmgr_head.py +0 -206
- pyxlpr/ppocr/modeling/heads/multiheadAttention.py +0 -163
- pyxlpr/ppocr/modeling/heads/rec_aster_head.py +0 -393
- pyxlpr/ppocr/modeling/heads/rec_att_head.py +0 -202
- pyxlpr/ppocr/modeling/heads/rec_ctc_head.py +0 -88
- pyxlpr/ppocr/modeling/heads/rec_nrtr_head.py +0 -826
- pyxlpr/ppocr/modeling/heads/rec_sar_head.py +0 -402
- pyxlpr/ppocr/modeling/heads/rec_srn_head.py +0 -280
- pyxlpr/ppocr/modeling/heads/self_attention.py +0 -406
- pyxlpr/ppocr/modeling/heads/table_att_head.py +0 -246
- pyxlpr/ppocr/modeling/necks/__init__.py +0 -32
- pyxlpr/ppocr/modeling/necks/db_fpn.py +0 -111
- pyxlpr/ppocr/modeling/necks/east_fpn.py +0 -188
- pyxlpr/ppocr/modeling/necks/fpn.py +0 -138
- pyxlpr/ppocr/modeling/necks/pg_fpn.py +0 -314
- pyxlpr/ppocr/modeling/necks/rnn.py +0 -92
- pyxlpr/ppocr/modeling/necks/sast_fpn.py +0 -284
- pyxlpr/ppocr/modeling/necks/table_fpn.py +0 -110
- pyxlpr/ppocr/modeling/transforms/__init__.py +0 -28
- pyxlpr/ppocr/modeling/transforms/stn.py +0 -135
- pyxlpr/ppocr/modeling/transforms/tps.py +0 -308
- pyxlpr/ppocr/modeling/transforms/tps_spatial_transformer.py +0 -156
- pyxlpr/ppocr/optimizer/__init__.py +0 -61
- pyxlpr/ppocr/optimizer/learning_rate.py +0 -228
- pyxlpr/ppocr/optimizer/lr_scheduler.py +0 -49
- pyxlpr/ppocr/optimizer/optimizer.py +0 -160
- pyxlpr/ppocr/optimizer/regularizer.py +0 -52
- pyxlpr/ppocr/postprocess/__init__.py +0 -55
- pyxlpr/ppocr/postprocess/cls_postprocess.py +0 -33
- pyxlpr/ppocr/postprocess/db_postprocess.py +0 -234
- pyxlpr/ppocr/postprocess/east_postprocess.py +0 -143
- pyxlpr/ppocr/postprocess/locality_aware_nms.py +0 -200
- pyxlpr/ppocr/postprocess/pg_postprocess.py +0 -52
- pyxlpr/ppocr/postprocess/pse_postprocess/__init__.py +0 -15
- pyxlpr/ppocr/postprocess/pse_postprocess/pse/__init__.py +0 -29
- pyxlpr/ppocr/postprocess/pse_postprocess/pse/setup.py +0 -14
- pyxlpr/ppocr/postprocess/pse_postprocess/pse_postprocess.py +0 -118
- pyxlpr/ppocr/postprocess/rec_postprocess.py +0 -654
- pyxlpr/ppocr/postprocess/sast_postprocess.py +0 -355
- pyxlpr/ppocr/tools/__init__.py +0 -14
- pyxlpr/ppocr/tools/eval.py +0 -83
- pyxlpr/ppocr/tools/export_center.py +0 -77
- pyxlpr/ppocr/tools/export_model.py +0 -129
- pyxlpr/ppocr/tools/infer/predict_cls.py +0 -151
- pyxlpr/ppocr/tools/infer/predict_det.py +0 -300
- pyxlpr/ppocr/tools/infer/predict_e2e.py +0 -169
- pyxlpr/ppocr/tools/infer/predict_rec.py +0 -414
- pyxlpr/ppocr/tools/infer/predict_system.py +0 -204
- pyxlpr/ppocr/tools/infer/utility.py +0 -629
- pyxlpr/ppocr/tools/infer_cls.py +0 -83
- pyxlpr/ppocr/tools/infer_det.py +0 -134
- pyxlpr/ppocr/tools/infer_e2e.py +0 -122
- pyxlpr/ppocr/tools/infer_kie.py +0 -153
- pyxlpr/ppocr/tools/infer_rec.py +0 -146
- pyxlpr/ppocr/tools/infer_table.py +0 -107
- pyxlpr/ppocr/tools/program.py +0 -596
- pyxlpr/ppocr/tools/test_hubserving.py +0 -117
- pyxlpr/ppocr/tools/train.py +0 -163
- pyxlpr/ppocr/tools/xlprog.py +0 -748
- pyxlpr/ppocr/utils/EN_symbol_dict.txt +0 -94
- pyxlpr/ppocr/utils/__init__.py +0 -24
- pyxlpr/ppocr/utils/dict/ar_dict.txt +0 -117
- pyxlpr/ppocr/utils/dict/arabic_dict.txt +0 -162
- pyxlpr/ppocr/utils/dict/be_dict.txt +0 -145
- pyxlpr/ppocr/utils/dict/bg_dict.txt +0 -140
- pyxlpr/ppocr/utils/dict/chinese_cht_dict.txt +0 -8421
- pyxlpr/ppocr/utils/dict/cyrillic_dict.txt +0 -163
- pyxlpr/ppocr/utils/dict/devanagari_dict.txt +0 -167
- pyxlpr/ppocr/utils/dict/en_dict.txt +0 -63
- pyxlpr/ppocr/utils/dict/fa_dict.txt +0 -136
- pyxlpr/ppocr/utils/dict/french_dict.txt +0 -136
- pyxlpr/ppocr/utils/dict/german_dict.txt +0 -143
- pyxlpr/ppocr/utils/dict/hi_dict.txt +0 -162
- pyxlpr/ppocr/utils/dict/it_dict.txt +0 -118
- pyxlpr/ppocr/utils/dict/japan_dict.txt +0 -4399
- pyxlpr/ppocr/utils/dict/ka_dict.txt +0 -153
- pyxlpr/ppocr/utils/dict/korean_dict.txt +0 -3688
- pyxlpr/ppocr/utils/dict/latin_dict.txt +0 -185
- pyxlpr/ppocr/utils/dict/mr_dict.txt +0 -153
- pyxlpr/ppocr/utils/dict/ne_dict.txt +0 -153
- pyxlpr/ppocr/utils/dict/oc_dict.txt +0 -96
- pyxlpr/ppocr/utils/dict/pu_dict.txt +0 -130
- pyxlpr/ppocr/utils/dict/rs_dict.txt +0 -91
- pyxlpr/ppocr/utils/dict/rsc_dict.txt +0 -134
- pyxlpr/ppocr/utils/dict/ru_dict.txt +0 -125
- pyxlpr/ppocr/utils/dict/ta_dict.txt +0 -128
- pyxlpr/ppocr/utils/dict/table_dict.txt +0 -277
- pyxlpr/ppocr/utils/dict/table_structure_dict.txt +0 -2759
- pyxlpr/ppocr/utils/dict/te_dict.txt +0 -151
- pyxlpr/ppocr/utils/dict/ug_dict.txt +0 -114
- pyxlpr/ppocr/utils/dict/uk_dict.txt +0 -142
- pyxlpr/ppocr/utils/dict/ur_dict.txt +0 -137
- pyxlpr/ppocr/utils/dict/xi_dict.txt +0 -110
- pyxlpr/ppocr/utils/dict90.txt +0 -90
- pyxlpr/ppocr/utils/e2e_metric/Deteval.py +0 -574
- pyxlpr/ppocr/utils/e2e_metric/polygon_fast.py +0 -83
- pyxlpr/ppocr/utils/e2e_utils/extract_batchsize.py +0 -87
- pyxlpr/ppocr/utils/e2e_utils/extract_textpoint_fast.py +0 -457
- pyxlpr/ppocr/utils/e2e_utils/extract_textpoint_slow.py +0 -592
- pyxlpr/ppocr/utils/e2e_utils/pgnet_pp_utils.py +0 -162
- pyxlpr/ppocr/utils/e2e_utils/visual.py +0 -162
- pyxlpr/ppocr/utils/en_dict.txt +0 -95
- pyxlpr/ppocr/utils/gen_label.py +0 -81
- pyxlpr/ppocr/utils/ic15_dict.txt +0 -36
- pyxlpr/ppocr/utils/iou.py +0 -54
- pyxlpr/ppocr/utils/logging.py +0 -69
- pyxlpr/ppocr/utils/network.py +0 -84
- pyxlpr/ppocr/utils/ppocr_keys_v1.txt +0 -6623
- pyxlpr/ppocr/utils/profiler.py +0 -110
- pyxlpr/ppocr/utils/save_load.py +0 -150
- pyxlpr/ppocr/utils/stats.py +0 -72
- pyxlpr/ppocr/utils/utility.py +0 -80
- pyxlpr/ppstructure/__init__.py +0 -13
- pyxlpr/ppstructure/predict_system.py +0 -187
- pyxlpr/ppstructure/table/__init__.py +0 -13
- pyxlpr/ppstructure/table/eval_table.py +0 -72
- pyxlpr/ppstructure/table/matcher.py +0 -192
- pyxlpr/ppstructure/table/predict_structure.py +0 -136
- pyxlpr/ppstructure/table/predict_table.py +0 -221
- pyxlpr/ppstructure/table/table_metric/__init__.py +0 -16
- pyxlpr/ppstructure/table/table_metric/parallel.py +0 -51
- pyxlpr/ppstructure/table/table_metric/table_metric.py +0 -247
- pyxlpr/ppstructure/table/tablepyxl/__init__.py +0 -13
- pyxlpr/ppstructure/table/tablepyxl/style.py +0 -283
- pyxlpr/ppstructure/table/tablepyxl/tablepyxl.py +0 -118
- pyxlpr/ppstructure/utility.py +0 -71
- pyxlpr/xlai.py +0 -10
- /pyxllib/{ext/autogui → autogui}/virtualkey.py +0 -0
- {pyxllib-0.3.96.dist-info → pyxllib-0.3.197.dist-info/licenses}/LICENSE +0 -0
@@ -1,654 +0,0 @@
|
|
1
|
-
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
import numpy as np
|
15
|
-
import string
|
16
|
-
import paddle
|
17
|
-
from paddle.nn import functional as F
|
18
|
-
import re
|
19
|
-
|
20
|
-
|
21
|
-
class BaseRecLabelDecode(object):
|
22
|
-
""" Convert between text-label and text-index """
|
23
|
-
|
24
|
-
def __init__(self, character_dict_path=None, use_space_char=False):
|
25
|
-
self.beg_str = "sos"
|
26
|
-
self.end_str = "eos"
|
27
|
-
|
28
|
-
self.character_str = []
|
29
|
-
if character_dict_path is None:
|
30
|
-
self.character_str = "0123456789abcdefghijklmnopqrstuvwxyz"
|
31
|
-
dict_character = list(self.character_str)
|
32
|
-
else:
|
33
|
-
with open(character_dict_path, "rb") as fin:
|
34
|
-
lines = fin.readlines()
|
35
|
-
for line in lines:
|
36
|
-
line = line.decode('utf-8').strip("\n").strip("\r\n")
|
37
|
-
self.character_str.append(line)
|
38
|
-
if use_space_char:
|
39
|
-
self.character_str.append(" ")
|
40
|
-
dict_character = list(self.character_str)
|
41
|
-
|
42
|
-
dict_character = self.add_special_char(dict_character)
|
43
|
-
self.dict = {}
|
44
|
-
for i, char in enumerate(dict_character):
|
45
|
-
self.dict[char] = i
|
46
|
-
self.character = dict_character
|
47
|
-
|
48
|
-
def add_special_char(self, dict_character):
|
49
|
-
return dict_character
|
50
|
-
|
51
|
-
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
52
|
-
""" convert text-index into text-label. """
|
53
|
-
result_list = []
|
54
|
-
ignored_tokens = self.get_ignored_tokens()
|
55
|
-
batch_size = len(text_index)
|
56
|
-
for batch_idx in range(batch_size):
|
57
|
-
char_list = []
|
58
|
-
conf_list = []
|
59
|
-
for idx in range(len(text_index[batch_idx])):
|
60
|
-
if text_index[batch_idx][idx] in ignored_tokens:
|
61
|
-
continue
|
62
|
-
if is_remove_duplicate:
|
63
|
-
# only for predict
|
64
|
-
if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
|
65
|
-
batch_idx][idx]:
|
66
|
-
continue
|
67
|
-
char_list.append(self.character[int(text_index[batch_idx][
|
68
|
-
idx])])
|
69
|
-
if text_prob is not None:
|
70
|
-
conf_list.append(text_prob[batch_idx][idx])
|
71
|
-
else:
|
72
|
-
conf_list.append(1)
|
73
|
-
text = ''.join(char_list)
|
74
|
-
result_list.append((text, np.mean(conf_list)))
|
75
|
-
return result_list
|
76
|
-
|
77
|
-
def get_ignored_tokens(self):
|
78
|
-
return [0] # for ctc blank
|
79
|
-
|
80
|
-
|
81
|
-
class CTCLabelDecode(BaseRecLabelDecode):
|
82
|
-
""" Convert between text-label and text-index """
|
83
|
-
|
84
|
-
def __init__(self, character_dict_path=None, use_space_char=False,
|
85
|
-
**kwargs):
|
86
|
-
super(CTCLabelDecode, self).__init__(character_dict_path,
|
87
|
-
use_space_char)
|
88
|
-
|
89
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
90
|
-
if isinstance(preds, tuple):
|
91
|
-
preds = preds[-1]
|
92
|
-
if isinstance(preds, paddle.Tensor):
|
93
|
-
preds = preds.numpy()
|
94
|
-
preds_idx = preds.argmax(axis=2)
|
95
|
-
preds_prob = preds.max(axis=2)
|
96
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
|
97
|
-
if label is None:
|
98
|
-
return text
|
99
|
-
label = self.decode(label)
|
100
|
-
return text, label
|
101
|
-
|
102
|
-
def add_special_char(self, dict_character):
|
103
|
-
dict_character = ['blank'] + dict_character
|
104
|
-
return dict_character
|
105
|
-
|
106
|
-
|
107
|
-
class DistillationCTCLabelDecode(CTCLabelDecode):
|
108
|
-
"""
|
109
|
-
Convert
|
110
|
-
Convert between text-label and text-index
|
111
|
-
"""
|
112
|
-
|
113
|
-
def __init__(self,
|
114
|
-
character_dict_path=None,
|
115
|
-
use_space_char=False,
|
116
|
-
model_name=["student"],
|
117
|
-
key=None,
|
118
|
-
**kwargs):
|
119
|
-
super(DistillationCTCLabelDecode, self).__init__(character_dict_path,
|
120
|
-
use_space_char)
|
121
|
-
if not isinstance(model_name, list):
|
122
|
-
model_name = [model_name]
|
123
|
-
self.model_name = model_name
|
124
|
-
|
125
|
-
self.key = key
|
126
|
-
|
127
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
128
|
-
output = dict()
|
129
|
-
for name in self.model_name:
|
130
|
-
pred = preds[name]
|
131
|
-
if self.key is not None:
|
132
|
-
pred = pred[self.key]
|
133
|
-
output[name] = super().__call__(pred, label=label, *args, **kwargs)
|
134
|
-
return output
|
135
|
-
|
136
|
-
|
137
|
-
class NRTRLabelDecode(BaseRecLabelDecode):
|
138
|
-
""" Convert between text-label and text-index """
|
139
|
-
|
140
|
-
def __init__(self, character_dict_path=None, use_space_char=True, **kwargs):
|
141
|
-
super(NRTRLabelDecode, self).__init__(character_dict_path,
|
142
|
-
use_space_char)
|
143
|
-
|
144
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
145
|
-
|
146
|
-
if len(preds) == 2:
|
147
|
-
preds_id = preds[0]
|
148
|
-
preds_prob = preds[1]
|
149
|
-
if isinstance(preds_id, paddle.Tensor):
|
150
|
-
preds_id = preds_id.numpy()
|
151
|
-
if isinstance(preds_prob, paddle.Tensor):
|
152
|
-
preds_prob = preds_prob.numpy()
|
153
|
-
if preds_id[0][0] == 2:
|
154
|
-
preds_idx = preds_id[:, 1:]
|
155
|
-
preds_prob = preds_prob[:, 1:]
|
156
|
-
else:
|
157
|
-
preds_idx = preds_id
|
158
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=False)
|
159
|
-
if label is None:
|
160
|
-
return text
|
161
|
-
label = self.decode(label[:, 1:])
|
162
|
-
else:
|
163
|
-
if isinstance(preds, paddle.Tensor):
|
164
|
-
preds = preds.numpy()
|
165
|
-
preds_idx = preds.argmax(axis=2)
|
166
|
-
preds_prob = preds.max(axis=2)
|
167
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=False)
|
168
|
-
if label is None:
|
169
|
-
return text
|
170
|
-
label = self.decode(label[:, 1:])
|
171
|
-
return text, label
|
172
|
-
|
173
|
-
def add_special_char(self, dict_character):
|
174
|
-
dict_character = ['blank', '<unk>', '<s>', '</s>'] + dict_character
|
175
|
-
return dict_character
|
176
|
-
|
177
|
-
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
178
|
-
""" convert text-index into text-label. """
|
179
|
-
result_list = []
|
180
|
-
batch_size = len(text_index)
|
181
|
-
for batch_idx in range(batch_size):
|
182
|
-
char_list = []
|
183
|
-
conf_list = []
|
184
|
-
for idx in range(len(text_index[batch_idx])):
|
185
|
-
if text_index[batch_idx][idx] == 3: # end
|
186
|
-
break
|
187
|
-
try:
|
188
|
-
char_list.append(self.character[int(text_index[batch_idx][
|
189
|
-
idx])])
|
190
|
-
except:
|
191
|
-
continue
|
192
|
-
if text_prob is not None:
|
193
|
-
conf_list.append(text_prob[batch_idx][idx])
|
194
|
-
else:
|
195
|
-
conf_list.append(1)
|
196
|
-
text = ''.join(char_list)
|
197
|
-
result_list.append((text.lower(), np.mean(conf_list)))
|
198
|
-
return result_list
|
199
|
-
|
200
|
-
|
201
|
-
class AttnLabelDecode(BaseRecLabelDecode):
|
202
|
-
""" Convert between text-label and text-index """
|
203
|
-
|
204
|
-
def __init__(self, character_dict_path=None, use_space_char=False,
|
205
|
-
**kwargs):
|
206
|
-
super(AttnLabelDecode, self).__init__(character_dict_path,
|
207
|
-
use_space_char)
|
208
|
-
|
209
|
-
def add_special_char(self, dict_character):
|
210
|
-
self.beg_str = "sos"
|
211
|
-
self.end_str = "eos"
|
212
|
-
dict_character = dict_character
|
213
|
-
dict_character = [self.beg_str] + dict_character + [self.end_str]
|
214
|
-
return dict_character
|
215
|
-
|
216
|
-
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
217
|
-
""" convert text-index into text-label. """
|
218
|
-
result_list = []
|
219
|
-
ignored_tokens = self.get_ignored_tokens()
|
220
|
-
[beg_idx, end_idx] = self.get_ignored_tokens()
|
221
|
-
batch_size = len(text_index)
|
222
|
-
for batch_idx in range(batch_size):
|
223
|
-
char_list = []
|
224
|
-
conf_list = []
|
225
|
-
for idx in range(len(text_index[batch_idx])):
|
226
|
-
if text_index[batch_idx][idx] in ignored_tokens:
|
227
|
-
continue
|
228
|
-
if int(text_index[batch_idx][idx]) == int(end_idx):
|
229
|
-
break
|
230
|
-
if is_remove_duplicate:
|
231
|
-
# only for predict
|
232
|
-
if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
|
233
|
-
batch_idx][idx]:
|
234
|
-
continue
|
235
|
-
char_list.append(self.character[int(text_index[batch_idx][
|
236
|
-
idx])])
|
237
|
-
if text_prob is not None:
|
238
|
-
conf_list.append(text_prob[batch_idx][idx])
|
239
|
-
else:
|
240
|
-
conf_list.append(1)
|
241
|
-
text = ''.join(char_list)
|
242
|
-
result_list.append((text, np.mean(conf_list)))
|
243
|
-
return result_list
|
244
|
-
|
245
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
246
|
-
"""
|
247
|
-
text = self.decode(text)
|
248
|
-
if label is None:
|
249
|
-
return text
|
250
|
-
else:
|
251
|
-
label = self.decode(label, is_remove_duplicate=False)
|
252
|
-
return text, label
|
253
|
-
"""
|
254
|
-
if isinstance(preds, paddle.Tensor):
|
255
|
-
preds = preds.numpy()
|
256
|
-
|
257
|
-
preds_idx = preds.argmax(axis=2)
|
258
|
-
preds_prob = preds.max(axis=2)
|
259
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=False)
|
260
|
-
if label is None:
|
261
|
-
return text
|
262
|
-
label = self.decode(label, is_remove_duplicate=False)
|
263
|
-
return text, label
|
264
|
-
|
265
|
-
def get_ignored_tokens(self):
|
266
|
-
beg_idx = self.get_beg_end_flag_idx("beg")
|
267
|
-
end_idx = self.get_beg_end_flag_idx("end")
|
268
|
-
return [beg_idx, end_idx]
|
269
|
-
|
270
|
-
def get_beg_end_flag_idx(self, beg_or_end):
|
271
|
-
if beg_or_end == "beg":
|
272
|
-
idx = np.array(self.dict[self.beg_str])
|
273
|
-
elif beg_or_end == "end":
|
274
|
-
idx = np.array(self.dict[self.end_str])
|
275
|
-
else:
|
276
|
-
assert False, "unsupport type %s in get_beg_end_flag_idx" \
|
277
|
-
% beg_or_end
|
278
|
-
return idx
|
279
|
-
|
280
|
-
|
281
|
-
class SEEDLabelDecode(BaseRecLabelDecode):
|
282
|
-
""" Convert between text-label and text-index """
|
283
|
-
|
284
|
-
def __init__(self, character_dict_path=None, use_space_char=False,
|
285
|
-
**kwargs):
|
286
|
-
super(SEEDLabelDecode, self).__init__(character_dict_path,
|
287
|
-
use_space_char)
|
288
|
-
|
289
|
-
def add_special_char(self, dict_character):
|
290
|
-
self.padding_str = "padding"
|
291
|
-
self.end_str = "eos"
|
292
|
-
self.unknown = "unknown"
|
293
|
-
dict_character = dict_character + [
|
294
|
-
self.end_str, self.padding_str, self.unknown
|
295
|
-
]
|
296
|
-
return dict_character
|
297
|
-
|
298
|
-
def get_ignored_tokens(self):
|
299
|
-
end_idx = self.get_beg_end_flag_idx("eos")
|
300
|
-
return [end_idx]
|
301
|
-
|
302
|
-
def get_beg_end_flag_idx(self, beg_or_end):
|
303
|
-
if beg_or_end == "sos":
|
304
|
-
idx = np.array(self.dict[self.beg_str])
|
305
|
-
elif beg_or_end == "eos":
|
306
|
-
idx = np.array(self.dict[self.end_str])
|
307
|
-
else:
|
308
|
-
assert False, "unsupport type %s in get_beg_end_flag_idx" % beg_or_end
|
309
|
-
return idx
|
310
|
-
|
311
|
-
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
312
|
-
""" convert text-index into text-label. """
|
313
|
-
result_list = []
|
314
|
-
[end_idx] = self.get_ignored_tokens()
|
315
|
-
batch_size = len(text_index)
|
316
|
-
for batch_idx in range(batch_size):
|
317
|
-
char_list = []
|
318
|
-
conf_list = []
|
319
|
-
for idx in range(len(text_index[batch_idx])):
|
320
|
-
if int(text_index[batch_idx][idx]) == int(end_idx):
|
321
|
-
break
|
322
|
-
if is_remove_duplicate:
|
323
|
-
# only for predict
|
324
|
-
if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
|
325
|
-
batch_idx][idx]:
|
326
|
-
continue
|
327
|
-
char_list.append(self.character[int(text_index[batch_idx][
|
328
|
-
idx])])
|
329
|
-
if text_prob is not None:
|
330
|
-
conf_list.append(text_prob[batch_idx][idx])
|
331
|
-
else:
|
332
|
-
conf_list.append(1)
|
333
|
-
text = ''.join(char_list)
|
334
|
-
result_list.append((text, np.mean(conf_list)))
|
335
|
-
return result_list
|
336
|
-
|
337
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
338
|
-
"""
|
339
|
-
text = self.decode(text)
|
340
|
-
if label is None:
|
341
|
-
return text
|
342
|
-
else:
|
343
|
-
label = self.decode(label, is_remove_duplicate=False)
|
344
|
-
return text, label
|
345
|
-
"""
|
346
|
-
preds_idx = preds["rec_pred"]
|
347
|
-
if isinstance(preds_idx, paddle.Tensor):
|
348
|
-
preds_idx = preds_idx.numpy()
|
349
|
-
if "rec_pred_scores" in preds:
|
350
|
-
preds_idx = preds["rec_pred"]
|
351
|
-
preds_prob = preds["rec_pred_scores"]
|
352
|
-
else:
|
353
|
-
preds_idx = preds["rec_pred"].argmax(axis=2)
|
354
|
-
preds_prob = preds["rec_pred"].max(axis=2)
|
355
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=False)
|
356
|
-
if label is None:
|
357
|
-
return text
|
358
|
-
label = self.decode(label, is_remove_duplicate=False)
|
359
|
-
return text, label
|
360
|
-
|
361
|
-
|
362
|
-
class SRNLabelDecode(BaseRecLabelDecode):
|
363
|
-
""" Convert between text-label and text-index """
|
364
|
-
|
365
|
-
def __init__(self, character_dict_path=None, use_space_char=False,
|
366
|
-
**kwargs):
|
367
|
-
super(SRNLabelDecode, self).__init__(character_dict_path,
|
368
|
-
use_space_char)
|
369
|
-
self.max_text_length = kwargs.get('max_text_length', 25)
|
370
|
-
|
371
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
372
|
-
pred = preds['predict']
|
373
|
-
char_num = len(self.character_str) + 2
|
374
|
-
if isinstance(pred, paddle.Tensor):
|
375
|
-
pred = pred.numpy()
|
376
|
-
pred = np.reshape(pred, [-1, char_num])
|
377
|
-
|
378
|
-
preds_idx = np.argmax(pred, axis=1)
|
379
|
-
preds_prob = np.max(pred, axis=1)
|
380
|
-
|
381
|
-
preds_idx = np.reshape(preds_idx, [-1, self.max_text_length])
|
382
|
-
|
383
|
-
preds_prob = np.reshape(preds_prob, [-1, self.max_text_length])
|
384
|
-
|
385
|
-
text = self.decode(preds_idx, preds_prob)
|
386
|
-
|
387
|
-
if label is None:
|
388
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=False)
|
389
|
-
return text
|
390
|
-
label = self.decode(label)
|
391
|
-
return text, label
|
392
|
-
|
393
|
-
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
394
|
-
""" convert text-index into text-label. """
|
395
|
-
result_list = []
|
396
|
-
ignored_tokens = self.get_ignored_tokens()
|
397
|
-
batch_size = len(text_index)
|
398
|
-
|
399
|
-
for batch_idx in range(batch_size):
|
400
|
-
char_list = []
|
401
|
-
conf_list = []
|
402
|
-
for idx in range(len(text_index[batch_idx])):
|
403
|
-
if text_index[batch_idx][idx] in ignored_tokens:
|
404
|
-
continue
|
405
|
-
if is_remove_duplicate:
|
406
|
-
# only for predict
|
407
|
-
if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
|
408
|
-
batch_idx][idx]:
|
409
|
-
continue
|
410
|
-
char_list.append(self.character[int(text_index[batch_idx][
|
411
|
-
idx])])
|
412
|
-
if text_prob is not None:
|
413
|
-
conf_list.append(text_prob[batch_idx][idx])
|
414
|
-
else:
|
415
|
-
conf_list.append(1)
|
416
|
-
|
417
|
-
text = ''.join(char_list)
|
418
|
-
result_list.append((text, np.mean(conf_list)))
|
419
|
-
return result_list
|
420
|
-
|
421
|
-
def add_special_char(self, dict_character):
|
422
|
-
dict_character = dict_character + [self.beg_str, self.end_str]
|
423
|
-
return dict_character
|
424
|
-
|
425
|
-
def get_ignored_tokens(self):
|
426
|
-
beg_idx = self.get_beg_end_flag_idx("beg")
|
427
|
-
end_idx = self.get_beg_end_flag_idx("end")
|
428
|
-
return [beg_idx, end_idx]
|
429
|
-
|
430
|
-
def get_beg_end_flag_idx(self, beg_or_end):
|
431
|
-
if beg_or_end == "beg":
|
432
|
-
idx = np.array(self.dict[self.beg_str])
|
433
|
-
elif beg_or_end == "end":
|
434
|
-
idx = np.array(self.dict[self.end_str])
|
435
|
-
else:
|
436
|
-
assert False, "unsupport type %s in get_beg_end_flag_idx" \
|
437
|
-
% beg_or_end
|
438
|
-
return idx
|
439
|
-
|
440
|
-
|
441
|
-
class TableLabelDecode(object):
|
442
|
-
""" """
|
443
|
-
|
444
|
-
def __init__(self, character_dict_path, **kwargs):
|
445
|
-
list_character, list_elem = self.load_char_elem_dict(
|
446
|
-
character_dict_path)
|
447
|
-
list_character = self.add_special_char(list_character)
|
448
|
-
list_elem = self.add_special_char(list_elem)
|
449
|
-
self.dict_character = {}
|
450
|
-
self.dict_idx_character = {}
|
451
|
-
for i, char in enumerate(list_character):
|
452
|
-
self.dict_idx_character[i] = char
|
453
|
-
self.dict_character[char] = i
|
454
|
-
self.dict_elem = {}
|
455
|
-
self.dict_idx_elem = {}
|
456
|
-
for i, elem in enumerate(list_elem):
|
457
|
-
self.dict_idx_elem[i] = elem
|
458
|
-
self.dict_elem[elem] = i
|
459
|
-
|
460
|
-
def load_char_elem_dict(self, character_dict_path):
|
461
|
-
list_character = []
|
462
|
-
list_elem = []
|
463
|
-
with open(character_dict_path, "rb") as fin:
|
464
|
-
lines = fin.readlines()
|
465
|
-
substr = lines[0].decode('utf-8').strip("\n").strip("\r\n").split(
|
466
|
-
"\t")
|
467
|
-
character_num = int(substr[0])
|
468
|
-
elem_num = int(substr[1])
|
469
|
-
for cno in range(1, 1 + character_num):
|
470
|
-
character = lines[cno].decode('utf-8').strip("\n").strip("\r\n")
|
471
|
-
list_character.append(character)
|
472
|
-
for eno in range(1 + character_num, 1 + character_num + elem_num):
|
473
|
-
elem = lines[eno].decode('utf-8').strip("\n").strip("\r\n")
|
474
|
-
list_elem.append(elem)
|
475
|
-
return list_character, list_elem
|
476
|
-
|
477
|
-
def add_special_char(self, list_character):
|
478
|
-
self.beg_str = "sos"
|
479
|
-
self.end_str = "eos"
|
480
|
-
list_character = [self.beg_str] + list_character + [self.end_str]
|
481
|
-
return list_character
|
482
|
-
|
483
|
-
def __call__(self, preds):
|
484
|
-
structure_probs = preds['structure_probs']
|
485
|
-
loc_preds = preds['loc_preds']
|
486
|
-
if isinstance(structure_probs, paddle.Tensor):
|
487
|
-
structure_probs = structure_probs.numpy()
|
488
|
-
if isinstance(loc_preds, paddle.Tensor):
|
489
|
-
loc_preds = loc_preds.numpy()
|
490
|
-
structure_idx = structure_probs.argmax(axis=2)
|
491
|
-
structure_probs = structure_probs.max(axis=2)
|
492
|
-
structure_str, structure_pos, result_score_list, result_elem_idx_list = self.decode(
|
493
|
-
structure_idx, structure_probs, 'elem')
|
494
|
-
res_html_code_list = []
|
495
|
-
res_loc_list = []
|
496
|
-
batch_num = len(structure_str)
|
497
|
-
for bno in range(batch_num):
|
498
|
-
res_loc = []
|
499
|
-
for sno in range(len(structure_str[bno])):
|
500
|
-
text = structure_str[bno][sno]
|
501
|
-
if text in ['<td>', '<td']:
|
502
|
-
pos = structure_pos[bno][sno]
|
503
|
-
res_loc.append(loc_preds[bno, pos])
|
504
|
-
res_html_code = ''.join(structure_str[bno])
|
505
|
-
res_loc = np.array(res_loc)
|
506
|
-
res_html_code_list.append(res_html_code)
|
507
|
-
res_loc_list.append(res_loc)
|
508
|
-
return {
|
509
|
-
'res_html_code': res_html_code_list,
|
510
|
-
'res_loc': res_loc_list,
|
511
|
-
'res_score_list': result_score_list,
|
512
|
-
'res_elem_idx_list': result_elem_idx_list,
|
513
|
-
'structure_str_list': structure_str
|
514
|
-
}
|
515
|
-
|
516
|
-
def decode(self, text_index, structure_probs, char_or_elem):
|
517
|
-
"""convert text-label into text-index.
|
518
|
-
"""
|
519
|
-
if char_or_elem == "char":
|
520
|
-
current_dict = self.dict_idx_character
|
521
|
-
else:
|
522
|
-
current_dict = self.dict_idx_elem
|
523
|
-
ignored_tokens = self.get_ignored_tokens('elem')
|
524
|
-
beg_idx, end_idx = ignored_tokens
|
525
|
-
|
526
|
-
result_list = []
|
527
|
-
result_pos_list = []
|
528
|
-
result_score_list = []
|
529
|
-
result_elem_idx_list = []
|
530
|
-
batch_size = len(text_index)
|
531
|
-
for batch_idx in range(batch_size):
|
532
|
-
char_list = []
|
533
|
-
elem_pos_list = []
|
534
|
-
elem_idx_list = []
|
535
|
-
score_list = []
|
536
|
-
for idx in range(len(text_index[batch_idx])):
|
537
|
-
tmp_elem_idx = int(text_index[batch_idx][idx])
|
538
|
-
if idx > 0 and tmp_elem_idx == end_idx:
|
539
|
-
break
|
540
|
-
if tmp_elem_idx in ignored_tokens:
|
541
|
-
continue
|
542
|
-
|
543
|
-
char_list.append(current_dict[tmp_elem_idx])
|
544
|
-
elem_pos_list.append(idx)
|
545
|
-
score_list.append(structure_probs[batch_idx, idx])
|
546
|
-
elem_idx_list.append(tmp_elem_idx)
|
547
|
-
result_list.append(char_list)
|
548
|
-
result_pos_list.append(elem_pos_list)
|
549
|
-
result_score_list.append(score_list)
|
550
|
-
result_elem_idx_list.append(elem_idx_list)
|
551
|
-
return result_list, result_pos_list, result_score_list, result_elem_idx_list
|
552
|
-
|
553
|
-
def get_ignored_tokens(self, char_or_elem):
|
554
|
-
beg_idx = self.get_beg_end_flag_idx("beg", char_or_elem)
|
555
|
-
end_idx = self.get_beg_end_flag_idx("end", char_or_elem)
|
556
|
-
return [beg_idx, end_idx]
|
557
|
-
|
558
|
-
def get_beg_end_flag_idx(self, beg_or_end, char_or_elem):
|
559
|
-
if char_or_elem == "char":
|
560
|
-
if beg_or_end == "beg":
|
561
|
-
idx = self.dict_character[self.beg_str]
|
562
|
-
elif beg_or_end == "end":
|
563
|
-
idx = self.dict_character[self.end_str]
|
564
|
-
else:
|
565
|
-
assert False, "Unsupport type %s in get_beg_end_flag_idx of char" \
|
566
|
-
% beg_or_end
|
567
|
-
elif char_or_elem == "elem":
|
568
|
-
if beg_or_end == "beg":
|
569
|
-
idx = self.dict_elem[self.beg_str]
|
570
|
-
elif beg_or_end == "end":
|
571
|
-
idx = self.dict_elem[self.end_str]
|
572
|
-
else:
|
573
|
-
assert False, "Unsupport type %s in get_beg_end_flag_idx of elem" \
|
574
|
-
% beg_or_end
|
575
|
-
else:
|
576
|
-
assert False, "Unsupport type %s in char_or_elem" \
|
577
|
-
% char_or_elem
|
578
|
-
return idx
|
579
|
-
|
580
|
-
|
581
|
-
class SARLabelDecode(BaseRecLabelDecode):
|
582
|
-
""" Convert between text-label and text-index """
|
583
|
-
|
584
|
-
def __init__(self, character_dict_path=None, use_space_char=False,
|
585
|
-
**kwargs):
|
586
|
-
super(SARLabelDecode, self).__init__(character_dict_path,
|
587
|
-
use_space_char)
|
588
|
-
|
589
|
-
self.rm_symbol = kwargs.get('rm_symbol', False)
|
590
|
-
|
591
|
-
def add_special_char(self, dict_character):
|
592
|
-
beg_end_str = "<BOS/EOS>"
|
593
|
-
unknown_str = "<UKN>"
|
594
|
-
padding_str = "<PAD>"
|
595
|
-
dict_character = dict_character + [unknown_str]
|
596
|
-
self.unknown_idx = len(dict_character) - 1
|
597
|
-
dict_character = dict_character + [beg_end_str]
|
598
|
-
self.start_idx = len(dict_character) - 1
|
599
|
-
self.end_idx = len(dict_character) - 1
|
600
|
-
dict_character = dict_character + [padding_str]
|
601
|
-
self.padding_idx = len(dict_character) - 1
|
602
|
-
return dict_character
|
603
|
-
|
604
|
-
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
605
|
-
""" convert text-index into text-label. """
|
606
|
-
result_list = []
|
607
|
-
ignored_tokens = self.get_ignored_tokens()
|
608
|
-
|
609
|
-
batch_size = len(text_index)
|
610
|
-
for batch_idx in range(batch_size):
|
611
|
-
char_list = []
|
612
|
-
conf_list = []
|
613
|
-
for idx in range(len(text_index[batch_idx])):
|
614
|
-
if text_index[batch_idx][idx] in ignored_tokens:
|
615
|
-
continue
|
616
|
-
if int(text_index[batch_idx][idx]) == int(self.end_idx):
|
617
|
-
if text_prob is None and idx == 0:
|
618
|
-
continue
|
619
|
-
else:
|
620
|
-
break
|
621
|
-
if is_remove_duplicate:
|
622
|
-
# only for predict
|
623
|
-
if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
|
624
|
-
batch_idx][idx]:
|
625
|
-
continue
|
626
|
-
char_list.append(self.character[int(text_index[batch_idx][
|
627
|
-
idx])])
|
628
|
-
if text_prob is not None:
|
629
|
-
conf_list.append(text_prob[batch_idx][idx])
|
630
|
-
else:
|
631
|
-
conf_list.append(1)
|
632
|
-
text = ''.join(char_list)
|
633
|
-
if self.rm_symbol:
|
634
|
-
comp = re.compile('[^A-Z^a-z^0-9^\u4e00-\u9fa5]')
|
635
|
-
text = text.lower()
|
636
|
-
text = comp.sub('', text)
|
637
|
-
result_list.append((text, np.mean(conf_list)))
|
638
|
-
return result_list
|
639
|
-
|
640
|
-
def __call__(self, preds, label=None, *args, **kwargs):
|
641
|
-
if isinstance(preds, paddle.Tensor):
|
642
|
-
preds = preds.numpy()
|
643
|
-
preds_idx = preds.argmax(axis=2)
|
644
|
-
preds_prob = preds.max(axis=2)
|
645
|
-
|
646
|
-
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=False)
|
647
|
-
|
648
|
-
if label is None:
|
649
|
-
return text
|
650
|
-
label = self.decode(label, is_remove_duplicate=False)
|
651
|
-
return text, label
|
652
|
-
|
653
|
-
def get_ignored_tokens(self):
|
654
|
-
return [self.padding_idx]
|