// 訳文に残ってしまった伏せ字を、括弧の形を問わずに洗い出す。
import fs from 'fs';
const tm = JSON.parse(fs.readFileSync(process.argv[2] || 'tm.json', 'utf8'));

// 1〜3文字の大文字を何かで囲んだもの全部
const WIDE = /[‹›〈-】「-』＜＞（）［］｛｝[\](){}⟦⟧「」『』〈〉《》【】〔〕]/;
const CAND = /([‹〈《【〔＜（［｛[({⟦「『])\s*([A-Z]{1,3})\s*([›〉》】〕＞）］｝\])}⟧」』])/g;

const hits = [];
for (const [en, ja] of Object.entries(tm)) {
  const found = [...ja.matchAll(CAND)].map(m => m[0]);
  if (found.length) hits.push({ en, ja, found });
}
console.log(`残存する伏せ字らしきもの: ${hits.length} 件 / ${Object.keys(tm).length} 件`);
const forms = {};
hits.forEach(h => h.found.forEach(f => { forms[f] = (forms[f] || 0) + 1; }));
console.log('形:', Object.entries(forms).sort((a, b) => b[1] - a[1]).slice(0, 15).map(([f, n]) => `${f}×${n}`).join('  '));
hits.slice(0, 5).forEach(h => {
  console.log('---');
  console.log('EN:', h.en.slice(0, 90));
  console.log('JA:', h.ja.slice(0, 110));
});
fs.writeFileSync('sentinel-hits.json', JSON.stringify(hits.map(h => h.en), null, 1));
