#!/usr/bin/env python3 """Carry the formulas of the first corpus file into the version 1 layout. The first published file (`data/formulas.jsonl`, 6,170 rows) held formulas from one export of Copernicus Publications' open-access article XML (`copernicus-jats`) and from two web pages (`crawl-focus`). This script rewrites those rows with the version 1 fields and writes, for each source, `.jsonl` and `.documents.jsonl` in the layout that the other extraction scripts produce, so that `assemble.py` reads them like any other group. Changes to the rows: - `id` is the first 16 hex characters of the SHA-256 of `mathml` (the first file used SHA-1); - `producer` is added: "Copernicus Publications" for the journal XML and the article page, "Mathoid (MediaWiki Math)" for the DeWiki page; - `markup` is added; - `carried_tex` becomes `tex` with `tex_kind` "author" (the TeX of the Wikipedia editors, read from the annotation of the page) or null. The `mathml` strings are not changed. Usage: carry_v0.py --v0 FORMULAS_JSONL --jats OAI_PAGE_XML --out DIR --v0 the first published file --jats the OAI-PMH page of Copernicus article XML the rows were taken from; it supplies title, authors, journal and year of each article --out directory that receives the four output files """ import argparse import html import json import os import re import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import corpus_common as cc # noqa: E402 CC_BY = ("CC-BY-4.0", "https://creativecommons.org/licenses/by/4.0/") RETRIEVED = "2026-12-03" PRODUCER_JATS = "Copernicus Publications" # Metadata read from the two pages on 2026-12-03; the licence statement of # each page is at `evidence`. PAGES = { "https://acp.copernicus.org/articles/18/3065/2018/index.html": { "producer": PRODUCER_JATS, "title": "Contrasting the co-variability of daytime cloud and precipitation over tropical land and ocean", "authors": "Daeho Jin; Lazaros Oreopoulos; Dongmin Lee; Nayeong Cho; Jackson Tan", "journal": "Atmospheric Chemistry and Physics", "year": "2018", "publisher": "Copernicus Publications", "evidence": "https://acp.copernicus.org/articles/18/3065/2018/", }, } DEWIKI = { "producer": "Mathoid (MediaWiki Math)", "title": "Ideales Gas", "authors": "Contributors to the German Wikipedia article " "(history: https://de.wikipedia.org/w/index.php?action=history&title=Ideales_Gas)", "journal": "DeWiki (copy of the German Wikipedia)", "year": None, "publisher": "DeWiki", "evidence": "https://dewiki.de/Lexikon/Ideales_Gas", } def strip(s): s = re.sub(r"<[^>]+>", "", s) return re.sub(r"\s+", " ", html.unescape(s)).strip() def read_jats(path): """Article metadata by DOI from an OAI-PMH page of JATS records.""" text = open(path, encoding="utf-8").read() arts = {} for body in re.findall(r"(.*?)", text, re.S): if "
([^<]+)', body).group(1) lic = " ".join(re.findall(r"].*?", body, re.S)) urls = set(re.findall(r'xlink:href="(https?://creativecommons\.org/[^"]+)"', lic)) authors = [] for c in re.findall(r"]*contrib-type=\"author\".*?", body, re.S): sn = re.search(r"(.*?)", c, re.S) gn = re.search(r"(.*?)", c, re.S) if sn: authors.append(strip((gn.group(1) + " " if gn else "") + sn.group(1))) arts[doi] = { "title": strip(re.search(r"(.*?)", body, re.S).group(1)), "authors": "; ".join(authors), "journal": strip(re.search(r"(.*?)", body, re.S).group(1)), "year": re.search(r"(\d{4})", body).group(1), "cc_by": urls == {CC_BY[1]} and "Attribution 4.0" in lic, } return arts def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--v0", required=True) ap.add_argument("--jats", required=True) ap.add_argument("--out", required=True) a = ap.parse_args() arts = read_jats(a.jats) os.makedirs(a.out, exist_ok=True) rows = {"copernicus-jats": [], "crawl-focus": []} docs = {"copernicus-jats": {}, "crawl-focus": {}} with open(a.v0, encoding="utf-8") as f: for line in f: r = json.loads(line) s, origin = r["source"], r["origin"] if s == "copernicus-jats": doi = origin.removeprefix("https://doi.org/") art = arts[doi] assert art["cc_by"], origin producer = PRODUCER_JATS docs[s].setdefault(origin, { "origin": origin, "title": art["title"], "authors": art["authors"], "journal": art["journal"], "year": art["year"], "publisher": "Copernicus Publications", "producer": producer, "licence": r["licence"], "licence_url": r["licence_url"], "access_url": origin, "retrieved": RETRIEVED}) else: page = PAGES.get(origin, DEWIKI) producer = page["producer"] docs[s].setdefault(origin, { "origin": origin, "title": page["title"], "authors": page["authors"], "journal": page["journal"], "year": page["year"], "publisher": page["publisher"], "producer": producer, "licence": r["licence"], "licence_url": r["licence_url"], "licence_source": page["evidence"], "access_url": origin, "retrieved": RETRIEVED}) tex = r["carried_tex"] rows[s].append({ "id": cc.row_id(r["mathml"]), "mathml": r["mathml"], "source": s, "producer": producer, "origin": origin, "licence": r["licence"], "licence_url": r["licence_url"], "context": r["context"], "tex": tex, "tex_kind": "author" if tex else None, "markup": cc.classify_markup(r["mathml"]), "count": r["count"]}) for s in rows: with open(os.path.join(a.out, s + ".jsonl"), "w", encoding="utf-8") as f: for r in rows[s]: f.write(json.dumps(r, ensure_ascii=False) + "\n") with open(os.path.join(a.out, s + ".documents.jsonl"), "w", encoding="utf-8") as f: for d in docs[s].values(): f.write(json.dumps(d, ensure_ascii=False) + "\n") print(s, len(rows[s]), "rows", len(docs[s]), "documents") if __name__ == "__main__": main()