def search(text, prev='<s>'):
    if text == '':
        return 0.0, []

def candidates():
    for prefix, suffix in divide(text):
        prefix_score = log10(score(prefix, prev))
pair = suffix, prefix
if pair not in memo:
    memo[pair] = search(suffix, prefix)
suffix_score, suffix_words = memo[pair]
yield prefix_score + suffix_score, [prefix] + suffix_words
return max(candidates())
result_score, result_words = search(clean(text))
return result_words
Word segmentation using dynamic programming