|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import shutil |
| 5 | +from typing import Union |
| 6 | + |
| 7 | +import nbformat |
| 8 | +from nbformat.notebooknode import NotebookNode |
| 9 | +from distutils.util import strtobool |
| 10 | + |
| 11 | +parser = argparse.ArgumentParser() |
| 12 | +parser.add_argument("--tutorials-dir", default="tutorials") |
| 13 | +parser.add_argument("--tutorials-bilingual-dir", default="tutorials/bilingual") |
| 14 | +parser.add_argument("--remove-bilingual-dir", type=strtobool, default="false") |
| 15 | +args = parser.parse_args() |
| 16 | + |
| 17 | + |
| 18 | +def split_notebook( |
| 19 | + notebook: Union[str, pathlib.Path], |
| 20 | + jp_notebook: Union[str, pathlib.Path], |
| 21 | + en_notebook: Union[str, pathlib.Path], |
| 22 | + strict: bool = True, |
| 23 | +) -> None: |
| 24 | + # When `strict=True`, all cell must be annotated by [JP], [EN] or [ALL]. |
| 25 | + with open(notebook, "r") as f: |
| 26 | + text = f.read().replace("\n", "") |
| 27 | + notebook = nbformat.reads(text, as_version=4) |
| 28 | + |
| 29 | + JP_cell = [] |
| 30 | + EN_cell = [] |
| 31 | + |
| 32 | + for i, cell in enumerate(notebook.cells): |
| 33 | + if cell["cell_type"] == "markdown": |
| 34 | + if cell["source"][:5] == "[EN]\n": |
| 35 | + cell["source"] = cell["source"][5:] |
| 36 | + if cell["source"].startswith("\n"): |
| 37 | + cell["source"] = cell["source"][1:] |
| 38 | + EN_cell.append(cell) |
| 39 | + elif cell["source"][:5] == "[JP]\n": |
| 40 | + cell["source"] = cell["source"][5:] |
| 41 | + if cell["source"].startswith("\n"): |
| 42 | + cell["source"] = cell["source"][1:] |
| 43 | + JP_cell.append(cell) |
| 44 | + elif cell["source"][:6] == "[ALL]\n": |
| 45 | + cell["source"] = cell["source"][6:] |
| 46 | + if cell["source"].startswith("\n"): |
| 47 | + cell["source"] = cell["source"][1:] |
| 48 | + JP_cell.append(cell) |
| 49 | + EN_cell.append(cell) |
| 50 | + else: |
| 51 | + if strict: |
| 52 | + raise RuntimeError(f"INFO : cannot find language annotation for cell {i}") |
| 53 | + else: |
| 54 | + print(f"INFO : cannot find language annotation for cell {i}, considered as both JP & EN") |
| 55 | + JP_cell.append(cell) |
| 56 | + EN_cell.append(cell) |
| 57 | + else: |
| 58 | + JP_cell.append(cell) |
| 59 | + EN_cell.append(cell) |
| 60 | + |
| 61 | + if len(JP_cell) != len(EN_cell): |
| 62 | + if strict: |
| 63 | + raise RuntimeError(f"The number of cell is different! JP {len(JP_cell)} != EN {len(EN_cell)}.") |
| 64 | + else: |
| 65 | + print(f"WARNING: the number of cell is different! JP {len(JP_cell)} != EN {len(EN_cell)}.") |
| 66 | + |
| 67 | + with open(en_notebook, mode="wt") as f: |
| 68 | + EN_nb = NotebookNode( |
| 69 | + { |
| 70 | + "cells": EN_cell, |
| 71 | + "metadata": notebook["metadata"], |
| 72 | + "nbformat": notebook["nbformat"], |
| 73 | + "nbformat_minor": notebook["nbformat_minor"], |
| 74 | + } |
| 75 | + ) |
| 76 | + nbformat.write(EN_nb, f) |
| 77 | + |
| 78 | + with open(jp_notebook, mode="wt") as f: |
| 79 | + JP_nb = NotebookNode( |
| 80 | + { |
| 81 | + "cells": JP_cell, |
| 82 | + "metadata": notebook["metadata"], |
| 83 | + "nbformat": notebook["nbformat"], |
| 84 | + "nbformat_minor": notebook["nbformat_minor"], |
| 85 | + } |
| 86 | + ) |
| 87 | + nbformat.write(JP_nb, f) |
| 88 | + |
| 89 | + |
| 90 | +project_root_dir = pathlib.Path(os.path.abspath(__file__)).parent.parent.parent |
| 91 | +tutorials = project_root_dir / args.tutorials_dir |
| 92 | +tutorials_bilingual = project_root_dir / args.tutorials_bilingual_dir |
| 93 | +print(f"project_root_dir : {project_root_dir}") |
| 94 | +print(f"tutorials : {tutorials}") |
| 95 | +print(f"tutorials_bilingual: {tutorials_bilingual}") |
| 96 | +file_names = sorted([f.name.replace("".join(f.suffixes), "") for f in tutorials_bilingual.glob("*.ipynb")]) |
| 97 | +print(f"file_names: {file_names}") |
| 98 | +tutorials_jp = tutorials / "ja" |
| 99 | +tutorials_en = tutorials / "en" |
| 100 | + |
| 101 | +tutorials.mkdir(exist_ok=True, parents=True) |
| 102 | +tutorials_jp.mkdir(exist_ok=True, parents=True) |
| 103 | +tutorials_en.mkdir(exist_ok=True, parents=True) |
| 104 | + |
| 105 | +for f in file_names: |
| 106 | + print(f) |
| 107 | + split_notebook( |
| 108 | + notebook=tutorials_bilingual / f"{f}.ipynb", |
| 109 | + jp_notebook=tutorials_jp / f"{f}.ipynb", |
| 110 | + en_notebook=tutorials_en / f"{f}.ipynb", |
| 111 | + ) |
| 112 | + |
| 113 | +# Copy `output` directory |
| 114 | +shutil.copytree(tutorials_bilingual / "output", tutorials_jp / "output") |
| 115 | +shutil.copytree(tutorials_bilingual / "output", tutorials_en / "output") |
| 116 | + |
| 117 | +if args.remove_bilingual_dir: |
| 118 | + shutil.rmtree(tutorials_bilingual) |
0 commit comments