def test_chain_traversal(): word_list = get_words("fish.txt") markov_chain = MarkovChain(word_list) pprint(markov_chain)
from dictionary_words import get_words from dictionary_histogram import dict_histogram wordList = get_words('animals.txt') def histogram_tuple(list): dictionary = dict_histogram(list) tuple_histogram = [] for i in dictionary: tuple_histogram.append((i, dictionary[i])) return tuple_histogram if __name__ == "__main__": print(histogram_tuple(wordList))
num = 0 random_num = uniform(0, total_hist_count(hist)) for word in hist: count = hist[word] num += count if num > random_num: return word def sample_sentence(num, hist): """ using the sample function, this function creates a sentence, the length depending on what is passed into num """ sentence = [] for _ in range(0, num): sentence.append(sample(hist)) return ' '.join(sentence) def test_probability(word_hist, num): probability = [] for _ in range(0, num): probability.append(sample(word_hist)) pprint(dict_histogram(probability)) if __name__ == "__main__": word_list = get_words('fish.txt') word_hist = dict_histogram(word_list) test_probability(word_hist, 1000)
import dictionary_words import rearrange import sys import itertools dic_words = dictionary_words.get_words() def get_all_anagrams(word): output_anagrams = [] word_chars = list(word) for n in range(len(word_chars) - 1): for permutation in itertools.permutations(word_chars, len(word_chars)): current_str = set_to_string(permutation).lower() output_anagrams.append(current_str) return remove_duplicates(output_anagrams) def get_anagrams_from_dictionary(word): output_anagrams = [] for dic_word in dic_words: # Only look if length matches if len(dic_word) == len(word): original_dic_word = dic_word word_chars = list(word.lower()) dic_word_chars = list(dic_word.lower().strip())
from dictionary_words import get_words def dict_histogram(lists): """Count occurences in the given list and return that data structure""" dictionary = {} for i in lists: if i in dictionary: dictionary[i] += 1 else: dictionary[i] = 1 return dictionary def total_hist_count(hist): total_count = 0 for word in hist: total_count += hist[word] return total_count if __name__ == "__main__": print(dict_histogram(get_words('1984.txt')))
def tweet(): word_list = get_words('1984.txt') markov_chain = MarkovChain(word_list) sentence = markov_chain.chain_traversal(30) return render_template("index.html", sentence = sentence)