<commit_before># -*- coding: utf-8 -*-
from geography.models import Answer, Place, Value
from random import choice
from math import floor
import logging
import pprint

LOGGER = logging.getLogger(__name__)


class Question():
    options = []

    def __init__(self, place, number_of_options, map_place, ab_values):
        self.place = place
        self.map_place = map_place
        self.ab_values = ab_values
        if number_of_options > 1:
            self.qtype = QuestionType(
                choice([Answer.FIND_ON_MAP, Answer.PICK_NAME]),
                self.place.type,
                number_of_options)
        else:
            self.qtype = QuestionType(Answer.FIND_ON_MAP, self.place.type, 0)
        if number_of_options > 1:
            self.options = self.get_options(self.qtype.number_of_options)

    def to_serializable(self):
        ret = self.qtype.to_serializable()
        ret['asked_code'] = self.place.code
        ret['map_code'] = self.map_place.place.code
        ret['place'] = self.place.name
        ret['ab_values'] = [v.value for v in self.ab_values]
        if (self.options != []):
            ret["options"] = self.options
        return ret

    def get_options(self, n):
        ps = [self.place]
        ps += self.place.confusing_places(self.map_place, n - 1)
        options = [p.to_serializable() for p in ps]
        options.sort(key=lambda tup: tup["name"])
        return options


class QuestionService:

    def __init__(self, user, map_place, ab_env, target_probability=0.75, history_length=10):
        self.user = user
        self.map_place = map_place
        self.target_probability = target_probability
        self.history_length = history_length
        self.ab_env = ab_env

    def get_questions(self, n, place_types):
        target_probability = self.adjust_target_probability()
        candidates = Place.objects.get_places_to_ask(
            self.user,
            self.map_place,
            target_probability,
            n,
            place_types,
            self.ab_env)
        LOGGER.debug(
            "user %s, question candidates with predicted probability (target %s) for map %s are:\n %s",
            str(self.user),
            target_probability,
            str(self.map_place),
            pprint.pformat(map(lambda (place, raw): raw, candidates)))
        candidates = map(
            lambda (place, raw): (place, self.number_of_options(raw['predicted_probability'], 0.75, raw['number_of_answers'])),
            candidates)
        LOGGER.debug(
            "user %s, question candidates with number of options for map %s are %s",
            str(self.user),
            str(self.map_place),
            str(candidates))
        return [
            Question(
                place,
                options,
                self.map_place,
                self.ab_env.get_affecting_values(Place.AB_REASON_RECOMMENDATION)).to_serializable()
            for (place, options) in candidates]

    def adjust_target_probability(self):
        success_rate = Answer.objects.get_success_rate(self.user, self.history_length)<commit_msg>remove some intensive logging<commit_after># -*- coding: utf-8 -*-
from geography.models import Answer, Place, Value
from random import choice
from math import floor
import logging

LOGGER = logging.getLogger(__name__)


class Question():
    options = []

    def __init__(self, place, number_of_options, map_place, ab_values):
        self.place = place
        self.map_place = map_place
        self.ab_values = ab_values
        if number_of_options > 1:
            self.qtype = QuestionType(
                choice([Answer.FIND_ON_MAP, Answer.PICK_NAME]),
                self.place.type,
                number_of_options)
        else:
            self.qtype = QuestionType(Answer.FIND_ON_MAP, self.place.type, 0)
        if number_of_options > 1:
            self.options = self.get_options(self.qtype.number_of_options)

    def to_serializable(self):
        ret = self.qtype.to_serializable()
        ret['asked_code'] = self.place.code
        ret['map_code'] = self.map_place.place.code
        ret['place'] = self.place.name
        ret['ab_values'] = [v.value for v in self.ab_values]
        if (self.options != []):
            ret["options"] = self.options
        return ret

    def get_options(self, n):
        ps = [self.place]
        ps += self.place.confusing_places(self.map_place, n - 1)
        options = [p.to_serializable() for p in ps]
        options.sort(key=lambda tup: tup["name"])
        return options


class QuestionService:

    def __init__(self, user, map_place, ab_env, target_probability=0.75, history_length=10):
        self.user = user
        self.map_place = map_place
        self.target_probability = target_probability
        self.history_length = history_length
        self.ab_env = ab_env

    def get_questions(self, n, place_types):
        target_probability = self.adjust_target_probability()
        candidates = Place.objects.get_places_to_ask(
            self.user,
            self.map_place,
            target_probability,
            n,
            place_types,
            self.ab_env)
        candidates = map(
            lambda (place, raw): (place, self.number_of_options(raw['predicted_probability'], 0.75, raw['number_of_answers'])),
            candidates)
        LOGGER.debug(
            "user %s, question candidates with number of options for map %s are %s",
            str(self.user),
            str(self.map_place),
            str(candidates))
        return [
            Question(
                place,
                options,
                self.map_place,
                self.ab_env.get_affecting_values(Place.AB_REASON_RECOMMENDATION)).to_serializable()
            for (place, options) in candidates]

    def adjust_target_probability(self):
        success_rate = Answer.objects.get_success_rate(self.user, self.history_length)
{'fraction_non_alphanumeric': 0.09922637066935755, 'fraction_numerical': 0.004372687521022536, 'mean_word_length': 2.211123110151188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '97733', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1741, 'n_words': 373}