File size: 2,201 Bytes
43e7de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import re
from typing import Any, Dict, List

_ARTICLE_RE = re.compile(r"điều\s+(\d+)", re.IGNORECASE)
_CLAUSE_RE = re.compile(r"khoản\s+(\d+)", re.IGNORECASE)
_POINT_RE = re.compile(r"điểm\s+([a-zđ])", re.IGNORECASE)
_DOC_NUMBER_RE = re.compile(r"\b\d{1,4}/\d{4}/[A-ZĐ\-]+\b", re.IGNORECASE)


def _norm_space(text: Any) -> str:
    return " ".join(str(text or "").split()).strip()


def _norm_key(text: Any) -> str:
    return _norm_space(text).lower()


def _source_values(sources: List[Dict[str, Any]], key: str) -> set[str]:
    aliases = {
        "doc_number": ("doc_number",),
        "article": ("article",),
        "clause": ("clause",),
        "point": ("point",),
    }
    values: set[str] = set()
    for source in sources:
        md = dict(source.get("metadata") or {})
        merged = {**md, **source}
        for candidate_key in aliases.get(key, (key,)):
            value = merged.get(candidate_key)
            if value not in (None, ""):
                values.add(_norm_key(value))
    return values


def _refs_from_answer(answer: str) -> Dict[str, List[str]]:
    text = _norm_space(answer)
    return {
        "article": [f"Điều {m.group(1)}" for m in _ARTICLE_RE.finditer(text)],
        "clause": [f"Khoản {m.group(1)}" for m in _CLAUSE_RE.finditer(text)],
        "point": [f"Điểm {m.group(1).lower()}" for m in _POINT_RE.finditer(text)],
        "doc_number": [m.group(0).upper() for m in _DOC_NUMBER_RE.finditer(text)],
    }


def validate_citations(answer: str, sources: List[Dict[str, Any]]) -> Dict[str, Any]:
    refs = _refs_from_answer(answer)
    unsupported: List[Dict[str, str]] = []
    warnings: List[str] = []

    for key, labels in refs.items():
        source_values = _source_values(sources, key)
        for label in labels:
            if _norm_key(label) not in source_values:
                unsupported.append({"type": key, "reference": label})

    if unsupported:
        warnings.append("Answer references legal citations not present in source metadata.")

    return {
        "valid": not unsupported,
        "warnings": warnings,
        "unsupported_references": unsupported,
    }