|
|
| |
|
|
| |
|
|
| |
|
|
| if __name__ == '__main__': |
|
|
| import glob |
| import os |
| import json |
| import numpy as np |
| from pathlib import Path |
| import opencc |
| import copy |
| from collections import defaultdict, OrderedDict |
| s2t = opencc.OpenCC('s2t.json') |
| t2s = opencc.OpenCC('t2s.json') |
|
|
| chs = glob.glob('data/ch*', recursive=False) |
|
|
| menus = [] |
| pages_all = [] |
| 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}/*.boxsnew", recursive=False) |
|
|
| |
| menu = '' |
| menu_title = '' |
|
|
| pages = [] |
|
|
| for j, pt in enumerate(boxs): |
|
|
| pageName = Path(pt).stem |
| pageId = pageNames_pageIds[pageName] |
|
|
| with open(pt, "r", encoding='UTF-8') as fp: |
| box = json.load(fp) |
|
|
| page_text = '' |
|
|
| for k, bx in enumerate(box): |
| charPoly = bx["charPoly"] |
| charText = bx["charText"] |
| sign = bx["sign"] |
| space = bx["space"] |
| is_menu = bx["is_menu"] |
| is_menutitle = bx["is_menutitle"] |
|
|
| if is_menu: |
| menu = menu + charText + sign |
| if is_menutitle: |
| menu_title = menu_title + charText + sign |
| |
| if menu_title != '' and not is_menutitle: |
| |
| if len(pages) > 0: |
| menus[-1]["pages"] = copy.deepcopy(pages) |
| pages = [] |
|
|
| menus.append({ "menu": menu, "menu_title": menu_title }) |
| menu = '' |
| menu_title = '' |
|
|
| |
| |
| page_text = page_text + charText + sign |
| pass |
|
|
| pages.append({"pageName":pageName, "pageId":pageId, "chapter":chapter, "page_text": page_text, "box": box }) |
| pages_all.append({"pageName":pageName, "pageId":pageId, "chapter":chapter, "page_text": page_text, "box": box }) |
|
|
| pass |
|
|
| |
| pth_pages_all = 'data/pages_all.json' |
| if not os.path.exists(pth_pages_all): |
| with open(pth_pages_all, 'w', encoding='utf-8') as fp: |
| json.dump(pages_all, fp, indent=4, ensure_ascii=False) |
|
|
| |
| if os.path.exists(pth_pages_all): |
| with open(pth_pages_all, "r", encoding='UTF-8') as fp: |
| pages_all = json.load(fp) |
|
|
|
|
| dir_autochapters = 'auto_chapters' |
| if not os.path.exists(dir_autochapters): |
| os.makedirs(dir_autochapters) |
|
|
| |
| grouped = defaultdict(list) |
| for item in pages_all: |
| grouped[item.get("chapter")].append(item) |
|
|
| for ch, pages in grouped.items(): |
| pt_ch = f'{dir_autochapters}/{ch}.typ' |
| menu = '' |
| menu_title = '' |
| page_text = '' |
| ch_text = """#import "../lib.typ": *\n#heading(level: 1, outlined: true)""" |
| menu_linkChar = '' |
| menu_title_linkChar = '' |
| for i, page in enumerate(pages): |
| box = page["box"] |
| pageName = page['pageName'] |
| for k, bx in enumerate(box): |
| charPoly = bx["charPoly"] |
| charText = bx["charText"] |
| sign = bx["sign"] |
| space = ' ' if bx["space"] else '' |
| space_n = ' ' if bx["space"] else '' |
| is_menu = bx["is_menu"] |
| is_menutitle = bx["is_menutitle"] |
| x0, y0, x1, y1, x2, y2, x3, y3 = charPoly['x0'], charPoly['y0'], charPoly['x1'], charPoly['y1'], charPoly['x2'], charPoly['y2'], charPoly['x3'], charPoly['y3'] |
| linkChar = f"""#c("{charText}", "{pageName},{x0},{y0},{x1},{y1},{x2},{y2},{x3},{y3}")""" |
| if is_menu: |
| menu = menu + charText + space_n |
| menu_linkChar = menu_linkChar + linkChar + space |
| if is_menutitle: |
| menu_title = menu_title + charText + space_n |
| menu_title_linkChar = menu_title_linkChar + linkChar + space |
|
|
| if menu_title != '' and not is_menutitle: |
| ch_text = ch_text + f"""[{menu} {menu_title}]\n #v(0.1em){menu_linkChar}#v(18em)\n {menu_title_linkChar}#v(18em)\n""" |
| menu = '' |
| menu_title = '' |
| menu_linkChar = '' |
| menu_title_linkChar = '' |
|
|
| if not is_menu and not is_menutitle: |
| page_text = page_text + linkChar + space + sign |
| |
| page_text += '\n' |
|
|
| |
| ch_text += page_text |
|
|
| with open(pt_ch, 'w', encoding='utf-8') as f: |
| f.write(ch_text) |
|
|
| pass |
|
|
|
|