|
|
| |
|
|
| if __name__ == '__main__': |
|
|
| import glob |
| import os |
| import json |
| import numpy as np |
| from pathlib import Path |
| import opencc |
| s2t = opencc.OpenCC('s2t.json') |
| t2s = opencc.OpenCC('t2s.json') |
|
|
| chs = glob.glob('data/ch*', recursive=False) |
| m5s = {} |
| with open('data/variants.json', "r", encoding='UTF-8') as fp: |
| variants = json.load(fp) |
| for i, pth in enumerate(chs): |
| pth = pth.replace('\\', '/') |
| parts = pth.split('/') |
| chapter = parts[1] |
| |
| out2 = f'data/{chapter}/out2' |
|
|
|
|
| with open(f"{out2}/page_texts.json", "r", encoding='UTF-8') as fp: |
| page_texts = json.load(fp) |
|
|
| with open(f"{out2}/pageIds_pageNames.json", "r", encoding='UTF-8') as fp: |
| pageIds_pageNames = json.load(fp) |
| |
| pageNames_pageIds = {} |
| for k, v in pageIds_pageNames.items(): |
| pageNames_pageIds[v] = k |
|
|
| |
| boxs = glob.glob(f"{out2}/*.boxs", recursive=False) |
|
|
| last_x = -1 |
| last_y = -1 |
| last_height = -1 |
| new_line = False |
| new_space = False |
| lines = [] |
| line = [] |
| signs = ['。', ':', ',', '?', '、', '!'] |
| for j, pt in enumerate(boxs): |
| sign_count = 0 |
| stem = Path(pt).stem |
| pageId = pageNames_pageIds[stem] |
| page_text = page_texts[pageId] |
| page_text = page_text.replace(' ', '') |
| pt_boxsnew = pt.replace('.boxs', '.boxsnew') |
|
|
| if os.path.exists(pt_boxsnew): |
| continue |
|
|
| with open(pt, "r", encoding='UTF-8') as fp: |
| box = json.load(fp) |
| g = 0 |
| for k, bx in enumerate(box): |
| charPoly = bx["charPoly"] |
| charText = bx["charText"] |
| bx["sign"] = '' |
| bx["space"] = '' |
| bx["is_menu"] = False |
| bx["is_menutitle"] = False |
| sign = '' |
| if charText in variants: |
| charText = variants[charText] |
| charText = s2t.convert(charText) |
| g = k + sign_count |
| if g < len(page_text) and page_text[g] in signs: |
| sign = page_text[g] |
| |
| box[k-1]["sign"] = sign |
|
|
| sign_count += 1 |
| g = k + sign_count |
|
|
| if g < len(page_text) and page_text[g] in variants: |
| before = page_text[:g] |
| after = page_text[g+1:] |
| oldchar = page_text[g] |
| newchar = variants[oldchar] |
| page_text = before + newchar + after |
|
|
| if g < len(page_text): |
| before = page_text[:g] |
| after = page_text[g+1:] |
| oldchar = page_text[g] |
| newchar = s2t.convert(oldchar) |
| page_text = before + newchar + after |
| if page_text[g] == charText: |
| pass |
| else: |
| pass |
| lu = [charPoly["x0"], charPoly["y0"]] |
| ru = [charPoly["x1"], charPoly["y1"]] |
| rd = [charPoly["x2"], charPoly["y2"]] |
| ld = [charPoly["x3"], charPoly["y3"]] |
| width = max(ru[0], rd[0]) - min(lu[0], ld[0]) |
| height = max( ld[1], rd[1] ) - min(lu[1], ru[1]) |
| points = np.array([lu, ru, rd, ld]) |
|
|
| if last_x == -1: |
| last_x = lu[0] |
| else: |
| x_diff = abs(lu[0] - last_x) |
| if x_diff > int(width / 2): |
| new_line = True |
|
|
| if new_line: |
| |
| box[k-1]["sign"] = '\n' |
| lines.append(line) |
| line = [] |
| new_line = False |
|
|
| if last_y == -1: |
| last_y = lu[1] |
| last_height = height |
| else: |
| y_diff = abs(lu[1] - last_y) |
| if y_diff > last_height + last_height / 2: |
| new_space = True |
| |
| if new_space: |
| |
| new_space = False |
| |
|
|
|
|
| line.append(bx) |
| |
| |
| last_x = lu[0] |
| last_y = lu[1] |
| last_height = height |
|
|
| box_text = '' |
| for k, bx in enumerate(box): |
| charPoly = bx["charPoly"] |
| charText = bx["charText"] |
| sign = '' |
| if 'sign' in bx: |
| sign = bx["sign"] |
| box_text = box_text + charText + sign |
|
|
| with open(pt_boxsnew, 'w', encoding='utf-8') as fp: |
| json.dump(box, fp, indent=4, ensure_ascii=False) |
|
|
| print(f"box {j+1} / {len(boxs)}") |
|
|
| print(f"{chapter} {i+1} / {len(chs)}") |
|
|
|
|