FinanceEducationAssistant / src /data /ExtractQuestions.py
Fade0510's picture
Cleaned up code before reimplementing semantic cache
6710fbe
Raw
History Blame Contribute Delete
1.81 kB
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()