Spaces:
Sleeping
Sleeping
File size: 1,805 Bytes
6710fbe | 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 | import json
import os
def extract_questions():
# Attempt to find the correct input file.
# The user specifically asked for 'financial_kb_200.json', but
# 'financial_kb_2000.json' was found in the project.
# We will prioritize 'financial_kb_2000.json' as it exists.
input_file_path = os.path.join(os.path.dirname(__file__), 'financial_kb_2000.json')
output_file_path = os.path.join(os.path.dirname(__file__), 'kb_questions.txt')
if not os.path.exists(input_file_path):
print(f"File {input_file_path} not found. Trying local path...")
input_file_path = 'src/data/financial_kb_2000.json'
output_file_path = 'src/data/kb_questions.txt'
if not os.path.exists(input_file_path):
print(f"Error: {input_file_path} not found.")
return
try:
with open(input_file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Handle both list of dicts or a single dict (if the structure varies)
if isinstance(data, list):
questions = [item.get('question') for item in data if isinstance(item, dict) and 'question' in item]
elif isinstance(data, dict):
# If the JSON is a single object with a questions list or something similar
questions = [data.get('question')] if 'question' in data else []
else:
questions = []
# Remove None values
questions = [q for q in questions if q]
with open(output_file_path, 'w', encoding='utf-8') as f:
for q in questions:
f.write(f"{q}\n")
print(f"Successfully extracted {len(questions)} questions to {output_file_path}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
extract_questions()
|