import os
import unicodedata
SCRIPTS_PATH = os.path.join( os.path.dirname(os.path.abspath(file)), "unicode_scripts.txt")
END_CODEPOINT = 0xE0FFF # full cover of non private use code points, excluding surrogates
def unicode_script_map(filename=SCRIPTS_PATH) -> list[dict[str, str|None]]:
"""
Load Unicode script and category data from a file.
Returns:
A dictionary mapping codepoint (int) to a dict with 'script' and 'category' keys
"""
char_info: list[dict[str,str|None]] = [{'script': None, 'category': unicodedata.category(chr(i))} for i in range(END_CODEPOINT+1)]
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
# Skip comments and empty lines
line = line.strip()
if not line or line.startswith('#'):
continue
# Parse 0000..001F ; Common # Cc [32] <control-0000>..<control-001F>
range_str, semicol, script, hash, category, *_ = line.split()
assert semicol == ';' and hash == '#', f"Unexpected format in line: {line}"
# Handle single codepoint or range
if '..' in range_str:
start_str, end_str = range_str.split('..')
start, end = int(start_str, 16), int(end_str, 16)
else:
start = end = int(range_str, 16)
# Add each codepoint in the range to the result dictionary
for cp in range(start, end + 1):
assert char_info[cp]['category'][0] == category[0], f"Category mismatch for codepoint {cp}: {char_info[cp]['category']} vs {category}"
char_info[cp]['script'] = script
return char_info