def encode(self, word, mode=<NUM_LIT:1>, lang='<STR_LIT>'):
phonet_hash = Counter()<EOL>alpha_pos = Counter()<EOL>phonet_hash_1 = Counter()<EOL>phonet_hash_2 = Counter()<EOL>def _initialize_phonet(lang):<EOL><INDENT>"""<STR_LIT>"""<EOL>if lang == '<STR_LIT:none>':<EOL><INDENT>_phonet_rules = self._rules_no_lang<EOL><DEDENT>else:<EOL><INDENT>_phonet_rules = self._rules_german<EOL><DEDENT>phonet_hash['<STR_LIT>'] = -<NUM_LIT:1><EOL>for j in {<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>}:<EOL><INDENT>alpha_pos[j] = <NUM_LIT:1><EOL>phonet_hash[j] = -<NUM_LIT:1><EOL><DEDENT>for i, j in enumerate('<STR_LIT>'):<EOL><INDENT>alpha_pos[j] = i + <NUM_LIT:2><EOL>phonet_hash[j] = -<NUM_LIT:1><EOL><DEDENT>for i in range(<NUM_LIT>):<EOL><INDENT>for j in range(<NUM_LIT>):<EOL><INDENT>phonet_hash_1[i, j] = -<NUM_LIT:1><EOL>phonet_hash_2[i, j] = -<NUM_LIT:1><EOL><DEDENT><DEDENT>for i in range(len(_phonet_rules)):<EOL><INDENT>rule = _phonet_rules[i]<EOL>if rule and i % <NUM_LIT:3> == <NUM_LIT:0>:<EOL><INDENT>k = _phonet_rules[i][<NUM_LIT:0>]<EOL>if phonet_hash[k] < <NUM_LIT:0> and (<EOL>_phonet_rules[i + <NUM_LIT:1>] or _phonet_rules[i + <NUM_LIT:2>]<EOL>):<EOL><INDENT>phonet_hash[k] = i<EOL><DEDENT>if k and alpha_pos[k] >= <NUM_LIT:2>:<EOL><INDENT>k = alpha_pos[k]<EOL>j = k - <NUM_LIT:2><EOL>rule = rule[<NUM_LIT:1>:]<EOL>if not rule:<EOL><INDENT>rule = '<STR_LIT:U+0020>'<EOL><DEDENT>elif rule[<NUM_LIT:0>] == '<STR_LIT:(>':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>rule = rule[<NUM_LIT:0>]<EOL><DEDENT>while rule and (rule[<NUM_LIT:0>] != '<STR_LIT:)>'):<EOL><INDENT>k = alpha_pos[rule[<NUM_LIT:0>]]<EOL>if k > <NUM_LIT:0>:<EOL><INDENT>if phonet_hash_1[j, k] < <NUM_LIT:0>:<EOL><INDENT>phonet_hash_1[j, k] = i<EOL>phonet_hash_2[j, k] = i<EOL><DEDENT>if phonet_hash_2[j, k] >= (i - <NUM_LIT:30>):<EOL><INDENT>phonet_hash_2[j, k] = i<EOL><DEDENT>else:<EOL><INDENT>k = -<NUM_LIT:1><EOL><DEDENT><DEDENT>if k <= <NUM_LIT:0>:<EOL><INDENT>if phonet_hash_1[j, <NUM_LIT:0>] < <NUM_LIT:0>:<EOL><INDENT>phonet_hash_1[j, <NUM_LIT:0>] = i<EOL><DEDENT>phonet_hash_2[j, <NUM_LIT:0>] = i<EOL><DEDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>def _phonet(term, mode, lang):<EOL><INDENT>"""<STR_LIT>"""<EOL>if lang == '<STR_LIT:none>':<EOL><INDENT>_phonet_rules = self._rules_no_lang<EOL><DEDENT>else:<EOL><INDENT>_phonet_rules = self._rules_german<EOL><DEDENT>char0 = '<STR_LIT>'<EOL>dest = term<EOL>if not term:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>term_length = len(term)<EOL>src = term.translate(self._upper_trans)<EOL>i = <NUM_LIT:0><EOL>j = <NUM_LIT:0><EOL>zeta = <NUM_LIT:0><EOL>while i < len(src):<EOL><INDENT>char = src[i]<EOL>pos = alpha_pos[char]<EOL>if pos >= <NUM_LIT:2>:<EOL><INDENT>xpos = pos - <NUM_LIT:2><EOL>if i + <NUM_LIT:1> == len(src):<EOL><INDENT>pos = alpha_pos['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>pos = alpha_pos[src[i + <NUM_LIT:1>]]<EOL><DEDENT>start1 = phonet_hash_1[xpos, pos]<EOL>start2 = phonet_hash_1[xpos, <NUM_LIT:0>]<EOL>end1 = phonet_hash_2[xpos, pos]<EOL>end2 = phonet_hash_2[xpos, <NUM_LIT:0>]<EOL>if (start2 >= <NUM_LIT:0>) and ((start1 < <NUM_LIT:0>) or (start2 < start1)):<EOL><INDENT>pos = start1<EOL>start1 = start2<EOL>start2 = pos<EOL>pos = end1<EOL>end1 = end2<EOL>end2 = pos<EOL><DEDENT>if (end1 >= start2) and (start2 >= <NUM_LIT:0>):<EOL><INDENT>if end2 > end1:<EOL><INDENT>end1 = end2<EOL><DEDENT>start2 = -<NUM_LIT:1><EOL>end2 = -<NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>pos = phonet_hash[char]<EOL>start1 = pos<EOL>end1 = <NUM_LIT><EOL>start2 = -<NUM_LIT:1><EOL>end2 = -<NUM_LIT:1><EOL><DEDENT>pos = start1<EOL>zeta0 = <NUM_LIT:0><EOL>if pos >= <NUM_LIT:0>:<EOL><INDENT>while (_phonet_rules[pos] is None) or (<EOL>_phonet_rules[pos][<NUM_LIT:0>] == char<EOL>):<EOL><INDENT>if pos > end1:<EOL><INDENT>if start2 > <NUM_LIT:0>:<EOL><INDENT>pos = start2<EOL>start1 = start2<EOL>start2 = -<NUM_LIT:1><EOL>end1 = end2<EOL>end2 = -<NUM_LIT:1><EOL>continue<EOL><DEDENT>break<EOL><DEDENT>if (_phonet_rules[pos] is None) or (<EOL>_phonet_rules[pos + mode] is None<EOL>):<EOL><INDENT>pos += <NUM_LIT:3><EOL>continue<EOL><DEDENT>matches = <NUM_LIT:1>  <EOL>priority = <NUM_LIT:5>  <EOL>rule = _phonet_rules[pos]<EOL>rule = rule[<NUM_LIT:1>:]<EOL>while (<EOL>rule<EOL>and (len(src) > (i + matches))<EOL>and (src[i + matches] == rule[<NUM_LIT:0>])<EOL>and not rule[<NUM_LIT:0>].isdigit()<EOL>and (rule not in '<STR_LIT>')<EOL>):<EOL><INDENT>matches += <NUM_LIT:1><EOL>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and (rule[<NUM_LIT:0>] == '<STR_LIT:(>'):<EOL><INDENT>if (<EOL>(len(src) > (i + matches))<EOL>and src[i + matches].isalpha()<EOL>and (src[i + matches] in rule[<NUM_LIT:1>:])<EOL>):<EOL><INDENT>matches += <NUM_LIT:1><EOL>while rule and rule[<NUM_LIT:0>] != '<STR_LIT:)>':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT><DEDENT>if rule:<EOL><INDENT>priority0 = ord(rule[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>priority0 = <NUM_LIT:0><EOL><DEDENT>matches0 = matches<EOL>while rule and rule[<NUM_LIT:0>] == '<STR_LIT:->' and matches > <NUM_LIT:1>:<EOL><INDENT>matches -= <NUM_LIT:1><EOL>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and rule[<NUM_LIT:0>] == '<STR_LIT:<>':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and rule[<NUM_LIT:0>].isdigit():<EOL><INDENT>priority = int(rule[<NUM_LIT:0>])<EOL>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and rule[<NUM_LIT:0>:<NUM_LIT:2>] == '<STR_LIT>':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if (<EOL>not rule<EOL>or (<EOL>(rule[<NUM_LIT:0>] == '<STR_LIT>')<EOL>and ((i == <NUM_LIT:0>) or not src[i - <NUM_LIT:1>].isalpha())<EOL>and (<EOL>(rule[<NUM_LIT:1>:<NUM_LIT:2>] != '<STR_LIT:$>')<EOL>or (<EOL>not (<EOL>src[<EOL>i + matches0 : i + matches0 + <NUM_LIT:1><EOL>].isalpha()<EOL>)<EOL>and (<EOL>src[<EOL>i + matches0 : i + matches0 + <NUM_LIT:1><EOL>]<EOL>!= '<STR_LIT:.>'<EOL>)<EOL>)<EOL>)<EOL>)<EOL>or (<EOL>(rule[<NUM_LIT:0>] == '<STR_LIT:$>')<EOL>and (i > <NUM_LIT:0>)<EOL>and src[i - <NUM_LIT:1>].isalpha()<EOL>and (<EOL>(<EOL>not src[<EOL>i + matches0 : i + matches0 + <NUM_LIT:1><EOL>].isalpha()<EOL>)<EOL>and (<EOL>src[i + matches0 : i + matches0 + <NUM_LIT:1>]<EOL>!= '<STR_LIT:.>'<EOL>)<EOL>)<EOL>)<EOL>):<EOL><INDENT>pos0 = -<NUM_LIT:1><EOL>start3 = <NUM_LIT:0><EOL>start4 = <NUM_LIT:0><EOL>end3 = <NUM_LIT:0><EOL>end4 = <NUM_LIT:0><EOL>if (<EOL>(matches > <NUM_LIT:1>)<EOL>and src[i + matches : i + matches + <NUM_LIT:1>]<EOL>and (priority0 != ord('<STR_LIT:->'))<EOL>):<EOL><INDENT>char0 = src[i + matches - <NUM_LIT:1>]<EOL>pos0 = alpha_pos[char0]<EOL>if pos0 >= <NUM_LIT:2> and src[i + matches]:<EOL><INDENT>xpos = pos0 - <NUM_LIT:2><EOL>pos0 = alpha_pos[src[i + matches]]<EOL>start3 = phonet_hash_1[xpos, pos0]<EOL>start4 = phonet_hash_1[xpos, <NUM_LIT:0>]<EOL>end3 = phonet_hash_2[xpos, pos0]<EOL>end4 = phonet_hash_2[xpos, <NUM_LIT:0>]<EOL>if (start4 >= <NUM_LIT:0>) and (<EOL>(start3 < <NUM_LIT:0>) or (start4 < start3)<EOL>):<EOL><INDENT>pos0 = start3<EOL>start3 = start4<EOL>start4 = pos0<EOL>pos0 = end3<EOL>end3 = end4<EOL>end4 = pos0<EOL><DEDENT>if (end3 >= start4) and (start4 >= <NUM_LIT:0>):<EOL><INDENT>if end4 > end3:<EOL><INDENT>end3 = end4<EOL><DEDENT>start4 = -<NUM_LIT:1><EOL>end4 = -<NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>pos0 = phonet_hash[char0]<EOL>start3 = pos0<EOL>end3 = <NUM_LIT><EOL>start4 = -<NUM_LIT:1><EOL>end4 = -<NUM_LIT:1><EOL><DEDENT>pos0 = start3<EOL><DEDENT>if pos0 >= <NUM_LIT:0>:<EOL><INDENT>while (_phonet_rules[pos0] is None) or (<EOL>_phonet_rules[pos0][<NUM_LIT:0>] == char0<EOL>):<EOL><INDENT>if pos0 > end3:<EOL><INDENT>if start4 > <NUM_LIT:0>:<EOL><INDENT>pos0 = start4<EOL>start3 = start4<EOL>start4 = -<NUM_LIT:1><EOL>end3 = end4<EOL>end4 = -<NUM_LIT:1><EOL>continue<EOL><DEDENT>priority0 = -<NUM_LIT:1><EOL>break<EOL><DEDENT>if (_phonet_rules[pos0] is None) or (<EOL>_phonet_rules[pos0 + mode] is None<EOL>):<EOL><INDENT>pos0 += <NUM_LIT:3><EOL>continue<EOL><DEDENT>matches0 = matches<EOL>priority0 = <NUM_LIT:5><EOL>rule = _phonet_rules[pos0]<EOL>rule = rule[<NUM_LIT:1>:]<EOL>while (<EOL>rule<EOL>and (<EOL>src[<EOL>i + matches0 : i + matches0 + <NUM_LIT:1><EOL>]<EOL>== rule[<NUM_LIT:0>]<EOL>)<EOL>and (<EOL>not rule[<NUM_LIT:0>].isdigit()<EOL>or (rule in '<STR_LIT>')<EOL>)<EOL>):<EOL><INDENT>matches0 += <NUM_LIT:1><EOL>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and rule[<NUM_LIT:0>] == '<STR_LIT:(>':<EOL><INDENT>if src[<EOL>i + matches0 : i + matches0 + <NUM_LIT:1><EOL>].isalpha() and (<EOL>src[i + matches0] in rule[<NUM_LIT:1>:]<EOL>):<EOL><INDENT>matches0 += <NUM_LIT:1><EOL>while rule and rule[<NUM_LIT:0>] != '<STR_LIT:)>':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT><DEDENT>while rule and rule[<NUM_LIT:0>] == '<STR_LIT:->':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and rule[<NUM_LIT:0>] == '<STR_LIT:<>':<EOL><INDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if rule and rule[<NUM_LIT:0>].isdigit():<EOL><INDENT>priority0 = int(rule[<NUM_LIT:0>])<EOL>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if (<EOL>not rule<EOL>or<EOL>(<EOL>(rule[<NUM_LIT:0>] == '<STR_LIT:$>')<EOL>and not src[<EOL>i + matches0 : i + matches0 + <NUM_LIT:1><EOL>].isalpha()<EOL>and (<EOL>src[<EOL>i<EOL>+ matches0 : i<EOL>+ matches0<EOL>+ <NUM_LIT:1><EOL>]<EOL>!= '<STR_LIT:.>'<EOL>)<EOL>)<EOL>):<EOL><INDENT>if matches0 == matches:<EOL><INDENT>pos0 += <NUM_LIT:3><EOL>continue<EOL><DEDENT>if priority0 < priority:<EOL><INDENT>pos0 += <NUM_LIT:3><EOL>continue<EOL><DEDENT>break<EOL><DEDENT>pos0 += <NUM_LIT:3><EOL><DEDENT>if (priority0 >= priority) and (<EOL>(_phonet_rules[pos0] is not None)<EOL>and (_phonet_rules[pos0][<NUM_LIT:0>] == char0)<EOL>):<EOL><INDENT>pos += <NUM_LIT:3><EOL>continue<EOL><DEDENT><DEDENT>if _phonet_rules[pos] and (<EOL>'<STR_LIT:<>' in _phonet_rules[pos][<NUM_LIT:1>:]<EOL>):<EOL><INDENT>priority0 = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>priority0 = <NUM_LIT:0><EOL><DEDENT>rule = _phonet_rules[pos + mode]<EOL>if (priority0 == <NUM_LIT:1>) and (zeta == <NUM_LIT:0>):<EOL><INDENT>if (<EOL>(j > <NUM_LIT:0>)<EOL>and rule<EOL>and (<EOL>(dest[j - <NUM_LIT:1>] == char)<EOL>or (dest[j - <NUM_LIT:1>] == rule[<NUM_LIT:0>])<EOL>)<EOL>):<EOL><INDENT>j -= <NUM_LIT:1><EOL><DEDENT>zeta0 = <NUM_LIT:1><EOL>zeta += <NUM_LIT:1><EOL>matches0 = <NUM_LIT:0><EOL>while rule and src[i + matches0]:<EOL><INDENT>src = (<EOL>src[<NUM_LIT:0> : i + matches0]<EOL>+ rule[<NUM_LIT:0>]<EOL>+ src[i + matches0 + <NUM_LIT:1> :]<EOL>)<EOL>matches0 += <NUM_LIT:1><EOL>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if matches0 < matches:<EOL><INDENT>src = (<EOL>src[<NUM_LIT:0> : i + matches0]<EOL>+ src[i + matches :]<EOL>)<EOL><DEDENT>char = src[i]<EOL><DEDENT>else:<EOL><INDENT>i = i + matches - <NUM_LIT:1><EOL>zeta = <NUM_LIT:0><EOL>while len(rule) > <NUM_LIT:1>:<EOL><INDENT>if (j == <NUM_LIT:0>) or (dest[j - <NUM_LIT:1>] != rule[<NUM_LIT:0>]):<EOL><INDENT>dest = (<EOL>dest[<NUM_LIT:0>:j]<EOL>+ rule[<NUM_LIT:0>]<EOL>+ dest[min(len(dest), j + <NUM_LIT:1>) :]<EOL>)<EOL>j += <NUM_LIT:1><EOL><DEDENT>rule = rule[<NUM_LIT:1>:]<EOL><DEDENT>if not rule:<EOL><INDENT>rule = '<STR_LIT>'<EOL>char = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>char = rule[<NUM_LIT:0>]<EOL><DEDENT>if (<EOL>_phonet_rules[pos]<EOL>and '<STR_LIT>' in _phonet_rules[pos][<NUM_LIT:1>:]<EOL>):<EOL><INDENT>if char:<EOL><INDENT>dest = (<EOL>dest[<NUM_LIT:0>:j]<EOL>+ char<EOL>+ dest[min(len(dest), j + <NUM_LIT:1>) :]<EOL>)<EOL>j += <NUM_LIT:1><EOL><DEDENT>src = src[i + <NUM_LIT:1> :]<EOL>i = <NUM_LIT:0><EOL>zeta0 = <NUM_LIT:1><EOL><DEDENT><DEDENT>break<EOL><DEDENT>pos += <NUM_LIT:3><EOL>if pos > end1 and start2 > <NUM_LIT:0>:<EOL><INDENT>pos = start2<EOL>start1 = start2<EOL>end1 = end2<EOL>start2 = -<NUM_LIT:1><EOL>end2 = -<NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>if zeta0 == <NUM_LIT:0>:<EOL><INDENT>if char and ((j == <NUM_LIT:0>) or (dest[j - <NUM_LIT:1>] != char)):<EOL><INDENT>dest = (<EOL>dest[<NUM_LIT:0>:j] + char + dest[min(j + <NUM_LIT:1>, term_length) :]<EOL>)<EOL>j += <NUM_LIT:1><EOL><DEDENT>i += <NUM_LIT:1><EOL>zeta = <NUM_LIT:0><EOL><DEDENT><DEDENT>dest = dest[<NUM_LIT:0>:j]<EOL>return dest<EOL><DEDENT>_initialize_phonet(lang)<EOL>word = unicode_normalize('<STR_LIT>', text_type(word))<EOL>return _phonet(word, mode, lang)<EOL>
Return the phonet code for a word.

        Parameters
        ----------
        word : str
            The word to transform
        mode : int
            The ponet variant to employ (1 or 2)
        lang : str
            ``de`` (default) for German, ``none`` for no language

        Returns
        -------
        str
            The phonet value

        Examples
        --------
        >>> pe = Phonet()
        >>> pe.encode('Christopher')
        'KRISTOFA'
        >>> pe.encode('Niall')
        'NIAL'
        >>> pe.encode('Smith')
        'SMIT'
        >>> pe.encode('Schmidt')
        'SHMIT'

        >>> pe.encode('Christopher', mode=2)
        'KRIZTUFA'
        >>> pe.encode('Niall', mode=2)
        'NIAL'
        >>> pe.encode('Smith', mode=2)
        'ZNIT'
        >>> pe.encode('Schmidt', mode=2)
        'ZNIT'

        >>> pe.encode('Christopher', lang='none')
        'CHRISTOPHER'
        >>> pe.encode('Niall', lang='none')
        'NIAL'
        >>> pe.encode('Smith', lang='none')
        'SMITH'
        >>> pe.encode('Schmidt', lang='none')
        'SCHMIDT'