<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> def fuzzy_search ( user_input , corpus ) : <EOL> candidates = [ ] <EOL> for word in corpus : <EOL> current_score = calculate_score ( user_input , word ) <EOL> if current_score > <NUM_LIT:0> : <EOL> candidates . append ( ( word , current_score ) ) <EOL> return [ c [ <NUM_LIT:0> ] for c in sorted ( candidates , key = lambda x : x [ <NUM_LIT:1> ] , reverse = True ) ] <EOL> def calculate_score ( search_string , word ) : <EOL> """<STR_LIT>""" <EOL> if len ( search_string ) > len ( word ) : <EOL> return <NUM_LIT:0> <EOL> original_word = word <EOL> score = <NUM_LIT:1> <EOL> search_index = <NUM_LIT:0> <EOL> while True : <EOL> scale = <NUM_LIT:1.0> <EOL> search_char = search_string [ search_index ] <EOL> i = word . find ( search_char ) <EOL> if i < <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> if i > <NUM_LIT:0> and word [ i - <NUM_LIT:1> ] == '<STR_LIT:->' : <EOL> scale = <NUM_LIT> <EOL> else : <EOL> scale = <NUM_LIT:1> - ( i / float ( len ( word ) ) ) <EOL> score *= scale <EOL> word = word [ i + <NUM_LIT:1> : ] <EOL> search_index += <NUM_LIT:1> <EOL> if search_index >= len ( search_string ) : <EOL> break <EOL> completion_scale = <NUM_LIT:1> - ( len ( word ) / float ( len ( original_word ) ) ) <EOL> score *= completion_scale <EOL> return score </s>
