diff --git a/scripts/index_generator.py b/scripts/index_generator.py new file mode 100644 index 0000000..510fb73 --- /dev/null +++ b/scripts/index_generator.py @@ -0,0 +1,27 @@ + +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()