Exemplo n.º 1
0
def question(request, lang, model_components=5, model_iterations=100):
    ''' Displays a 20Q question'''
    if not 'answers' in request.session:
        request.session['answers'] = []
    answer_list = request.session['answers']

    if request.method == 'POST':
        ans = request.POST['answer']
        relation = request.POST['relation']
        concept = request.POST['concept']
        slot = int(request.POST['slot'])
        guess = bool(int(request.POST['guess']))

        # Special case: if we guessed a concept AND the answer was yes,
        # then the game is over.
        if guess and ans[0] == 'Y':
            return show_results(request, Concept.get_raw(concept, en).canonical_name, answer_list)

        answer_list.append( (relation, concept, slot, ans, guess) )


    yes_features = [to_feature(r, c, s) for (r, c, s, ans, g) in answer_list if ans[0] == 'Y']
    no_features = [to_feature(r, c, s) for (r, c, s, ans, g) in answer_list if
    ans[0] == 'N' or ans[0] == 'D']
    maybe_features = [to_feature(r, c, s) for (r, c, s, ans, g) in answer_list
    if ans[0] == 'M']+['IsA/way', 'UsedFor/sex', 'AtLocation/theatr',
    'AtLocation/home', 'human/AtLocation']

    prev_questions = [(make_question(r, c, s), ans, g, Concept.get_raw(c, en).canonical_name)
                      for (r, c, s, ans, g) in answer_list]

    global mixture_model_cache
    questioner = StatelessReconstructedMixtureModelQuestioner(min_observations=20,
                                                              model_components=model_components,
                                                              model_iterations=model_iterations,
                                                              recompute=False,
                                                              memcache=mixture_model_cache,
                                                              neg_weight_assertions= True,
                                                              beta_prior_probability=0.04,
                                                              beta_prior_weight=0.2)

    if model_components == 5: threshold=.5
    else: threshold=.8
    feature = questioner.get_question_stateless(yes_features, no_features,
    maybe_features, threshold=threshold)
    likelyrank = questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)
    '''    try:
        feature = questioner.get_question_stateless(yes_features, no_features, maybe_features, threshold=.5)
        likelyrank = questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)
    except NotCachedError, OutOfQuestions:
        # Fall back on AnalogySpace's ad-hoc categories
        questioner = StatelessAnalogySpaceQuestioner([], recompute=False, memcache=mixture_model_cache)
        feature = questioner.get_question_stateless(yes_features, no_features, maybe_features)
        likelyrank = questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)'''
    relation, concept, slot = from_feature(feature)
    likely = [Concept.get_raw(x[0], en).canonical_name for x in likelyrank[:10]]
    quest = make_question(relation, concept, slot)
    canonical_concept = Concept.get_raw(concept, en).canonical_name

    aspace_questioner = StatelessAnalogySpaceQuestioner([], recompute=False, memcache=mixture_model_cache)
    aspace_rank = aspace_questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)

    guess = 0
    global mm_guess_concept, mm_question_limit
    if len(yes_features) + len(no_features) in mm_guess_concept:
        # Start guessing concepts
        guess = 1
        relation = 'IsA'
        previous_guesses = set([c for (r, c, s, ans, g) in answer_list if g])
        concept = [c[0] for c in likelyrank if c[0] not in previous_guesses][0]
        canonical_concept = Concept.get_raw(concept, en).canonical_name
        slot = 1

    give_up = False
    if len(yes_features) + len(no_features) > mm_question_limit:
        give_up = True

    return respond_with('question.html', request,
                        {'guess' : guess,
                         'question': quest,
                         'relation': relation,
                         'concept': concept,
                         'canonical_concept' : canonical_concept,
                         'slot': slot,
                         'prev': prev_questions,
                         'likely': likely,
                         'guess_frame' : 'Is it ',
                         'give_up' : give_up})
Exemplo n.º 2
0
def question(request, lang, model_components=5, model_iterations=100):
    """ Displays a 20Q question"""
    if not "answers" in request.session:
        request.session["answers"] = []
    answer_list = request.session["answers"]

    if request.method == "POST":
        ans = request.POST["answer"]
        relation = request.POST["relation"]
        concept = request.POST["concept"]
        slot = int(request.POST["slot"])
        guess = bool(int(request.POST["guess"]))

        # Special case: if we guessed a concept AND the answer was yes,
        # then the game is over.
        if guess and ans[0] == "Y":
            return show_results(request, Concept.get_raw(concept, en).canonical_name, answer_list)

        answer_list.append((relation, concept, slot, ans, guess))

    yes_features = [to_feature(r, c, s) for (r, c, s, ans, g) in answer_list if ans[0] == "Y"]
    no_features = [to_feature(r, c, s) for (r, c, s, ans, g) in answer_list if ans[0] == "N" or ans[0] == "D"]
    maybe_features = [to_feature(r, c, s) for (r, c, s, ans, g) in answer_list if ans[0] == "M"] + [
        "IsA/way",
        "UsedFor/sex",
        "AtLocation/theatr",
        "AtLocation/home",
        "human/AtLocation",
    ]

    prev_questions = [
        (make_question(r, c, s), ans, g, Concept.get_raw(c, en).canonical_name) for (r, c, s, ans, g) in answer_list
    ]

    global mixture_model_cache
    questioner = StatelessReconstructedMixtureModelQuestioner(
        min_observations=20,
        model_components=model_components,
        model_iterations=model_iterations,
        recompute=False,
        memcache=mixture_model_cache,
        neg_weight_assertions=True,
        beta_prior_probability=0.04,
        beta_prior_weight=0.2,
    )

    if model_components == 5:
        threshold = 0.5
    else:
        threshold = 0.8
    feature = questioner.get_question_stateless(yes_features, no_features, maybe_features, threshold=threshold)
    likelyrank = questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)
    """    try:
        feature = questioner.get_question_stateless(yes_features, no_features, maybe_features, threshold=.5)
        likelyrank = questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)
    except NotCachedError, OutOfQuestions:
        # Fall back on AnalogySpace's ad-hoc categories
        questioner = StatelessAnalogySpaceQuestioner([], recompute=False, memcache=mixture_model_cache)
        feature = questioner.get_question_stateless(yes_features, no_features, maybe_features)
        likelyrank = questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)"""
    relation, concept, slot = from_feature(feature)
    likely = [Concept.get_raw(x[0], en).canonical_name for x in likelyrank[:10]]
    quest = make_question(relation, concept, slot)
    canonical_concept = Concept.get_raw(concept, en).canonical_name

    aspace_questioner = StatelessAnalogySpaceQuestioner([], recompute=False, memcache=mixture_model_cache)
    aspace_rank = aspace_questioner.get_likely_concepts(20, yes_features, no_features, maybe_features)

    guess = 0
    global mm_guess_concept, mm_question_limit
    if len(yes_features) + len(no_features) in mm_guess_concept:
        # Start guessing concepts
        guess = 1
        relation = "IsA"
        previous_guesses = set([c for (r, c, s, ans, g) in answer_list if g])
        concept = [c[0] for c in likelyrank if c[0] not in previous_guesses][0]
        canonical_concept = Concept.get_raw(concept, en).canonical_name
        slot = 1

    give_up = False
    if len(yes_features) + len(no_features) > mm_question_limit:
        give_up = True

    return respond_with(
        "question.html",
        request,
        {
            "guess": guess,
            "question": quest,
            "relation": relation,
            "concept": concept,
            "canonical_concept": canonical_concept,
            "slot": slot,
            "prev": prev_questions,
            "likely": likely,
            "guess_frame": "Is it ",
            "give_up": give_up,
        },
    )