def select(self, selected, min_length, max_length): if not selected: # nothing selected, so choosing start node node = self.__graph.find_node_by_key(Symbols.START) else: # words have been selected, finding node for last selected word node = self.__graph.find_node_by_key(selected[len(selected)-1]) iterator = node.randomized_iterator() next_node = iterator.next() next_word = next_node.key # calculates size of current mixture selected.append(next_word) length = calculate_length(selected) if length >= max_length: if next_word is Symbols.END: # returning None if I have found an end node result = None else: # fails since I have not found a valid end node raise TextException("could not achieve valid ending: %s" % self.__join(selected)) elif length >= min_length and next_node.has(Symbols.END): # I am past the min length and I can end. so let's end. pass else: result = (next_word,) return result
def select(self, selected, min_length, max_length): if not selected: # nothing selected, so choosing start node node = self.__graph.find_node_by_key(Symbols.START) else: # words have been selected, finding node for last selected word node = self.__graph.find_node_by_key(selected[len(selected) - 1]) iterator = node.randomized_iterator() next_node = iterator.next() next_word = next_node.key # calculates size of current mixture selected.append(next_word) length = calculate_length(selected) if length >= max_length: if next_word is Symbols.END: # returning None if I have found an end node result = None else: # fails since I have not found a valid end node raise TextException("could not achieve valid ending: %s" % self.__join(selected)) elif length >= min_length and next_node.has(Symbols.END): # I am past the min length and I can end. so let's end. pass else: result = (next_word, ) return result
def _speak(self, min_length, max_length): start_node = self.__graph.find_node_by_key(Symbols.START) if not start_node: raise IllegalStateException("Node '%s' not found" % Symbols.START) iterator = start_node.randomized_iterator() # initial cut will not allow duplicate words words = [] for n, can_end in iterator: logging.debug("word: %s" % n) length = calculate_length(words) # if word has been used as an end and I am past min lenth, break # if (n.key is Symbols.END) and (length >= min_length): # logging.debug("found end after min_length; breaking") # break if can_end and (length >= min_length): logging.debug("found end after min_length; breaking") break # breaks if we have reached max length if length >= max_length: raise TextException("could not achieve valid ending: %s" % self.__join(words)) # TODO: backtrack and try to find ending words.append(n.key) return self.__join(words)
def speak(self, min_length, max_length): iterator = SimpleTreeIterator(self.__tree) words = [] current_node = None while iterator.has_next() and calculate_length(words) < max_length: current_node = iterator.next() words.append(current_node.key) # has last word been used to end a phrase? if not, backtrack if not current_node.has("end"): iterator.previous() return " ".join(words)
def speak(self, min_length, max_length): iterator = SimpleTreeIterator(self.__tree) words = [] current_node = None while iterator.has_next() and calculate_length(words) < max_length: current_node = iterator.next() words.append(current_node.key) # has last word been used to end a phrase? if not, backtrack if not current_node.has("end"): iterator.previous(); return " ".join(words)
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
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
def char_count(self): return calculate_length([w for w in self.__word_iterator()])