File size: 1,407 Bytes
3af75d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import json

input_path = "cemig_resumo.jsonl"
output_path = "cemig_resumo_v0.jsonl"
TARGET = "Informação não encontrada no contexto fornecido."

def bad(t):
    return not isinstance(t, str) or not t.strip() or t.strip() == TARGET

def first_pair(conv):
    if not isinstance(conv, list):
        return None
    u = None
    for i, m in enumerate(conv):
        if isinstance(m, dict) and m.get("role") == "user":
            u = (i, m)
            break
    if not u:
        return None
    for m in conv[u[0] + 1:]:
        if isinstance(m, dict) and m.get("role") == "assistant":
            return u[1], m
    return None

total = dropped = 0
with open(input_path, encoding="utf-8") as fin, open(output_path, "w", encoding="utf-8") as fout:
    for line in fin:
        if not line.strip():
            continue
        total += 1
        try:
            obj = json.loads(line)
        except Exception:
            dropped += 1
            continue
        pair = first_pair(obj.get("conversation"))
        if not pair:
            dropped += 1
            continue
        u, a = pair
        if bad(u.get("content", "")) or bad(a.get("content", "")):
            dropped += 1
            continue
        obj["conversation"] = [u, a]
        fout.write(json.dumps(obj, ensure_ascii=False) + "\n")

lost = (dropped / total * 100) if total else 0.0
print(f"{lost:.2f}%")