def get_blank_answers(clue:Clue,limit=10,words_only=True): """Takes in a fill-in-the-blank clue and returns a list of possible answers from Google""" if clue not in google_cache.keys(): # quote wrap and google q = '"{}"'.format(clue.get_description().lower()) results = " ".join(i for i in search_google(q)) # regex search only for the words immediately before and after the blank q_split = q.split(" ") q_around_blank = " ".join([q_split[q_split.index("*")-1],"*",q_split[q_split.index("*")+1]]) regex = q_around_blank.replace("*",".{{{}}}".format(clue.get_length())).strip('"') r = re.compile(regex) matches = re.findall(r,results) words = [] for match in matches: for word in match.split(" "): if word.lower() not in regex.split(" "): words.append(word) scores = score_words(words) ret = [Guess(clue, word, score) for word, score in scores] google_cache[clue] = ret return ret[:limit] else: return google_cache[clue][:limit]
def get_answers(clue: Clue, limit=10): """Takes in a Clue object and returns a list of words (optionally associated with scores) which are potential answers to the given clue.""" params = {} length = clue.get_length() params["ml"] = clue.description params["sp"] = "?" * clue.length results = [] for item in request(params)[:limit]: guess = Guess(clue, item["word"], item["score"]) results.append(guess) return results