def build_trie(pattern_filename, pattern_format, encoding, on_word_boundaries):
boundaries = on_word_boundaries<EOL>if pattern_format == '<STR_LIT>' or not on_word_boundaries:<EOL><INDENT>tsv, boundaries = detect_pattern_format(pattern_filename, encoding,<EOL>on_word_boundaries)<EOL><DEDENT>if pattern_format == '<STR_LIT>':<EOL><INDENT>if tsv:<EOL><INDENT>pattern_format = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>pattern_format = '<STR_LIT>'<EOL><DEDENT><DEDENT>trie = fsed.ahocorasick.AhoCorasickTrie()<EOL>num_candidates = <NUM_LIT:0><EOL>with open_file(pattern_filename) as pattern_file:<EOL><INDENT>for lineno, line in enumerate(pattern_file):<EOL><INDENT>line = line.decode(encoding).rstrip('<STR_LIT:\n>')<EOL>if not line.strip():<EOL><INDENT>continue<EOL><DEDENT>if pattern_format == '<STR_LIT>':<EOL><INDENT>fields = line.split('<STR_LIT:\t>')<EOL>if len(fields) != <NUM_LIT:2>:<EOL><INDENT>LOGGER.warning(('<STR_LIT>'<EOL>'<STR_LIT>').format(lineno, line))<EOL>continue<EOL><DEDENT>before, after = fields<EOL><DEDENT>elif pattern_format == '<STR_LIT>':<EOL><INDENT>before = after = None<EOL>line = line.lstrip()<EOL>if line[<NUM_LIT:0>] == '<STR_LIT:s>':<EOL><INDENT>delim = line[<NUM_LIT:1>]<EOL>if delim in '<STR_LIT>':<EOL><INDENT>delim = '<STR_LIT:\\>' + delim<EOL><DEDENT>fields = re.split(r'<STR_LIT>'.format(delim), line)<EOL>if len(fields) == <NUM_LIT:4>:<EOL><INDENT>before, after = fields[<NUM_LIT:1>], fields[<NUM_LIT:2>]<EOL>before = re.sub(r'<STR_LIT>'.format(delim), delim, before)<EOL>after = re.sub(r'<STR_LIT>'.format(delim), delim, after)<EOL><DEDENT><DEDENT>if before is None or after is None:<EOL><INDENT>LOGGER.warning(('<STR_LIT>'<EOL>'<STR_LIT>').format(lineno, line))<EOL>continue<EOL><DEDENT><DEDENT>num_candidates += <NUM_LIT:1><EOL>if on_word_boundaries and before != before.strip():<EOL><INDENT>LOGGER.warning(('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>').format(lineno, line))<EOL><DEDENT>before = sub_escapes(before)<EOL>after = sub_escapes(after)<EOL>if boundaries:<EOL><INDENT>before = fsed.ahocorasick.boundary_transform(before, on_word_boundaries)<EOL><DEDENT>trie[before] = after<EOL><DEDENT><DEDENT>LOGGER.info('<STR_LIT>'.format(num_candidates,<EOL>pattern_filename))<EOL>return trie, boundaries<EOL>
Constructs a finite state machine for performing string rewriting.

Arguments:
- `pattern_filename`:
- `pattern_format`:
- `encoding`:
- `on_word_boundaries`: