Exemplo n.º 1
0
    def random_resource(self, max_length=130):
        resource = None
        q = UrlResource.find_latest()
        if not q.count():
            raise MissingDataException("no results found")

        for resource in random_query_results(q, 10):
            if len(resource.url) < max_length:
                break
        if not resource:
            raise MissingDataException("no sufficient resources found")
        return resource
Exemplo n.º 2
0
    def select(self, selected, min_length, max_length):
        """
        params:
            selected - list of selected words.
        return:
            a tuple of words
        """
        if not self.__words:
            raise MissingDataException(
                "no satisfactory content has been ingested")
        elif not self.__words_compiled:
            self.compile()

        # select a trailing word via weighted random
        def select_next(current_word):
            word_stats = self.__words_compiled.get(current_word, None)
            next_word = None
            if word_stats:
                # stats were found for this word
                next_word = rrandom.select_weighted_with_replacement(
                    word_stats[1])
            return (next_word, ) if next_word else None

        if not selected:
            # select first word
            next = (rrandom.select_weighted_with_replacement(
                self.__word_weights), )
        else:
            # select next word using last word
            next = select_next(selected[-1])

        return next
Exemplo n.º 3
0
 def speak(self, min_length, max_length):
     def is_length_legit(s):
         s_len = len(s)
         return s_len <= max_length and s_len >= min_length
     candidates = filter(is_length_legit, self.__phrases)
     if not candidates:
         raise MissingDataException("no sentences of appropriate length found")
     return random.choice(candidates).strip()
Exemplo n.º 4
0
 def __random_content_from_query(self,
                                 q,
                                 count=CONTENT_BATCH_SIZE,
                                 minimum_results=MINIMUM_ARTIFACTS):
     """
     returns:
         list of ArtifactContent records
     """
     q_count = q.count()
     if q_count < minimum_results:
         msg = "not enough ArtifactContents found in query (%d < %d)" % \
             (q_count, minimum_results)
         logging.error(msg)
         raise MissingDataException(msg)
     return random_query_results(q, count)
Exemplo n.º 5
0
    def select(self, selected, min_length, max_length):
        """
        params:
            selected - list of selected words.
        return:
            a tuple of words
        """
        if not self.__words:
            raise MissingDataException(
                "no satisfactory content has been ingested")
        elif not self.__words_compiled:
            self.compile()

        # select a trailing word via weighted random
        def select_next(current_pair):
            pair_stats = self.__words_compiled.get(current_pair, None)
            next_word = None
            if pair_stats:
                # ends if we're past the min length and have END as a potential next word
                if (calculate_length(selected) > min_length) and \
                    Symbols.END in pair_stats:
                    next_word = None
                else:
                    # don't end if we're not longer than min_length
                    logging.debug("select_next %s %s" %
                                  (current_pair, str(pair_stats)))

                    # stats were found for this pair
                    # next_word = rrandom.select_weighted_with_replacement(pair_stats[1])
                    next_word = random.choice(pair_stats[1])[0]

            if next_word is Symbols.END: next_word = None
            return (next_word, ) if next_word else None

        # select first pair via weighted random
        if not selected:
            #next = (rrandom.select_weighted_with_replacement(self.__word_weights))
            next = (rrandom.select_weighted_with_replacement(
                self.__heads_compiled))
        else:
            # select using last 2 words as params
            next = select_next((selected[-2], selected[-1]))

        return next
Exemplo n.º 6
0
    def __random_content_for_source(self,
                                    source,
                                    count,
                                    minimum_results=MINIMUM_ARTIFACTS):
        """
        returns:
            list of ArtifactContent records
        """
        if not source:
            raise IllegalArgumentException("source cannot be None")

        logging.debug("__random_content_for_source source:%s" % source)
        content_q = ArtifactContent.all().filter("source =", source)
        content_q_count = content_q.count()
        if content_q_count < minimum_results:
            msg = "not enough ArtifactContents found for ArtifactSource %s (%d < %d)" % \
                (source.name, content_q_count, minimum_results)
            raise MissingDataException(msg)
        return random_query_results(content_q, count)
Exemplo n.º 7
0
 def mix_random_limit_sources(self, source_count, degrade=False):
     """
     params:
         source_count - number of sources to mix
         degrade - if True, mix even if source_count sources cannot be found
     returns:
         ((sources), mixed_content)
     """
     # choose random sources
     source_q = ArtifactSource.all()
     q_count = source_q.count()
     if (q_count < source_count):
         if degrade:
             logging.debug(
                 "mix_random_limit_sources requested %d sources; degrading to %d"
                 % (source_count, q_count))
             source_count = q_count
         else:
             raise MissingDataException("insufficient ArtifactSources found (%d < %d)" % \
                 (q_count, source_count))
     sources = random_query_results(source_q, source_count)
     return self.__random_content_for_sources(sources)