def viterbi_segment(text, P):
<EOL>n = len(text)<EOL>words = ['<STR_LIT>'] + list(text)<EOL>best = [<NUM_LIT:1.0>] + [<NUM_LIT:0.0>] * n<EOL>for i in range(n+<NUM_LIT:1>):<EOL><INDENT>for j in range(<NUM_LIT:0>, i):<EOL><INDENT>w = text[j:i]<EOL>if P[w] * best[i - len(w)] >= best[i]:<EOL><INDENT>best[i] = P[w] * best[i - len(w)]<EOL>words[i] = w<EOL><DEDENT><DEDENT><DEDENT>sequence = []; i = len(words)-<NUM_LIT:1><EOL>while i > <NUM_LIT:0>:<EOL><INDENT>sequence[<NUM_LIT:0>:<NUM_LIT:0>] = [words[i]]<EOL>i = i - len(words[i])<EOL><DEDENT>return sequence, best[-<NUM_LIT:1>]<EOL>
Find the best segmentation of the string of characters, given the
    UnigramTextModel P.