tosinamuda commited on
Commit
42f4347
·
verified ·
1 Parent(s): 148fcd2

Upload combine_data.py

Browse files
Files changed (1) hide show
  1. scripts/combine_data.py +78 -0
scripts/combine_data.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Combine the country data files into one JSONL file.
2
+
3
+ Each line is one record. Every record has the union of all input columns,
4
+ in order of first appearance, plus `source_file`, which names the file the
5
+ record came from. Columns a file does not have are empty strings. The list
6
+ columns, stored as JSON arrays in the CSVs, become real lists; an empty cell
7
+ becomes []. All other text is copied unchanged.
8
+
9
+ python3 scripts/combine_data.py
10
+ """
11
+
12
+ import csv
13
+ import json
14
+ import sys
15
+ from pathlib import Path
16
+
17
+ ROOT = Path(__file__).resolve().parent.parent
18
+ INPUTS = [
19
+ "data/kenya/af-scam-ke-v0.1.csv",
20
+ "data/nigeria/af-scam-ng-screenshots-v0.1.csv",
21
+ "data/nigeria/af-scam-ng-contributor-R-F8JM-W6U-v0.1.csv",
22
+ ]
23
+ LIST_COLUMNS = ["tactics", "requested_action", "risk_indicators", "language_tags"]
24
+ OUTPUT = "data/afriscam-v0.1.jsonl"
25
+
26
+
27
+ def read_rows(relative_path):
28
+ with open(ROOT / relative_path, encoding="utf-8", newline="") as f:
29
+ reader = csv.DictReader(f)
30
+ return reader.fieldnames, list(reader)
31
+
32
+
33
+ def parse_list(oid, column, value):
34
+ if not value.strip():
35
+ return []
36
+ parsed = json.loads(value)
37
+ if not isinstance(parsed, list):
38
+ raise ValueError(f"{oid}: {column} is not a JSON array")
39
+ return parsed
40
+
41
+
42
+ def combine(inputs):
43
+ columns, rows, seen = [], [], set()
44
+ for path in inputs:
45
+ header, file_rows = read_rows(path)
46
+ columns += [c for c in header if c not in columns]
47
+ for row in file_rows:
48
+ oid = row["observation_id"]
49
+ if oid in seen:
50
+ raise ValueError(f"{oid} appears in more than one file")
51
+ seen.add(oid)
52
+ rows.append((path, row))
53
+ return columns + ["source_file"], rows
54
+
55
+
56
+ def to_record(columns, path, row):
57
+ record = {c: row.get(c, "") for c in columns}
58
+ record["source_file"] = path
59
+ for column in LIST_COLUMNS:
60
+ record[column] = parse_list(row["observation_id"], column, record[column])
61
+ return record
62
+
63
+
64
+ def main():
65
+ try:
66
+ columns, rows = combine(INPUTS)
67
+ records = [to_record(columns, path, row) for path, row in rows]
68
+ except (ValueError, json.JSONDecodeError) as e:
69
+ print(f"Not combined: {e}", file=sys.stderr)
70
+ return 1
71
+ lines = [json.dumps(r, ensure_ascii=False) + "\n" for r in records]
72
+ (ROOT / OUTPUT).write_text("".join(lines), encoding="utf-8")
73
+ print(f"Wrote {len(records)} records and {len(columns)} columns to {OUTPUT}")
74
+ return 0
75
+
76
+
77
+ if __name__ == "__main__":
78
+ sys.exit(main())