import os import re def generate_index(): characters = [f.replace('.md', '') for f in os.listdir('characters') if f.endswith('.md')] index = {} for chapter_file in sorted(os.listdir('chapters')): if not chapter_file.endswith('.md'): continue with open(os.path.join('chapters', chapter_file), 'r') as f: content = f.read() for char in characters: if re.search(r'\b' + char + r'\b', content, re.IGNORECASE): if char not in index: index[char] = [] index[char].append(chapter_file) with open('KNOWLEDGE_GRAPH.md', 'w') as f: f.write('# Knowledge Graph\n\n') for char, chapters in index.items(): f.write(f'## {char}\n') for chap in chapters: f.write(f'- [{chap}](chapters/{chap})\n') f.write('\n') if __name__ == "__main__": generate_index()