File size: 2,880 Bytes
8032ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import re
from pathlib import Path
from typing import Dict, Any, List

EMAIL_RE = re.compile(r"(?i)[a-z0-9._%+-]+\s*@\s*[a-z0-9.-]+\s*\.\s*[a-z]{2,}")
ORCID_RE = re.compile(r"(?i)\b(?:0\s*0\s*0\s*[0-3])\s*-\s*(?:\d\s*){4}\s*-\s*(?:\d\s*){4}\s*-\s*(?:\d\s*){3}[\dX]\b")
PHONE_RE = re.compile(r"(?i)\btel\.?\s*:?\s*(?:\d\s*){9,}")
PESEL_RE = re.compile(r"(?i)\bpesel\b\s*:?\s*(?:\d[\s-]*){11}\b")
NIP_RE = re.compile(r"(?i)\bnip\b\s*:?\s*(?:\d[\s-]*){10}\b")
REGON_RE = re.compile(r"(?i)\bregon\b\s*:?\s*(?:(?:\d[\s-]*){9}|(?:\d[\s-]*){14})\b")
AFFILIATION_RE = re.compile(r"(?i)\b(uniwersytet|wydział|instytut|katedra|mgr inż|dr hab|prof\.)\b")
NAV_HEADER_RE = re.compile(r"(?i)\bstudia bas\b|\bnr \d+\(\d+\)")
HYPHEN_RE = re.compile(r"\b\w+-\s+[a-ząćęłńóśźż]\w+\b")

REQUIRED_KEYS = {"id", "text", "source", "created", "license", "author", "doi", "source_url", "issue_url", "issue", "license_url"}

def get_context(text, match):
    if not match: return ""
    s = max(0, match.start() - 40)
    e = min(len(text), match.end() + 40)
    snippet = text[s:e]
    redacted = snippet[:match.start()-s] + "[REDACTED]" + snippet[match.end()-s:]
    return redacted.replace('\n', ' ')

def audit_record(record: Dict[str, Any], seen_ids: set, seen_texts: set) -> Dict[str, List[str]]:
    issues = {}
    text = record.get("text", "")
    author = record.get("author", "")
    doc_id = record.get("id", "")

    # Missing metadata
    if not all(k in record for k in REQUIRED_KEYS):
        issues["malformed_metadata"] = ["Missing keys"]

    if not author.strip():
        issues["empty_author"] = ["Empty author field"]

    if doc_id in seen_ids:
        issues["duplicate_id"] = [f"ID: {doc_id}"]
    seen_ids.add(doc_id)

    if text in seen_texts:
        issues["duplicate_text"] = ["Duplicate text block"]
    seen_texts.add(text)

    for issue_name, regex in [
        ("residual_email", EMAIL_RE),
        ("orcid_found", ORCID_RE),
        ("phone_number", PHONE_RE),
        ("pesel", PESEL_RE),
        ("nip", NIP_RE),
        ("regon", REGON_RE),
        ("affiliation_block", AFFILIATION_RE),
        ("navigation_header", NAV_HEADER_RE),
        ("suspicious_hyphenation", HYPHEN_RE),
    ]:
        matches = list(regex.finditer(text))
        if matches:
            issues[issue_name] = [get_context(text, m) for m in matches[:2]]

    return issues

def audit_corpus(path: Path) -> Dict[str, Dict[str, List[str]]]:
    results = {}
    seen_ids = set()
    seen_texts = set()

    with open(path, 'r', encoding='utf-8') as f:
        for line in f:
            if not line.strip():
                continue
            record = json.loads(line)
            doc_id = record.get("id", "UNKNOWN")
            issues = audit_record(record, seen_ids, seen_texts)
            if issues:
                results[doc_id] = issues

    return results