in prompts/openstax/build_openstax_prompts.py [0:0]
def parse_chapter(chapter, level=0, trail=[]):
"""Parse each chapter recursively and take into account sections"""
trail_current = trail + [chapter["title"]]
chapter_info = {
"title": chapter["title"],
"level": level,
"trail": trail_current,
"abstract": chapter.get("abstract", ""),
"sections": [],
"sub_chapters": [],
}
if chapter.get("sections"):
for section in chapter.get("sections"):
chapter_info["sections"].append(
{
"title": section["title"],
"content": section.get("paragraph", ""),
"trail": trail_current,
"abstract": section.get("abstract", ""),
}
)
# Handle sub-chapters recursively
if chapter.get("chapters"):
for sub_chapter in chapter.get("chapters"):
chapter_info["sub_chapters"].append(
parse_chapter(sub_chapter, level + 1, trail_current)
)
return chapter_info