exinfo.py
CHANGED
|
@@ -1,5 +1,72 @@
|
|
| 1 |
|
| 2 |
# 改成原地加换行空格标点符号
|
| 3 |
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 1 |
|
| 2 |
# 改成原地加换行空格标点符号
|
| 3 |
|
| 4 |
+
if __name__ == '__main__':
|
| 5 |
|
| 6 |
+
import glob
|
| 7 |
+
import os
|
| 8 |
+
import json
|
| 9 |
+
import numpy as np
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
chs = glob.glob('data/ch*', recursive=False)
|
| 12 |
+
m5s = {}
|
| 13 |
+
for i, pth in enumerate(chs):
|
| 14 |
+
pth = pth.replace('\\', '/')
|
| 15 |
+
parts = pth.split('/')
|
| 16 |
+
chapter = parts[1]
|
| 17 |
+
|
| 18 |
+
out2 = f'data/{chapter}/out2'
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
with open(f"{out2}/page_texts.json", "r", encoding='UTF-8') as fp:
|
| 22 |
+
page_texts = json.load(fp)
|
| 23 |
+
|
| 24 |
+
with open(f"{out2}/pageIds_pageNames.json", "r", encoding='UTF-8') as fp:
|
| 25 |
+
pageIds_pageNames = json.load(fp)
|
| 26 |
+
|
| 27 |
+
boxs = glob.glob(f"{out2}/*.boxs", recursive=False)
|
| 28 |
+
|
| 29 |
+
last_x = -1
|
| 30 |
+
new_line = False
|
| 31 |
+
lines = []
|
| 32 |
+
line = []
|
| 33 |
+
for j, pt in enumerate(boxs):
|
| 34 |
+
pt_lines = pt.replace('.boxs', '.lines')
|
| 35 |
+
with open(pt, "r", encoding='UTF-8') as fp:
|
| 36 |
+
box = json.load(fp)
|
| 37 |
+
|
| 38 |
+
for bx in box:
|
| 39 |
+
charPoly = bx["charPoly"]
|
| 40 |
+
lu = [charPoly["x0"], charPoly["y0"]]
|
| 41 |
+
ru = [charPoly["x1"], charPoly["y1"]]
|
| 42 |
+
rd = [charPoly["x2"], charPoly["y2"]]
|
| 43 |
+
ld = [charPoly["x3"], charPoly["y3"]]
|
| 44 |
+
width = max(ru[0], rd[0]) - min(lu[0], ld[0])
|
| 45 |
+
height = max( ld[1], rd[1] ) - min(lu[1], ru[1])
|
| 46 |
+
points = np.array([lu, ru, rd, ld])
|
| 47 |
+
|
| 48 |
+
if last_x == -1:
|
| 49 |
+
last_x = lu[0]
|
| 50 |
+
else:
|
| 51 |
+
x_diff = abs(lu[0] - last_x)
|
| 52 |
+
if x_diff > int(width / 2):
|
| 53 |
+
new_line = True
|
| 54 |
+
|
| 55 |
+
if new_line:
|
| 56 |
+
lines.append(line)
|
| 57 |
+
line = []
|
| 58 |
+
new_line = False
|
| 59 |
+
|
| 60 |
+
line.append(bx)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
last_x = lu[0]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
with open(pt_lines, 'w', encoding='utf-8') as fp:
|
| 67 |
+
json.dump(lines, fp, indent=4, ensure_ascii=False)
|
| 68 |
+
|
| 69 |
+
print(f"box {j+1} / {len(boxs)}")
|
| 70 |
+
|
| 71 |
+
print(f"{chapter} {i+1} / {len(pths)}")
|
| 72 |
|