# Reading Order for Republican-Era Chinese Newspapers

CS224V final project, Track A (CHURRO-Synthesis: domain-adaptive OCR through distillation). Yueer Zhou and Ray Hung.

We reorder the OCR lines of *Jingbao* (晶報) into article reading order without changing the text. A Qwen3.5-9B
vision-language student, trained only on Claude's labels, answers local multiple-choice questions over page crops
("after this run ends, which of these eight nearby runs is read next?"). Each question is asked three ways, and a
LightGBM classifier decides whether to jump or keep box order.

| Test, 23 spreads (block order) | Score |
|---|---|
| Box order | 0.538 |
| Kit baseline student | 0.548 |
| **Ours** | **0.663** |
| Claude (teacher) | 0.712 |

> This repository includes course data (OCR lines, labels, page crops), shared with the course staff's permission.

**[Report](report.pdf)** · **[Poster](poster.pdf)** · **[Project page](https://zhouyueer7.github.io/newspaper-reading-order/)** · **[Interactive viewer](demo/viewer/index.html)** · **[Video](https://youtu.be/BcJQwHRfqAI)**

## Quick start: replay the final system (CPU, about one second)

```bash
python3 demo/replay.py                                    # scores of every test spread: box order / ours / Claude
python3 demo/replay.py --fold jb_0129_1920-04-03_0002to0003 --page 2
                                                          # our reading order of one spread, run by run, with every jump
```

No model is run and nothing beyond Python 3 is needed. `demo/data/pred_best.json` holds the final system's outputs
on test: every candidate link with its classifier probability, and the decision threshold. `demo/data/test_pages.json`
holds the test pages with their runs. `replay.py` decodes them exactly as the scorer did (it checks this for every
spread) and scores them with the course kit's scorer in `OCR-JIT-main/ecpo`.

**Interactive viewer:** open `demo/viewer/index.html` in a browser (no server needed). Pick one of five featured
articles or any human-labelled block of the 23 test spreads, choose which of eight systems to compare (human order, box
order, kit student, Codex alone, Claude Code alone, Claude Code + 4 hints, ours, Claude), and see the block on the page,
how each system breaks it, what text is read in between, and the block as each system reads it. `demo/build_viewer.py`
writes its data by mapping every system's output lines back to the OCR lines, and its means reproduce the reported
test scores.

**Project page:** `index.html` (with `assets/`), a short visual version of the report, online at
https://zhouyueer7.github.io/newspaper-reading-order/.

## Layout

| Path | What it is |
|---|---|
| `report.pdf`, `poster.pdf` | Final report and poster |
| `index.html`, `assets/` | Project page |
| `analysis/` | The final method: the 19 scripts that produced the reported number, as they ran on the SC cluster |
| `analysis/slurm/` | The job scripts of the final pipeline (cluster paths, SLURM settings) |
| `demo/replay.py`, `demo/data/` | CPU replay of the final system (above) |
| `demo/viewer/`, `demo/build_viewer.py` | Interactive viewer and the script that builds its data |
| `OCR-JIT-main/` | The course starter kit: data, scorer, LoRA trainer |

Earlier experiments that were tried and dropped are not included. The project page is served with GitHub Pages from the
repository root (Settings, Pages, branch `main`, folder `/`).

## The final method

Five steps per page (report Section 4). All models are LoRA adapters trained on Claude's dev groupings only. Human
labels are used only for scoring and for choosing the decision threshold on dev. Dev is split into two halves by issue:
each half's models are trained on the other half, and test uses the mean of both.

| Step | What happens | Scripts |
|---|---|---|
| 1 Cut | An image cutter (Qwen3.5-9B) joins neighbouring columns of a box into runs | `vl_cut.py`, `vl_cut_runs.py`, `vl_cut_testruns.py` |
| 2 Propose | A LightGBM ranker over geometry keeps 8 candidate next runs (box order's next is always one) | `geo_rank.py`, `colgrid.py` |
| 3 Ask | The student sees a page crop and the candidates' text and answers A to H or Z | `geo_mc.py`, `vl_items.py`, `vl_sft.py`, `vl_rl_train.py`, `geo_rl_reward.py` |
| 4 Check | The same question with the options reversed, and a backward question | `geo_tta.py`, `geo_mcb.py` |
| 5 Decide | A two-sided LightGBM edge classifier, greedy over links above a threshold, box order otherwise | `geo_edge2.py`, `geo_decode.py` |

Shared: `geo_pairs.py` (pages, geometry, kit paths), `human_labels.py` (label transfer to OCR lines),
`geo_infer.py` (text cutter for the dev runs). Test: `test_prep.py`, `test_score.py`.

### Order of the runs (from `OCR-JIT-main`, on the cluster)

1. **Dev runs.** `geo_pairs.py` builds the pairwise data, `slurm/geo_job.sh P1 {0,1}` (TAG=P1x) trains the Qwen2.5-7B
   text cutter per half and `geo_infer.py` writes the dev pages with runs to `runs/geo_out_x`.
2. **Candidates.** `geo_rank.py runs/geo_out_x P1` (ranker per half).
3. **Forward items and student.** `geo_mc.py build` (ACTX=6 BCTX=4 XBOX=1), then `vl_items.py` adds the page crops
   (`runs/geo_mc_vl`). `slurm/vl_job.sh {0,1} q9vl` (SFT, 3 epochs), `geo_rl_reward.py`, then
   `slurm/vl_rl_job.sh {0,1} q9vl q9vlrl` (RL).
4. **Reversed options.** `slurm/tta_jobs.sh` (`geo_tta.py make` / `merge`, tag `q9vlrlT`).
5. **Backward student.** `slurm/mcb_jobs.sh` (`geo_mcb.py build`, SFT, tag `q9b`).
6. **Image cutter for test.** `slurm/vlcut_prep.sh`, `slurm/vlcut_job.sh`, then `slurm/vcA_jobs.sh`: the all-dev
   cutter `q9cutA`, test runs at the same number of joins as the text cutter (`vl_cut_testruns.py`), test items
   (`test_prep.py`, `vl_items.py test`, `geo_mcb.py test`) and the students' answers on test (`geo_mc.py infer --cv 2`).
7. **Score.** `slurm/demo_pred.sh`: `test_score.py` with `FINE=1 QMATCH=1 BACK=...:q9b` gives 0.663 and writes
   `pred_best.json` (copied to `demo/data/`).

Adapters (not in git, on the cluster under `/juice2/scr2/nlp/cs224v/yueerz/runs/`): `mc_q9vlrl_cv{0,1}` (forward
student), `mcb_q9b_cv{0,1}` (backward student), `vc_q9cutA` (image cutter), `geo_P1x_cv{0,1}` (text cutter). Page
images come from the public CHURRO dataset (`VL_IMAGES`). The environment is `OCR-JIT-main/.venv` (Python 3.11,
torch, vLLM, transformers, peft, LightGBM); `slurm/sc_profile.sh` sets the cluster paths.

The scripts are kept exactly as they ran, so they still contain switches for variants we tried. Everything not listed
above is off by default.
