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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
restore_english_tags.py — 翻訳済みファイルのタグ復元スクリプト
|
| 3 |
+
|
| 4 |
+
翻訳時に日本語化されてしまった以下のタグを英語版に復元:
|
| 5 |
+
1. ######## (シナリオヘッダー)
|
| 6 |
+
2. {{XXX}} (プレースホルダー)
|
| 7 |
+
3. /XXX (スラッシュタグ)
|
| 8 |
+
4. [XXX] (角括弧タグ)
|
| 9 |
+
5. [/XXX] (閉じタグ)
|
| 10 |
+
|
| 11 |
+
使用方法:
|
| 12 |
+
python restore_english_tags.py
|
| 13 |
+
python restore_english_tags.py --dry-run # 変更内容を確認のみ
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import argparse
|
| 17 |
+
import re
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
from typing import Any
|
| 20 |
+
|
| 21 |
+
# ── 設定 ──────────────────────────────────────────────────────
|
| 22 |
+
WORKSPACE = Path("/workspace/eqbench-ja")
|
| 23 |
+
EQBENCH_DATA_DIR = WORKSPACE / "eqbench3" / "data"
|
| 24 |
+
|
| 25 |
+
# 対象ファイル
|
| 26 |
+
TARGET_FILES = [
|
| 27 |
+
"scenario_prompts.txt",
|
| 28 |
+
"scenario_notes.txt",
|
| 29 |
+
"scenario_master_prompt_message_drafting.txt",
|
| 30 |
+
"scenario_master_prompt_analysis.txt",
|
| 31 |
+
"scenario_master_prompt.txt",
|
| 32 |
+
"rubric_scoring_prompt_analysis.txt",
|
| 33 |
+
"rubric_scoring_prompt.txt",
|
| 34 |
+
"rubric_scoring_criteria_analysis.txt",
|
| 35 |
+
"rubric_scoring_criteria.txt",
|
| 36 |
+
"pairwise_prompt_eqbench3_analysis.txt",
|
| 37 |
+
"pairwise_prompt_eqbench3.txt",
|
| 38 |
+
"debrief_prompt.txt",
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# ── 復元パターン定義 ──────────────────────────────────────────
|
| 42 |
+
# (説明, 正規表現パターン, 抽出・復元方法)
|
| 43 |
+
|
| 44 |
+
RESTORE_PATTERNS = [
|
| 45 |
+
# 1. シナリオヘッダー: ######## 1 | Category | ...
|
| 46 |
+
(
|
| 47 |
+
"scenario_header",
|
| 48 |
+
re.compile(r'^(#{8,})\s*(\d+)\s*\|\s*([^|]+)\s*\|'),
|
| 49 |
+
"keep_original", # 行全体を英語版から保持
|
| 50 |
+
),
|
| 51 |
+
# 2. プレースホルダー: {{XXX}}
|
| 52 |
+
(
|
| 53 |
+
"placeholder",
|
| 54 |
+
re.compile(r'\{\{[^}]+\}\}'),
|
| 55 |
+
"keep_exact", # そのまま保持
|
| 56 |
+
),
|
| 57 |
+
# 3. プレースホルダー(単一括弧): {XXX}
|
| 58 |
+
(
|
| 59 |
+
"placeholder_single",
|
| 60 |
+
re.compile(r'\{[^{}]+\}'),
|
| 61 |
+
"keep_exact",
|
| 62 |
+
),
|
| 63 |
+
# 4. スラッシュタグ: /think, /response など
|
| 64 |
+
(
|
| 65 |
+
"slash_tag",
|
| 66 |
+
re.compile(r'(?<![a-zA-Z])/[a-zA-Z_][a-zA-Z0-9_]*(?![a-zA-Z0-9_])'),
|
| 67 |
+
"keep_exact",
|
| 68 |
+
),
|
| 69 |
+
# 5. 角括弧タグ: [INST], [/INST], [THOUGHT] など
|
| 70 |
+
(
|
| 71 |
+
"bracket_tag",
|
| 72 |
+
re.compile(r'\[/?[A-Z_][A-Z0-9_]*\]', re.IGNORECASE),
|
| 73 |
+
"keep_exact",
|
| 74 |
+
),
|
| 75 |
+
# 6. プロンプトセパレーター: ####### Prompt1 など
|
| 76 |
+
(
|
| 77 |
+
"prompt_separator",
|
| 78 |
+
re.compile(r'^(#{7,})\s*Prompt\d*'),
|
| 79 |
+
"keep_original",
|
| 80 |
+
),
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class TagRestorer:
|
| 85 |
+
"""英語版タグの復元を行うクラス"""
|
| 86 |
+
|
| 87 |
+
def __init__(self, dry_run: bool = False):
|
| 88 |
+
self.dry_run = dry_run
|
| 89 |
+
self.stats: dict[str, dict[str, int]] = {}
|
| 90 |
+
|
| 91 |
+
def extract_tags(self, text: str) -> dict[str, list[str]]:
|
| 92 |
+
"""テキストから保護対象のタグを抽出"""
|
| 93 |
+
tags = {}
|
| 94 |
+
for name, pattern, _ in RESTORE_PATTERNS:
|
| 95 |
+
matches = pattern.findall(text)
|
| 96 |
+
if matches:
|
| 97 |
+
tags[name] = matches if isinstance(matches[0], str) else [m[0] if isinstance(m, tuple) else m for m in matches]
|
| 98 |
+
return tags
|
| 99 |
+
|
| 100 |
+
def extract_lines_with_tags(self, text: str) -> dict[int, str]:
|
| 101 |
+
"""タグを含む行を抽出(行番号 -> 行内容)"""
|
| 102 |
+
lines = text.split('\n')
|
| 103 |
+
tagged_lines = {}
|
| 104 |
+
for i, line in enumerate(lines):
|
| 105 |
+
for name, pattern, mode in RESTORE_PATTERNS:
|
| 106 |
+
if pattern.search(line) and mode == "keep_original":
|
| 107 |
+
tagged_lines[i] = line
|
| 108 |
+
break
|
| 109 |
+
return tagged_lines
|
| 110 |
+
|
| 111 |
+
def restore_file(self, filepath: Path) -> dict[str, Any]:
|
| 112 |
+
"""単一ファイルのタグを復元"""
|
| 113 |
+
# 英語版バックアップを探す
|
| 114 |
+
en_backup = Path(str(filepath) + ".en.bak")
|
| 115 |
+
if not en_backup.exists():
|
| 116 |
+
# 別のバックアップ場所も試す
|
| 117 |
+
en_backup = filepath.parent / (filepath.name.replace(".txt", "_en.txt"))
|
| 118 |
+
|
| 119 |
+
if not en_backup.exists():
|
| 120 |
+
return {"status": "skip", "reason": "英語版バックアップが見つかりません"}
|
| 121 |
+
|
| 122 |
+
# ファイル読み込み
|
| 123 |
+
ja_text = filepath.read_text(encoding="utf-8")
|
| 124 |
+
en_text = en_backup.read_text(encoding="utf-8")
|
| 125 |
+
|
| 126 |
+
if ja_text == en_text:
|
| 127 |
+
return {"status": "skip", "reason": "英語版と同一"}
|
| 128 |
+
|
| 129 |
+
# タグ抽出
|
| 130 |
+
en_tags = self.extract_tags(en_text)
|
| 131 |
+
en_tagged_lines = self.extract_lines_with_tags(en_text)
|
| 132 |
+
|
| 133 |
+
# 復元処理
|
| 134 |
+
ja_lines = ja_text.split('\n')
|
| 135 |
+
en_lines = en_text.split('\n')
|
| 136 |
+
changes = []
|
| 137 |
+
|
| 138 |
+
# 行単位の復元(シナリオヘッダーなど)
|
| 139 |
+
for line_idx, en_line in en_tagged_lines.items():
|
| 140 |
+
if line_idx < len(ja_lines):
|
| 141 |
+
ja_line = ja_lines[line_idx]
|
| 142 |
+
if ja_line != en_line:
|
| 143 |
+
# 行全体を置換
|
| 144 |
+
changes.append({
|
| 145 |
+
"line": line_idx + 1,
|
| 146 |
+
"type": "line_replace",
|
| 147 |
+
"before": ja_line[:80] + "..." if len(ja_line) > 80 else ja_line,
|
| 148 |
+
"after": en_line[:80] + "..." if len(en_line) > 80 else en_line,
|
| 149 |
+
})
|
| 150 |
+
ja_lines[line_idx] = en_line
|
| 151 |
+
|
| 152 |
+
# インラインタグの復元
|
| 153 |
+
restored_text = '\n'.join(ja_lines)
|
| 154 |
+
|
| 155 |
+
# プレースホルダー {{XXX}} の復元
|
| 156 |
+
ja_placeholders = re.findall(r'\{\{[^}]+\}\}', restored_text)
|
| 157 |
+
en_placeholders = re.findall(r'\{\{[^}]+\}\}', en_text)
|
| 158 |
+
|
| 159 |
+
# 日本語版のプレースホルダーを英語版のものに置換
|
| 160 |
+
# 行番号対応でマッチング
|
| 161 |
+
for match in re.finditer(r'\{\{[^}]+\}\}', restored_text):
|
| 162 |
+
ja_ph = match.group()
|
| 163 |
+
# 対応する英語版プレースホルダーを探す
|
| 164 |
+
# 同じ行番号付近で探す
|
| 165 |
+
start_pos = match.start()
|
| 166 |
+
line_num = restored_text[:start_pos].count('\n')
|
| 167 |
+
|
| 168 |
+
# 英語版の同じ行付近で同じ種類のプレースホルダーを探す
|
| 169 |
+
for en_ph in en_placeholders:
|
| 170 |
+
if en_ph not in restored_text and ja_ph != en_ph:
|
| 171 |
+
# プレースホルダー名が似ているかチェック
|
| 172 |
+
ja_inner = ja_ph[2:-2]
|
| 173 |
+
en_inner = en_ph[2:-2]
|
| 174 |
+
# 日本語が含まれている場合は置換
|
| 175 |
+
if re.search(r'[^\x00-\x7F]', ja_inner):
|
| 176 |
+
changes.append({
|
| 177 |
+
"position": start_pos,
|
| 178 |
+
"type": "placeholder",
|
| 179 |
+
"before": ja_ph,
|
| 180 |
+
"after": en_ph,
|
| 181 |
+
})
|
| 182 |
+
restored_text = restored_text.replace(ja_ph, en_ph, 1)
|
| 183 |
+
break
|
| 184 |
+
|
| 185 |
+
# 単一括弧プレースホルダー {XXX} の復元
|
| 186 |
+
for match in re.finditer(r'\{[^{}]+\}', restored_text):
|
| 187 |
+
ja_ph = match.group()
|
| 188 |
+
if re.search(r'[^\x00-\x7F]', ja_ph): # 日本語が含まれる
|
| 189 |
+
# 英語版から対応するプレースホルダーを探す
|
| 190 |
+
for en_ph in re.finditer(r'\{[^{}]+\}', en_text):
|
| 191 |
+
en_match = en_ph.group()
|
| 192 |
+
if en_match not in restored_text:
|
| 193 |
+
changes.append({
|
| 194 |
+
"position": match.start(),
|
| 195 |
+
"type": "placeholder_single",
|
| 196 |
+
"before": ja_ph,
|
| 197 |
+
"after": en_match,
|
| 198 |
+
})
|
| 199 |
+
restored_text = restored_text.replace(ja_ph, en_match, 1)
|
| 200 |
+
break
|
| 201 |
+
|
| 202 |
+
# 角括弧タグ [XXX], [/XXX] の復元
|
| 203 |
+
for match in re.finditer(r'\[/?[^\]]+\]', restored_text):
|
| 204 |
+
ja_tag = match.group()
|
| 205 |
+
if re.search(r'[^\x00-\x7F]', ja_tag): # 日本語が含まれる
|
| 206 |
+
# 英語版から対応するタグを探す
|
| 207 |
+
for en_match in re.finditer(r'\[/?[^\]]+\]', en_text):
|
| 208 |
+
en_tag = en_match.group()
|
| 209 |
+
if en_tag not in restored_text and not re.search(r'[^\x00-\x7F]', en_tag):
|
| 210 |
+
# 開きタグ/閉じタグの対応をチェック
|
| 211 |
+
if ja_tag.startswith('[/') == en_tag.startswith('[/'):
|
| 212 |
+
changes.append({
|
| 213 |
+
"position": match.start(),
|
| 214 |
+
"type": "bracket_tag",
|
| 215 |
+
"before": ja_tag,
|
| 216 |
+
"after": en_tag,
|
| 217 |
+
})
|
| 218 |
+
restored_text = restored_text.replace(ja_tag, en_tag, 1)
|
| 219 |
+
break
|
| 220 |
+
|
| 221 |
+
# スラッシュタグ /XXX の復元
|
| 222 |
+
for match in re.finditer(r'(?<![a-zA-Z])/[a-zA-Z_][a-zA-Z0-9_]*(?![a-zA-Z0-9_])', restored_text):
|
| 223 |
+
tag = match.group()
|
| 224 |
+
# これは英語なのでそのまま
|
| 225 |
+
|
| 226 |
+
# 日本語化されたスラッシュタグを探す(例:/思考 → /think)
|
| 227 |
+
ja_slash_tags = re.findall(r'(?<![a-zA-Z])/[^\s\n"' + r"']+", restored_text)
|
| 228 |
+
en_slash_tags = re.findall(r'(?<![a-zA-Z])/[a-zA-Z_][a-zA-Z0-9_]*(?![a-zA-Z0-9_])', en_text)
|
| 229 |
+
|
| 230 |
+
for ja_tag in ja_slash_tags:
|
| 231 |
+
if re.search(r'[^\x00-\x7F]', ja_tag): # 日本語が含まれる
|
| 232 |
+
# 英語版から対応するタグを探す
|
| 233 |
+
for en_tag in en_slash_tags:
|
| 234 |
+
if en_tag not in restored_text:
|
| 235 |
+
changes.append({
|
| 236 |
+
"type": "slash_tag",
|
| 237 |
+
"before": ja_tag,
|
| 238 |
+
"after": en_tag,
|
| 239 |
+
})
|
| 240 |
+
restored_text = restored_text.replace(ja_tag, en_tag, 1)
|
| 241 |
+
break
|
| 242 |
+
|
| 243 |
+
if not changes:
|
| 244 |
+
return {"status": "skip", "reason": "変更なし"}
|
| 245 |
+
|
| 246 |
+
# ファイル保存
|
| 247 |
+
if not self.dry_run:
|
| 248 |
+
filepath.write_text(restored_text, encoding="utf-8")
|
| 249 |
+
|
| 250 |
+
return {
|
| 251 |
+
"status": "success",
|
| 252 |
+
"changes": changes,
|
| 253 |
+
"change_count": len(changes),
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
def restore_all(self) -> None:
|
| 257 |
+
"""全ファイルのタグを復元"""
|
| 258 |
+
print("=" * 60)
|
| 259 |
+
print("EQ-Bench3 タグ復元スクリプト")
|
| 260 |
+
print("=" * 60)
|
| 261 |
+
print(f"対象ディレクトリ: {EQBENCH_DATA_DIR}")
|
| 262 |
+
print(f"ドライラン: {self.dry_run}")
|
| 263 |
+
print()
|
| 264 |
+
|
| 265 |
+
total_changes = 0
|
| 266 |
+
|
| 267 |
+
for filename in TARGET_FILES:
|
| 268 |
+
filepath = EQBENCH_DATA_DIR / filename
|
| 269 |
+
if not filepath.exists():
|
| 270 |
+
print(f"[skip] {filename} - ファイルが見つかりません")
|
| 271 |
+
continue
|
| 272 |
+
|
| 273 |
+
print(f"[process] {filename}")
|
| 274 |
+
result = self.restore_file(filepath)
|
| 275 |
+
|
| 276 |
+
if result["status"] == "skip":
|
| 277 |
+
print(f" ⏭️ スキップ: {result['reason']}")
|
| 278 |
+
elif result["status"] == "success":
|
| 279 |
+
print(f" ✅ 復元完了: {result['change_count']}件の変更")
|
| 280 |
+
total_changes += result['change_count']
|
| 281 |
+
|
| 282 |
+
if self.dry_run and result.get("changes"):
|
| 283 |
+
for change in result["changes"][:5]: # 最初の5件のみ表示
|
| 284 |
+
print(f" - {change['type']}: '{change['before']}' → '{change['after']}'")
|
| 285 |
+
if len(result["changes"]) > 5:
|
| 286 |
+
print(f" ... 他 {len(result['changes']) - 5}件")
|
| 287 |
+
|
| 288 |
+
self.stats[filename] = result
|
| 289 |
+
|
| 290 |
+
print()
|
| 291 |
+
print("=" * 60)
|
| 292 |
+
if self.dry_run:
|
| 293 |
+
print(f"[dry-run] 合計 {total_changes}件の変更が検出されました")
|
| 294 |
+
print("実際に変更を適用するには --dry-run なしで実行してください")
|
| 295 |
+
else:
|
| 296 |
+
print(f"✅ 復元完了: 合計 {total_changes}件の変更を適用")
|
| 297 |
+
print("=" * 60)
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
def main():
|
| 301 |
+
parser = argparse.ArgumentParser(description="翻訳済みファイルのタグ復元")
|
| 302 |
+
parser.add_argument("--dry-run", action="store_true", help="変更を確認のみ(実際には適用しない)")
|
| 303 |
+
args = parser.parse_args()
|
| 304 |
+
|
| 305 |
+
restorer = TagRestorer(dry_run=args.dry_run)
|
| 306 |
+
restorer.restore_all()
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
if __name__ == "__main__":
|
| 310 |
+
main()
|