def fill_resp(p, matches, active_clue, do_list, stack): for i in range(len(matches)): word = matches[i] poss = True; xings = p.clue_xings(active_clue) wd_stack = [] wd_do_list = "" for j in range(len(word)): pattern = xings[j][2][0:xings[j][0] - 1] + word[j:j+1].upper() + xings[j][2][xings[j][0]:] if pattern.find('?') == -1: continue try: patscore_obj = Pattern.objects.get(pattern=pattern) patscore = patscore_obj.count except ObjectDoesNotExist: patscore = Answer.objects.extra(where=['answer LIKE "%s"' % pattern.replace('?','_')]).count() p_obj = Pattern(pattern=pattern, count=patscore) p_obj.save() if patscore == 0: poss = False break if patscore == 1: xwd = Answer.objects.extra(where=['answer LIKE "%s"' % pattern.replace('?','_')]).all()[0].answer.lower() if not wd_do_list == "": wd_do_list += "/" wd_do_list += "%s&%s" % (xings[j][3], xwd) wd_stack.append("%s&%s&%s" % (xings[j][3], pattern, "")) if poss: if not do_list == "": do_list += "/" do_list += "%d-%s&%s" % (active_clue.num, active_clue.dir, word) stack.append('%d-%s&%s&%s' % (active_clue.num, active_clue.dir, active_clue.ans, '&'.join(matches[i+1:]))) stack += wd_stack if not wd_do_list == "": do_list += '/' + wd_do_list resp = '%s:%s' % (do_list, ','.join(stack)) return resp else: continue try: while True: last = stack.pop().split('&') if len(last) > 2 and (not last[2] == ''): break if not do_list == "": do_list += "/" do_list += "%s&%s" % (last[0], last[1]) if not do_list == '': do_list += '/' do_list += "%s&%s" % (last[0], last[2]) stack.append('%s&%s&%s' % (last[0], last[1], '&'.join(last[3:]))) resp = "%s:%s" % (do_list, ','.join(stack)) return resp except Exception as e: return ""
def fill(request): resp = '' word_matches = [] p = Puzzle.fromPOST(request.POST) stackstr = request.POST["fill-stack"] if stackstr == "": stack = p.initial_stack() else: stack = stackstr.split(',') guide = request.POST['fill-guide'] if guide == 'on': clue = request.POST['active'] (n1,ad,pos) = clue.split('-') active_clue = p.clue_from_str(n1, ad) else: active_clue = p.fill_clue(stack) if active_clue: pat = active_clue.ans.lower() q = Answer.objects.extra(where=['answer LIKE "%s"' % pat.replace('?','_')]) q = q.order_by('count').reverse() for r in q.all(): word_matches.append(r.answer.lower()) if pat == '?' * len(pat): scores={} xings = p.clue_xings(active_clue) for word in word_matches: wordscore = 1.0 for j in range(len(word)): pattern = xings[j][2][0:xings[j][0] - 1] + word[j:j+1].upper() + xings[j][2][xings[j][0]:] try: patscore_obj = Pattern.objects.get(pattern=pattern) patscore = patscore_obj.count except ObjectDoesNotExist: patscore = Answer.objects.extra(where=['answer LIKE "%s"' % pattern.replace('?','_')]).count() p_obj = Pattern(pattern=pattern, count=patscore) p_obj.save() wordscore *= patscore if wordscore == 0: break if wordscore > 0: scores[word] = wordscore word_matches = [] ct = 0 for wd in sorted(scores.keys(), lambda x,y: cmp(scores[y], scores[x])): word_matches.append(wd) ct += 1 if ct > 100: break if len(word_matches) > 0: resp = fill_resp(p, word_matches, active_clue, "", stack) if len(word_matches) == 0 or resp == None: int_hash = {} cross_hash = p.cross_hash(active_clue) for i in range(len(pat)): if not pat[i:i+1] == '?': cross = cross_hash["%d-%s-%d" % (active_clue.num, active_clue.dir, i+1)].split('-') int_hash['-'.join(cross[0:2])] = 1 do_list = "" got_int = False while True: last = None try: last = stack.pop().split('&') except Exception: pass if last == None or last == '' or len(last) < 3: resp = "unfillable" break if len(last) >= 3: (num, ad) = last[0].split('-') clue = p.clue_from_str(num,ad) clue.ans = last[1] last_xings = p.clue_xings(clue) for i in range(clue.length): xing_pos = last_xings[i][0] if not clue.ans[i:i+1] == last_xings[i][2][xing_pos-1:xing_pos]: (num, ad) = last_xings[i][3].split('-') xing_clue = p.clue_from_str(num,ad) xing_clue.ans = xing_clue.ans[0:xing_pos-1] + clue.ans[i:i+1] + xing_clue.ans[xing_pos:] if not do_list == "": do_list += "/" do_list += "%s&%s" % (last[0], last[1]) if (last[2] == '') or ((not got_int) and (not int_hash.has_key(last[0]))): if int_hash.has_key(last[0]): got_int = True continue; else: resp = fill_resp(p, last[2:], clue, do_list, stack) break return HttpResponse(resp)