Dominic28 commited on
Commit
f076fda
·
verified ·
1 Parent(s): 6a0e2d0

Upload ocrpages2ocrdocs_thg.py

Browse files
Files changed (1) hide show
  1. ocrpages2ocrdocs_thg.py +32 -0
ocrpages2ocrdocs_thg.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import defaultdict
2
+ from datasets import Dataset, load_dataset
3
+
4
+ SOURCE = "divinity-library/thg-02" # page-level dataset with OCR column
5
+ TARGET = "divinity-library/thg-03"
6
+ OCR_COLUMN = "markdown_glm_ocr"
7
+ PAGE_MARKERS = True # insert <!-- page N --> between pages
8
+
9
+ # Drop the image column so no images are decoded
10
+ ds = load_dataset(SOURCE, split="train").select_columns(["source", "page", OCR_COLUMN])
11
+
12
+ pages = defaultdict(list)
13
+ for row in ds:
14
+ pages[row["source"]].append((row["page"], row[OCR_COLUMN]))
15
+
16
+ docs = []
17
+ for source, items in sorted(pages.items()):
18
+ items.sort(key=lambda x: x[0])
19
+ failed = [p for p, text in items if text == "[OCR ERROR]"]
20
+ if failed:
21
+ print(f"WARNING {source}: OCR failed on pages {failed}")
22
+ parts = [
23
+ (f"<!-- page {p} -->\n" if PAGE_MARKERS else "") + text
24
+ for p, text in items
25
+ ]
26
+ docs.append({
27
+ "source": source,
28
+ "num_pages": len(items),
29
+ "markdown": "\n\n".join(parts),
30
+ })
31
+
32
+ Dataset.from_list(docs).push_to_hub(TARGET, private=True)