def dictogram_dictlist(self): """ Converts the list in word_dict to dictionary by calling Dictogram on the values. """ for key, value in self.word_dict.items(): # if self.word_dict.get(key) is not None: # print(self.word_dict.get(key)) self.word_dict[key] = dictogram.Dictogram(value)
def gen_markov(words, order=1): pairs = make_pairs(words.split(" "), order) markov_dict = {} for word1, word2 in pairs: if word1 in markov_dict: markov_dict[word1].add_count(word2) else: markov_dict[word1] = dictogram.Dictogram([word2]) return markov_dict
def m_chain_one(text_list): markov_dict = {} for index in range(len(text_list) - 1): window = text_list[index] if index in markov_dict: markov_dict[window].add_count([text_list[index + 1]]) else: markov_dict[window] = dictogram.Dictogram([text_list[index + 1]]) return markov_dict
def order_mchain(order, text_list): markov_dict = {} for index in range(len(text_list) - order): window = tuple(text_list[index: index + order]) if window in markov_dict: markov_dict[window].add_count([text_list[index + order]]) else: markov_dict[window] = dictogram.Dictogram([text_list[index + order]]) return markov_dict
def markov_path(token_list, order): markov_map = {} starting_items = [] for n in range(order): starting_items.append(token_list[n]) state_tracker = queue.Queue(size=order, items=starting_items) state = tuple(state_tracker.items()) markov_map[state] = dictogram.Dictogram() for index in range(order, len(token_list)): new_token = token_list[index] state_tracker.enqueue(new_token) new_state = tuple(state_tracker.items()) if new_state not in markov_map: markov_map[new_state] = dictogram.Dictogram() markov_map[state].add_count(new_token) state = new_state return markov_map
def m_chain_one(text_list): markov_dict = {} # for each word in list, key is word and value is dictogram for index in range(len(text_list) - 1): window = text_list[index] if index in markov_dict: markov_dict[window].add_count([text_list[index + 1]]) else: markov_dict[window] = dictogram.Dictogram([text_list[index + 1]]) # return dictionary return markov_dict
def new_function(self, words, order): for i in range(len(words) - order): if tuple(words[i:i + order]) not in self.keys(): self[tuple(words[i:i + order])] = [] self.types += 1 self[tuple(words[i:i + order])].append( tuple(words[i + 1:i + order + 1])) self.tokens += 1 for key in self.keys(): self[key] = dictogram.Dictogram(self[key])
def markov(list_of_words): """ Input: A list of tokens Return: A sentence made by the list provided using first order Markov chain. """ dict_word = {} word_list = [] words = list_of_words #for each key, create a list holiding # values that comes after it on the original list for i in range(len(words) - 1): if words[i] not in dict_word.keys(): word_list.append(words[i + 1]) dict_word[words[i]] = word_list else: dict_word[words[i]].append(words[i + 1]) word_list = [] # print(dict_word) #make a nested dictionary using dictogram class nested_dict = {} for key, value in dict_word.items(): dict_hist = dictogram.Dictogram(dict_word.get(key)) nested_dict[key] = dict_hist # print(nested_dict) #generate sentence sentence_list = [] first_word = random.choice(list(nested_dict.keys())) # print("first word: "+first_word) sentence_list.append(first_word) # print("first word inside: " + str(nested_dict[first_word])) dict_inside = nested_dict[first_word] # print('weighed sample: ' + dict_inside.sample()) count = 10 while count > 0: dict_inside = nested_dict[first_word] next_word = dict_inside.sample() sentence_list.append(next_word) first_word = next_word count -= 1 # print('Sentence list: ' + str(sentence_list)) # print('Markov Sentence: ' + ' '.join(sentence_list)+'.') # print((' '.join(sentence_list)+'.').capitalize()) return (' '.join(sentence_list) + '.').capitalize()
def order_mchain(order, text_list): markov_dict = {} # for each word in list, key is word and value is dictogram for index in range(len(text_list) - order): # text_list[index] should be our word from list window = tuple(text_list[index: index + order]) # check if window is stored in the dictionary already if window in markov_dict: # if it is, then append it to the existing histogram markov_dict[window].add_count([text_list[index + order]]) else: # if not, create new entry with window as key and dictogram as value markov_dict[window] = dictogram.Dictogram([text_list[index + order]]) # return dictionary return markov_dict
def dictogram_dictlist(self): """ Converts the list in word_dict to dictionary by calling Dictogram on the values. """ for key, value in self.word_dict.items(): self.word_dict[key] = dictogram.Dictogram(value)
def generate(token_list): histogram = dictogram.Dictogram(token_list) return histogram