build_taxonomy.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Rebuild the runtime whitelist from the final UI tags, never legacy names."""
  2. import hashlib
  3. import json
  4. from pathlib import Path
  5. from openpyxl import load_workbook
  6. def build_taxonomy(source):
  7. source = Path(source)
  8. workbook = load_workbook(source, read_only=True, data_only=True)
  9. try:
  10. sheet = workbook['Sheet1']
  11. rows = iter(sheet.values)
  12. header = next(rows)
  13. category_index = header.index('TAG_CATEGORY')
  14. tag_index = header.index('最终使用TAG')
  15. categories = {}
  16. count = 0
  17. for row in rows:
  18. if not any(value is not None for value in row):
  19. continue
  20. category, tag = row[category_index], row[tag_index]
  21. if not all(isinstance(value, str) and value.strip() == value and value
  22. for value in (category, tag)):
  23. raise ValueError('invalid_final_tag_row')
  24. tags = categories.setdefault(category, [])
  25. if tag not in tags:
  26. tags.append(tag)
  27. count += 1
  28. return {'source': source.as_posix(),
  29. 'sha256': hashlib.sha256(source.read_bytes()).hexdigest(),
  30. 'sheet': 'Sheet1', 'tag_column': '最终使用TAG',
  31. 'row_count': count, 'categories': categories}
  32. finally:
  33. workbook.close()
  34. if __name__ == '__main__':
  35. snapshot = build_taxonomy(Path('docs/法人标签表.xlsx'))
  36. Path(__file__).with_name('taxonomy.json').write_text(
  37. json.dumps(snapshot, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')