MCPcopy Create free account
hub / github.com/BYVoid/OpenCC / compile_dict

Function compile_dict

scripts/compile_to_inline_config.py:111–141  ·  view source on GitHub ↗
(dict_def: Dict[str, Any], dict_dir: Path)

Source from the content-addressed store, hash-verified

109
110
111def compile_dict(dict_def: Dict[str, Any], dict_dir: Path) -> Dict[str, Any]:
112 if dict_def["type"] == "group":
113 compiled_children = [compile_dict(sub_def, dict_dir) for sub_def in dict_def["dicts"]]
114 match_policy = dict_def.get("match_policy", "short_circuit")
115
116 if match_policy == "union":
117 # Union semantics: longest prefix wins regardless of child order.
118 # A flat merged inline dict has the same semantics because LeafMatcher
119 # always returns the longest match; first-occurrence wins for same key.
120 # Nested union groups are also absorbed since union(union(...)) = union(...).
121 if all(is_union_mergeable(child) for child in compiled_children):
122 merged: Dict[str, str] = {}
123 for child in compiled_children:
124 collect_union_entries(child, merged)
125 return {"type": "inline", "entries": sort_dict_keys(merged)}
126
127 result: Dict[str, Any] = {"type": "group"}
128 if "match_policy" in dict_def:
129 result["match_policy"] = dict_def["match_policy"]
130 result["dicts"] = compiled_children
131 return result
132
133 if dict_def["type"] == "inline":
134 entries = dict_def["entries"]
135 else:
136 entries = inline_entries_for_file_dict(dict_def, dict_dir)
137
138 return {
139 "type": "inline",
140 "entries": sort_dict_keys(entries),
141 }
142
143def sort_dict_keys(d: Dict[str, str]) -> Dict[str, str]:
144 return {k: d[k] for k in sorted(d.keys())}

Callers 1

compile_configFunction · 0.85

Calls 5

is_union_mergeableFunction · 0.85
collect_union_entriesFunction · 0.85
sort_dict_keysFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected