|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Extract Glossary Terms from glossary.json |
| 4 | +
|
| 5 | +This script reads the glossary JSON file and converts it to GLOSSARY_DATA format |
| 6 | +for use in glossary_linker.py |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python3 extract_from_json.py website/static/glossary/glossary.json |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def extract_from_json(json_file_path): |
| 18 | + """Extract glossary terms from JSON file.""" |
| 19 | + |
| 20 | + with open(json_file_path, 'r', encoding='utf-8') as f: |
| 21 | + glossary_items = json.load(f) |
| 22 | + |
| 23 | + terms = [] |
| 24 | + |
| 25 | + for item in glossary_items: |
| 26 | + term_name = item.get('term', '').strip() |
| 27 | + abbreviation = item.get('abbreviation', '').strip() |
| 28 | + |
| 29 | + if not term_name: |
| 30 | + continue |
| 31 | + |
| 32 | + terms.append({ |
| 33 | + 'term': term_name, |
| 34 | + 'abbreviation': abbreviation if abbreviation else None |
| 35 | + }) |
| 36 | + |
| 37 | + return terms |
| 38 | + |
| 39 | + |
| 40 | +def generate_python_code(terms): |
| 41 | + """Generate Python code for GLOSSARY_DATA.""" |
| 42 | + |
| 43 | + lines = ['GLOSSARY_DATA = ['] |
| 44 | + |
| 45 | + for term in terms: |
| 46 | + abbr = f'"{term["abbreviation"]}"' if term['abbreviation'] else 'None' |
| 47 | + lines.append(f' {{"term": "{term["term"]}", "abbreviation": {abbr}}},') |
| 48 | + |
| 49 | + lines.append(']') |
| 50 | + |
| 51 | + return '\n'.join(lines) |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + if len(sys.argv) < 2: |
| 56 | + print("Usage: python3 extract_from_json.py <path_to_glossary.json>") |
| 57 | + print("\nExample:") |
| 58 | + print(" python3 extract_from_json.py website/static/glossary/glossary.json") |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + json_path = Path(sys.argv[1]) |
| 62 | + |
| 63 | + if not json_path.exists(): |
| 64 | + print(f"Error: File not found: {json_path}") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + print(f"Extracting terms from: {json_path}") |
| 68 | + print() |
| 69 | + |
| 70 | + terms = extract_from_json(json_path) |
| 71 | + |
| 72 | + print(f"Found {len(terms)} glossary terms:") |
| 73 | + print() |
| 74 | + |
| 75 | + # Show preview |
| 76 | + for i, term in enumerate(terms[:10], 1): |
| 77 | + abbr_str = f" ({term['abbreviation']})" if term['abbreviation'] else "" |
| 78 | + print(f" {i}. {term['term']}{abbr_str}") |
| 79 | + |
| 80 | + if len(terms) > 10: |
| 81 | + print(f" ... and {len(terms) - 10} more") |
| 82 | + |
| 83 | + print() |
| 84 | + print("="*70) |
| 85 | + print("PYTHON CODE TO ADD TO glossary_linker.py:") |
| 86 | + print("="*70) |
| 87 | + print() |
| 88 | + print(generate_python_code(terms)) |
| 89 | + print() |
| 90 | + |
| 91 | + # Also save to a JSON file for reference |
| 92 | + output_json = json_path.parent / 'glossary_terms_extracted.json' |
| 93 | + with open(output_json, 'w', encoding='utf-8') as f: |
| 94 | + json.dump(terms, f, indent=2, ensure_ascii=False) |
| 95 | + |
| 96 | + print("="*70) |
| 97 | + print(f"Also saved to: {output_json}") |
| 98 | + print("="*70) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + main() |
0 commit comments