Exemple #1
0
    def generate(self):
        interface, _ = Interface.objects.get_or_create(name='synthetic')

        random_gen = RandomWeighted()
        random_gen.seed(self.seed)

        # Data related to word use
        word_data = self.configuration.wordmeaningdata_set.filter(region=self.informer.region)
        n_word_data = len(word_data)
        if n_word_data:
            for _ in range(self.n_use_data):
                w = word_data[random_gen.randint(0, n_word_data-1)]  # TODO: El extremo derecho está incluído o no?
                dato = WordUse(interface=interface, informer=self.informer)
                dato.meaning = w.meaning
                if random_gen.random() < self.randomness:
                    dato.value = random_gen.choice([WordUse.USES.ok, WordUse.USES.prefer_other, WordUse.USES.unrecognized])
                    if dato.value == WordUse.USES.prefer_other:
                        # TODO: Cuando al azar prefiero otra palabra... ¿prefiero otra cualquiera al azar o una del subset?
                        # dato.alternative = random_gen.choice(list(w.alternatedata_set.all()))
                        word = random_gen.choice(Word.objects.all())
                        alternative_meaning = Meaning(word=word, definition=dato.meaning.definition, informer=self.informer)
                        alternative_meaning.save()
                        dato.alternative = alternative_meaning
                else:
                    dato.value = random_gen.weighted_choice([(WordUse.USES.ok, w.ok),
                                                             (WordUse.USES.prefer_other, 1-w.ok-w.unknown),
                                                             (WordUse.USES.unrecognized, w.unknown)])
                    if dato.value == WordUse.USES.prefer_other:
                        word = random_gen.weighted_choice([(it.word, it.percentage) for it in w.alternatedata_set.all()])
                        alternative_meaning = Meaning(word=word, definition=dato.meaning.definition, informer=self.informer)
                        alternative_meaning.save()
                        dato.alternative = alternative_meaning
                dato.save()

        # Data related to coarsity
        coarse_data = self.configuration.wordcoarsedata_set.filter(region=self.informer.region)
        n_word_data = len(coarse_data)
        if n_word_data:
            for _ in range(self.n_coarse_data):
                w = coarse_data[random_gen.randint(0, n_word_data-1)]  # TODO: El extremo derecho está incluído o no?
                dato = CoarseWord(interface=interface, informer=self.informer)
                dato.word = w.word
                if random_gen.random() < self.randomness:
                    # At random
                    dato.value = random_gen.random() < 0.5  # TODO: ¿mayor o mayor-igual?
                else:
                    # Use data
                    dato.value = random_gen.random() < w.coarse
                dato.save()

        self.generated = True
        self.save()
Exemple #2
0
    def form_valid(self, form, time_elapsed=None):
        meaning = Meaning.objects.get(pk=form.cleaned_data['item'])
        button = form.cleaned_data['button']
        word_alternate = WordAlternate(meaning=meaning)
        if button == 0:
            value = form.cleaned_data['value']
            if not value:
                # Jump to another item but keep track of error
                log.error("WordAlternateRun form error, user '{}' left value field empty.".format(self.request.user))
                return super(WordAlternateRun, self).form_valid(form=None)

            alternate, _ = Word.objects.get_or_create(word=value)
            alternate_meaning = Meaning(word=alternate, definition=meaning.definition)
            alternate_meaning.informer = self.request.user.as_informer()
            alternate_meaning.excluded = True  # TODO: May I accept directly new words?
            alternate_meaning.save()
            word_alternate.value = alternate_meaning
        word_alternate.informer = self.request.user.as_informer()
        word_alternate.interface = self.interface
        word_alternate.elapsed_time = time_elapsed
        word_alternate.save()
        return super(WordAlternateRun, self).form_valid(form=None)