File size: 13,656 Bytes
d49e060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""Rule-based checkers for the five hard constraints.

Ported unchanged from the internal scorer so published scores stay comparable.
Each checker returns ``{"is_valid": bool, ...}`` with diagnostic detail.
"""

from __future__ import annotations

import json
import re
from typing import Any

from .syllable import cal_syllable_count

HARD_CONSTRAINT_IDS = frozenset({
    "format_preserve", "layout_break", "term_compliance",
    "syllable_order", "social_preserve",
})
SOFT_CONSTRAINT_IDS = frozenset({
    "style_consistency", "context_disambiguate", "coref_resolution",
    "term_cross_sentence", "academic_format_preserve",
})


# ======================== Constraint text parsing ========================

def parse_term_targets(constraint_lines: list[str]) -> list[str]:
    """Target-side renderings from a `专名/术语对照: X→Y、...` line."""
    target_terms = []
    for line in constraint_lines:
        if "术语对照" not in line and "专名" not in line:
            continue
        match = re.search(r"(?:专名/?)?术语对照[::]\s*(.*)", line)
        if not match:
            continue
        for pair in re.split(r"[、,,]", match.group(1)):
            if "→" in pair:
                target = pair.split("→", 1)[1].strip()
                if target:
                    target_terms.append(target)
    return target_terms


def parse_layout_features(constraint_lines: list[str]) -> list[str]:
    features = []
    for line in constraint_lines:
        if "布局" not in line and "排版" not in line:
            continue
        if "换行" in line:
            features.append("newlines")
        if "缩进" in line:
            features.append("indent")
        if "表格" in line:
            features.append("table_align")
    return features if features else ["newlines", "indent"]


def parse_social_elements(constraint_lines: list[str]) -> list[str]:
    elements = []
    for line in constraint_lines:
        if "社交元素" not in line:
            continue
        for sep in (":", ":"):
            if sep in line:
                raw = line.split(sep, 1)[1].strip()
                elements.extend(p.strip() for p in re.split(r"[、,,]", raw) if p.strip())
                break
    return elements


def collect_soft_constraint_descs(
    constraint_ids: list[str], constraint_lines: list[str]
) -> dict[str, str]:
    """Map each soft constraint id to the prompt line stating it."""
    keyword_map = {
        "style_consistency": ["语体", "风格"],
        "context_disambiguate": ["歧义", "消歧"],
        "coref_resolution": ["指代", "代词"],
        "term_cross_sentence": ["跨句", "全文统一"],
        "academic_format_preserve": ["学术", "LaTeX", "学术格式"],
    }
    soft_descs: dict[str, str] = {}
    for cid in constraint_ids:
        if cid not in SOFT_CONSTRAINT_IDS:
            continue
        for line in constraint_lines:
            if any(kw in line for kw in keyword_map.get(cid, [])):
                soft_descs[cid] = line
                break
        soft_descs.setdefault(cid, cid)
    return soft_descs


# ======================== Rule-based checkers ========================

def _build_term_pattern(term: str) -> str:
    escaped = re.escape(term)
    # CJK has no word boundaries; \b would never match against them.
    if any(("一" <= ch <= "鿿") or ("぀" <= ch <= "ヿ") for ch in term):
        return escaped
    return rf"(?<!\w){escaped}(?!\w)"


def check_glossary(target_text: str, required_terms: list[str]) -> dict[str, Any]:
    missing = [t for t in required_terms
               if not re.search(_build_term_pattern(t), target_text)]
    return {"is_valid": not missing, "missing_terms": missing}


def check_json_preserved(source_text: str, target_text: str) -> dict[str, Any]:
    def extract_keys(text: str) -> tuple[bool, list[str]]:
        try:
            parsed = json.loads(text)
        except json.JSONDecodeError:
            return False, []
        keys: list[str] = []

        def walk(obj: Any, path: str = "") -> None:
            if isinstance(obj, dict):
                for key, value in obj.items():
                    current = f"{path}.{key}" if path else key
                    keys.append(current)
                    walk(value, current)
            elif isinstance(obj, list):
                for index, element in enumerate(obj):
                    walk(element, f"{path}[{index}]")

        walk(parsed)
        return True, keys

    src_valid, src_keys = extract_keys(source_text)
    tgt_valid, tgt_keys = extract_keys(target_text)
    if not src_valid:
        return {"is_valid": False, "issues": ["源文不是有效JSON"]}
    if not tgt_valid:
        return {"is_valid": False, "issues": ["译文不是有效JSON"]}

    issues = []
    missing = set(src_keys) - set(tgt_keys)
    extra = set(tgt_keys) - set(src_keys)
    if missing:
        issues.append(f"缺失key: {missing}")
    if extra:
        issues.append(f"多余key: {extra}")
    return {"is_valid": not issues, "issues": issues}


def _extract_visible_text(html: str) -> str:
    return re.sub(r"<[^>]+>", "", html).strip()


def _chinese_ratio(text: str) -> float:
    text = re.sub(r"\s+", "", text)
    if not text:
        return 0.0
    return sum(1 for ch in text if "一" <= ch <= "鿿") / len(text)


def check_html_preserved(
    source_text: str, target_text: str,
    src_lang: str | None = None, tgt_lang: str | None = None,
) -> dict[str, Any]:
    tag_pattern = re.compile(r"</?[a-z][a-z0-9]*\b[^>]*>", re.IGNORECASE)
    src_tag_types = [re.sub(r"\s+.*?>", ">", t) for t in tag_pattern.findall(source_text)]
    tgt_tag_types = [re.sub(r"\s+.*?>", ">", t) for t in tag_pattern.findall(target_text)]

    issues = []
    if src_tag_types != tgt_tag_types:
        issues.append(f"标签不一致: 源文{len(src_tag_types)}个, 译文{len(tgt_tag_types)}个")

    if src_lang == "zh" and tgt_lang not in ("ja", "zh"):
        tgt_visible = _extract_visible_text(target_text)
        if tgt_visible and _extract_visible_text(source_text):
            ratio = _chinese_ratio(tgt_visible)
            if ratio > 0.01:
                issues.append(f"译文中残留中文(占比{ratio:.1%})")

    return {"is_valid": not issues, "issues": issues}


def check_markdown_preserved(source_text: str, target_text: str) -> dict[str, Any]:
    md_patterns = [
        (r"^#{1,6}\s", "标题"), (r"\*\*[^*]+\*\*", "粗体"),
        (r"\*[^*]+\*", "斜体"), (r"`[^`]+`", "行内代码"),
        (r"```[\s\S]*?```", "代码块"), (r"^[-*+]\s", "列表项"),
        (r"^\d+\.\s", "有序列表"), (r"\[.*?\]\(.*?\)", "链接"),
        (r"!\[.*?\]\(.*?\)", "图片"), (r"^>\s", "引用"),
        (r"\|.*\|", "表格"),
    ]
    issues = []
    for pattern, name in md_patterns:
        if re.search(pattern, source_text, re.MULTILINE) and not re.search(
            pattern, target_text, re.MULTILINE
        ):
            issues.append(f"丢失{name}标记")
    return {"is_valid": not issues, "issues": issues}


def check_placeholder_preserved(source_text: str, target_text: str) -> dict[str, Any]:
    patterns = [
        r"\{[a-zA-Z_][a-zA-Z0-9_]*\}",
        r"%[dsf]",
        r"\$\d+",
        r"\{\{[a-zA-Z_][a-zA-Z0-9_]*\}\}",
    ]
    issues = []
    for pattern in patterns:
        missing = set(re.findall(pattern, source_text)) - set(re.findall(pattern, target_text))
        if missing:
            issues.append(f"丢失占位符: {missing}")
    return {"is_valid": not issues, "issues": issues}


def check_format_preserve(
    source_text: str, target_text: str,
    src_lang: str | None = None, tgt_lang: str | None = None,
) -> dict[str, Any]:
    """Dispatch to whichever format checkers the source text actually triggers."""
    results: dict[str, Any] = {}
    issues: list[str] = []
    stripped = source_text.strip()

    if stripped[:1] in ("{", "["):
        try:
            json.loads(stripped)
            is_json = True
        except json.JSONDecodeError:
            is_json = False
        if is_json:
            sub = check_json_preserved(stripped, target_text.strip())
            results["json"] = sub
            if not sub["is_valid"]:
                issues.extend(sub.get("issues", []))

    if re.search(r"</?[a-z][a-z0-9]*\b[^>]*>", source_text, re.IGNORECASE):
        sub = check_html_preserved(source_text, target_text, src_lang=src_lang, tgt_lang=tgt_lang)
        results["html"] = sub
        if not sub["is_valid"]:
            issues.extend(sub.get("issues", []))

    if re.search(r"^#{1,6}\s|\*\*|`|^[-*+]\s|^>\s|\[.*?\]\(.*?\)", source_text, re.MULTILINE):
        sub = check_markdown_preserved(source_text, target_text)
        results["markdown"] = sub
        if not sub["is_valid"]:
            issues.extend(sub.get("issues", []))

    if re.search(
        r"\{[a-zA-Z_][a-zA-Z0-9_]*\}|%[dsf]|\$\d+|\{\{[a-zA-Z_][a-zA-Z0-9_]*\}\}", source_text
    ):
        sub = check_placeholder_preserved(source_text, target_text)
        results["placeholder"] = sub
        if not sub["is_valid"]:
            issues.extend(sub.get("issues", []))

    return {"is_valid": not issues, "issues": issues, "sub_results": results}


def check_layout_preserved(
    source_text: str, target_text: str, layout_features: list[str] | None = None
) -> dict[str, Any]:
    layout_features = layout_features or ["newlines", "indent"]
    issues = []

    if "newlines" in layout_features:
        src_nl, tgt_nl = source_text.count("\n"), target_text.count("\n")
        if src_nl != tgt_nl:
            issues.append(f"换行数不一致: 源文{src_nl}, 译文{tgt_nl}")

    if "indent" in layout_features:
        src_indent = len(source_text) - len(source_text.lstrip())
        tgt_indent = len(target_text) - len(target_text.lstrip())
        if (src_indent > 0) != (tgt_indent > 0):
            issues.append("缩进风格不一致")

    if "table_align" in layout_features:
        src_has = any("|" in l and l.count("|") >= 2 for l in source_text.splitlines())
        tgt_has = any("|" in l and l.count("|") >= 2 for l in target_text.splitlines())
        if src_has and not tgt_has:
            issues.append("源文含表格对齐结构但译文丢失")

    return {"is_valid": not issues, "issues": issues}


def check_social_preserve(target_text: str, elements: list[str]) -> dict[str, Any]:
    if not elements:
        return {"is_valid": True, "note": "no elements to check"}
    missing = [e for e in elements if e not in target_text]
    return {"is_valid": not missing, "missing_elements": missing}


def check_syllable_order(
    prediction: str, durations: list[float], tgt_lang: str
) -> dict[str, Any]:
    """Rank-correlate per-sentence duration against translated syllable count.

    Passes at concordance >= 0.9. Pairs with equal durations, and pairs with equal
    syllable counts, are not counted as inversions.
    """
    if not durations or not prediction:
        return {"is_valid": True, "note": "no duration data"}

    try:
        output = json.loads(prediction)
    except (json.JSONDecodeError, TypeError):
        return {"is_valid": False, "note": "output is not valid JSON"}
    if not isinstance(output, dict):
        return {"is_valid": False, "note": "output is not a JSON object"}

    syllables = [
        cal_syllable_count(output.get(str(i), ""), tgt_lang)
        for i in range(1, len(durations) + 1)
    ]
    if len(syllables) < 2:
        return {"is_valid": True, "note": "too few sentences"}

    inversions = total_pairs = 0
    for i in range(len(durations)):
        for j in range(i + 1, len(durations)):
            if durations[i] == durations[j]:
                continue
            total_pairs += 1
            if syllables[i] == syllables[j]:
                continue
            if (durations[i] > durations[j]) != (syllables[i] > syllables[j]):
                inversions += 1

    if total_pairs == 0:
        return {"is_valid": True, "note": "all durations equal"}

    concordance = 1.0 - inversions / total_pairs
    return {
        "is_valid": concordance >= 0.9,
        "concordance": round(concordance, 3),
        "inversions": inversions,
        "total_pairs": total_pairs,
    }


def check_hard_constraints(row: dict[str, Any], prediction: str) -> dict[str, Any]:
    """Run every hard checker this instance is annotated for."""
    if not prediction:
        return {}

    source_text = row["source_text"]
    constraint_lines = row["constraints"]
    results: dict[str, Any] = {}

    for cid in row["constraint_ids"]:
        if cid not in HARD_CONSTRAINT_IDS:
            continue
        if cid == "format_preserve":
            results[cid] = check_format_preserve(
                source_text, prediction,
                src_lang=row["source_lang"], tgt_lang=row["target_lang"],
            )
        elif cid == "layout_break":
            results[cid] = check_layout_preserved(
                source_text, prediction, parse_layout_features(constraint_lines))
        elif cid == "term_compliance":
            terms = parse_term_targets(constraint_lines)
            results[cid] = (check_glossary(prediction, terms) if terms
                            else {"is_valid": True, "note": "no terms to check"})
        elif cid == "syllable_order":
            results[cid] = check_syllable_order(
                prediction, row.get("duration_s", []), row["target_lang"])
        elif cid == "social_preserve":
            results[cid] = check_social_preserve(
                prediction, parse_social_elements(constraint_lines))

    return results