- self.replacements = datrie.Trie(rules.replacement_charset)
- for full, repl in rules.replacements:
- self.replacements[full] = repl
+ # Intermediate reorder by source. Also compute required character set.
+ immediate = defaultdict(list)
+ chars = set()
+ for variant in rules.replacements:
+ if variant.source[-1] == ' ' and variant.replacement[-1] == ' ':
+ replstr = variant.replacement[:-1]
+ else:
+ replstr = variant.replacement
+ immediate[variant.source].append(replstr)
+ chars.update(variant.source)
+ # Then copy to datrie
+ self.replacements = datrie.Trie(''.join(chars))
+ for src, repllist in immediate.items():
+ self.replacements[src] = repllist