Beispiel #1
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      
Beispiel #2
0
 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
Beispiel #3
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
Beispiel #4
0
 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
Beispiel #5
0
 def should_act(self):
     now = datetime.utcnow()
     # TODO: optimize range search
     logging.debug("time: %s" % now)
     should_act = False
     for hour_range, actions in self._times_to_weights.iteritems():
         if now.hour in hour_range:
             logging.debug("actions: %s" % actions)
             should_act = rrandom.select_weighted_with_replacement(actions)
             break
     return should_act
Beispiel #6
0
 def should_act(self):
     now = datetime.utcnow()
     # TODO: optimize range search
     logging.debug("time: %s" % now)
     should_act = False
     for hour_range, actions in self._times_to_weights.iteritems():
         if now.hour in hour_range:
             logging.debug("actions: %s" % actions)
             should_act = rrandom.select_weighted_with_replacement(actions)
             break
     return should_act
Beispiel #7
0
    def _speak(self, min_length, max_length, **kw):
        parts = []

        # select first word via weighted random

        prev = None
        current = rrandom.select_weighted_with_replacement(word_weights)

        parts.append(current)
        parts_len = len(current)

        # select trailing words via weighted random until we've reached the max length
        def select(current_word):
            return rrandom.select_weighted_with_replacement(
                self.__words_compiled[current_word][1])

        while parts_len < max_length and current is not None:
            prev = current
            current = select(current)

            if current is None:
                # None means the possible end of the sentence

                # breaks if we selected an endpoint and we're past the min length
                if parts_len > min_length:
                    break

                # I'm not past the min length, so I only give up after trying to find another word 5 times.
                attempt = 0
                while current is None and attempt < 5:
                    current = select(prev)
                    attempt += 1

            # if I wasn't able to find a word, I quit.
            # TODO: consider throwing an exception
            if current is None:
                break

            # breaks if next word will push us over the limit
            if parts_len + len(current) >= max_length:
                break

            parts.append(current)

            # len(parts) at end is for spaces
            parts_len = sum([len(w) for w in parts]) + len(parts)

        return " ".join(parts)
Beispiel #8
0
 def _speak(self, min_length, max_length, **kw):
     parts = []
     
     # select first word via weighted random
     
     prev = None
     current = rrandom.select_weighted_with_replacement(word_weights)
     
     parts.append(current)
     parts_len = len(current)
     
     # select trailing words via weighted random until we've reached the max length
     def select(current_word):
         return rrandom.select_weighted_with_replacement(self.__words_compiled[current_word][1])
         
     while parts_len < max_length and current is not None:
         prev = current
         current = select(current)
                     
         if current is None:
             # None means the possible end of the sentence
             
             # breaks if we selected an endpoint and we're past the min length
             if parts_len > min_length:
                 break
                 
             # I'm not past the min length, so I only give up after trying to find another word 5 times.
             attempt = 0
             while current is None and attempt < 5:
                 current = select(prev)
                 attempt += 1
         
         # if I wasn't able to find a word, I quit.
         # TODO: consider throwing an exception
         if current is None:
             break
             
         # breaks if next word will push us over the limit
         if parts_len + len(current) >= max_length:
             break
             
         parts.append(current)
         
         # len(parts) at end is for spaces
         parts_len = sum([len(w) for w in parts]) + len(parts)
         
     return " ".join(parts)
Beispiel #9
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
Beispiel #10
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      
Beispiel #11
0
 def select_action(self):
     return rrandom.select_weighted_with_replacement(self._actions_weighted)
Beispiel #12
0
def new_random_speaker():
    return new_speaker(select_weighted_with_replacement(_speakers_weighted))
Beispiel #13
0
def new_random_speaker():
    return new_speaker(select_weighted_with_replacement(_speakers_weighted))
Beispiel #14
0
 def select_action(self):
     return rrandom.select_weighted_with_replacement(self._actions_weighted)
Beispiel #15
0
 def select(current_word):
     return rrandom.select_weighted_with_replacement(self.__words_compiled[current_word][1])
Beispiel #16
0
 def select(current_word):
     return rrandom.select_weighted_with_replacement(
         self.__words_compiled[current_word][1])