def string_partial_matching(alternatives, inp, ret_index=True):
 if ((not alternatives) or (not inp)):
  return []
 matches = defaultdict(list)
 inp_words = inp.lower().split()
 for (altindex, alt) in enumerate(alternatives):
  alt_words = alt.lower().split()
  last_index = 0
  score = 0
  for inp_word in inp_words:
   submatch = [(last_index + alt_num) for (alt_num, alt_word) in enumerate(alt_words[last_index:]) if alt_word.startswith(inp_word)]
   if submatch:
    last_index = (min(submatch) + 1)
    score += 1
   else:
    score = 0
    break
  if score:
   if ret_index:
    matches[score].append(altindex)
   else:
    matches[score].append(alt)
 if matches:
  return matches[max(matches)]
 return []

def string_partial_matching ( alternatives , inp , ret_index = True ) : if ( ( not alternatives ) or ( not inp ) ) : return [ ] matches = defaultdict ( list ) inp_words = inp . lower ( ) . split ( ) for ( altindex , alt ) in enumerate ( alternatives ) : alt_words = alt . lower ( ) . split ( ) last_index = 0 score = 0 for inp_word in inp_words : submatch = [ ( last_index + alt_num ) for ( alt_num , alt_word ) in enumerate ( alt_words [ last_index : ] ) if alt_word . startswith ( inp_word ) ] if submatch : last_index = ( min ( submatch ) + 1 ) score += 1 else : score = 0 break if score : if ret_index : matches [ score ] . append ( altindex ) else : matches [ score ] . append ( alt ) if matches : return matches [ max ( matches ) ] return [ ]
partially matches a string based on a list of alternatives .