-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathextract_proposed_questions.py
More file actions
executable file
·103 lines (88 loc) · 3.63 KB
/
Copy pathextract_proposed_questions.py
File metadata and controls
executable file
·103 lines (88 loc) · 3.63 KB
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
#!/usr/bin/env python3
"""Documentation omitted."""
import json
import os
import argparse
from pathlib import Path
def extract_questions_from_directory(input_dir, output_file):
"""Documentation omitted."""
input_path = Path(input_dir)
if not input_path.exists():
print(f"Error: directory does not exist: {input_dir}")
return
if not input_path.is_dir():
print(f"Error: path is not a directory: {input_dir}")
return
json_files = list(input_path.glob("*.json"))
if not json_files:
print(f"Warning: no JSON files found in {input_dir}")
return
print(f"Found {len(json_files)} JSON files")
extracted_count = 0
skipped_count = 0
with open(output_file, 'w', encoding='utf-8') as f:
for json_file in sorted(json_files):
try:
with open(json_file, 'r', encoding='utf-8') as jf:
data = json.load(jf)
answer_str = data.get("answer", "")
if not answer_str:
print(f"Warning: {json_file.name} has no answer field, skipping")
skipped_count += 1
continue
try:
answer_json = json.loads(answer_str)
except json.JSONDecodeError as e:
print(f"Warning: answer field in {json_file.name} is not valid JSON, skipping: {e}")
skipped_count += 1
continue
question = answer_json.get("proposed_question", "")
if not question:
print(f"Warning: answer in {json_file.name} has no proposed_question field, skipping")
skipped_count += 1
continue
filename = json_file.name
output_data = {
"question": question,
"answer": "",
"filename": filename
}
f.write(json.dumps(output_data, ensure_ascii=False) + "\n")
extracted_count += 1
except json.JSONDecodeError as e:
print(f"Error: failed to parse JSON file {json_file.name}: {e}")
skipped_count += 1
except Exception as e:
print(f"Error: failed while processing {json_file.name}: {e}")
skipped_count += 1
print("\nDone!")
print(f"Extracted: {extracted_count} files")
print(f"Skipped/failed: {skipped_count} files")
print(f"Output file: {output_file}")
def main():
default_base = Path("./outputs/objective_trajectories/formatted")
refined_accepted_dir = default_base / "refined" / "verifier" / "accepted_trajectories"
default_input_dir = (
refined_accepted_dir
if refined_accepted_dir.exists()
else default_base / "verifier" / "accepted_trajectories"
)
parser = argparse.ArgumentParser(description="Extract proposed questions from accepted objective trajectories.")
parser.add_argument(
"--input-dir",
default=str(default_input_dir),
help="Directory containing accepted trajectory JSON files.",
)
parser.add_argument(
"--output-file",
default="./outputs/objective_trajectories/extracted_questions.jsonl",
help="Output JSONL path.",
)
args = parser.parse_args()
input_dir = args.input_dir
output_file = args.output_file
output_path = Path(output_file)
output_path.parent.mkdir(parents=True, exist_ok=True)
extract_questions_from_directory(input_dir, output_file)
if __name__ == "__main__":
main()