def highlight(s): '''Returns a string of '^' characters highlighting any words in s.''' result = [' '] * len(s) for i in range(len(s)): for j in range(i, min(len(s) + 1, i + MAX_WORD_LEN)): score = dictionary.score(s[i:j].lower()) if score > 0 and j - i + score > 7: # ignore short rare words for k in range(i, j): result[k] = '^' return ''.join(result)
def highlight(s): """Returns a string of '^' characters highlighting any words in s.""" result = [" "] * len(s) for i in range(len(s)): for j in range(i, min(len(s) + 1, i + MAX_WORD_LEN)): score = dictionary.score(s[i:j].lower()) if score > 0 and j - i + score > 7: # ignore short rare words for k in range(i, j): result[k] = "^" return "".join(result)
def find_word(word): '''Looks up a word and returns a dictionary multiplier if there is a match, or 0 if not found. Also handles wildcards.''' i = word.find('?') if i == -1: return DICT_MULTIPLIER[dictionary.score(word)] best_result = 0 for c in alphabet: new_word = word[:i] + c + word[i+1:] result = find_word(new_word) if result == 1: return result if result and (result < best_result or best_result == 0): best_result = result return best_result
def find_word(word): '''Looks up a word and returns a dictionary multiplier if there is a match, or 0 if not found. Also handles wildcards.''' i = word.find('?') if i == -1: return DICT_MULTIPLIER[dictionary.score(word)] best_result = 0 for c in alphabet: new_word = word[:i] + c + word[i + 1:] result = find_word(new_word) if result == 1: return result if result and (result < best_result or best_result == 0): best_result = result return best_result
MIN_WORD_SCORE = 1 # find all words words = [] for r in range(len(puz)): for c in range(len(puz[r])): for dr, dc in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]: r1, c1 = r, c word = '' best_word = None best_word_score = MIN_WORD_SCORE - 0.0001 while r1 >= 0 and c1 >= 0 and r1 < len(puz) and c1 < len(puz[r1]): word += puz[r1][c1].lower() score = dictionary.score(word) if score > 0: score = len(word) - 3 + score * 0.5 if word in given_words: for i in range(len(word)): puz_blanked[r + dr * i][c + dc * i] = '`' puz_filled[r + dr * i][c + dc * i] = word[i].upper() elif score > best_word_score: best_word_score = score best_word = word r1 += dr c1 += dc if best_word: s = 'row %d col %d' % (r + 1, c + 1) if dr == -1: s += ' up' if dr == 1: s += ' down' if dc == -1: s += ' left'
given_words = set(map(lambda w: w.lower(), given_words.split())) MIN_WORD_SCORE = 1 # find all words words = [] for r in range(len(puz)): for c in range(len(puz[r])): for dr,dc in [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]: r1,c1 = r,c word = '' best_word = None best_word_score = MIN_WORD_SCORE - 0.0001 while r1 >= 0 and c1 >= 0 and r1 < len(puz) and c1 < len(puz[r1]): word += puz[r1][c1].lower() score = dictionary.score(word) if score > 0: score = len(word)-3 + score*0.5 if word in given_words: for i in range(len(word)): puz_blanked[r+dr*i][c+dc*i] = '`' puz_filled[r+dr*i][c+dc*i] = word[i].upper() elif score > best_word_score: best_word_score = score best_word = word r1 += dr c1 += dc if best_word: s = 'row %d col %d' % (r+1,c+1) if dr == -1: s += ' up' if dr == 1: s += ' down' if dc == -1: s += ' left'