英文の文中改行をスペースに置換する

英語の長文を翻訳サービスにかけたいとき、困るのが文中の改行。

メールやPDF等で遭遇することがたまにあるのですが、改行が存在するまま翻訳にかけると、そこで文が一度区切られる扱いとなり、翻訳がおかしくなってしまいます。かといって、長文だと手作業で改行をスペースに変換していくのは非効率的です。

例えば、以下のような文章。

Over the last several months we have been working to finalize version 2.7.1 of the Mozilla Root
Store Policy (MRSP). This is tracked in GitHub, and discussions have been held on the
mozilla.dev.security.policy list. As mentioned at the last Face-to-Face meeting in October,
proposed changes are indicated in GitHub with the 2.7.1 label and are also tracked in this
GitHub redline - https://github.com/mozilla/pkipolicy/compare/master...BenWilson-Mozilla:2.7.1.
(Future proposed changes have now been flagged with the 2.8 label.) We expect to be done
with the comment process soon, and we will also be publishing CA communication and a survey
pertaining to version 2.7.1 of the MRSP. We are targeting to have this done before June 30,
2021.

CA/Bフォーラムのドキュメントです。該当テキストはPDFで幅が決まっているため、そのままテキストをコピーすると上述引用の通りピリオド以外の場所で改行が入ってしまいます。

そこで、こうした改行を上手く扱うための変換器をサクッと作ることにしました。

    document.querySelector('#origin').addEventListener('blur', (e) => {
        document.querySelector('#converted').value = e.currentTarget.value
                                                        .replace(/(\u200B|\u0020|\u00A0)/g, ' ')
                                                        .replace(
                                                            /[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g,
                                                            ''
                                                        )
                                                        .replace(
                                                            /[^\.\n]\r?\n/ig,
                                                            ' '
                                                        );
    });

やっていることは以下の通り。

  1. ゼロ幅スペースやnbspはただの半角スペースに置換
  2. 制御文字は削除
  3. 1.,2.を経てピリオドが直前にない改行は半角スペースに置換

今回は英文の翻訳のためなので、ピリオドで終わっている文章はそのまま改行していた方が見やすいです。そこはそのままで、改行の直前にピリオドがない場合のみ文中改行と見なして半角スペースに置換する、というルールにしました。

上述コードの変換を通すと、冒頭の文章は以下のようになりました。

Over the last several months we have been working to finalize version 2.7.1 of the Mozilla Roo Store Policy (MRSP). This is tracked in GitHub, and discussions have been held on th mozilla.dev.security.policy list. As mentioned at the last Face-to-Face meeting in October proposed changes are indicated in GitHub with the 2.7.1 label and are also tracked in thi GitHub redline - https://github.com/mozilla/pkipolicy/compare/master...BenWilson-Mozilla:2.7.1.
(Future proposed changes have now been flagged with the 2.8 label.) We expect to be don with the comment process soon, and we will also be publishing CA communication and a surve pertaining to version 2.7.1 of the MRSP. We are targeting to have this done before June 30 2021.

翻訳内で文章が変なところで途切れなくなったので誤訳が減りました。色々粗はあるかもしれませんが、目的は達成できたのでひとまずfix。

参考

正規表現

制御文字

nbsp

zwsp

文字コード番号確認

この記事を書いた人

アルム=バンド

フロントエンド・バックエンド・サーバエンジニア。LAMPやNodeからWP、Gulpを使ってejs,Scss,JSのコーディングまで一通り。たまにRasPiで遊んだり、趣味で開発したり。