def prob_sample(histo): ''' Input a histogram. Output a randomly chosen word from the histogram relative to its frequency in the body of text. ''' words = stoch(histo) # print(words) dart = random.randint(0, 100) counter = 0 word = None for pair in words: if counter < dart: counter += pair[1] word = pair[0] return dart, counter, word if __name__ == "__main__": listo_histo = list_hist("source.txt") dicto_histo = dict_hist("source.txt") print(dicto_histo) print(prob_sample(listo_histo)) print(prob_sample(dicto_histo))
def hello_world(): my_file = ('source.txt') my_histogram = list_hist(my_file) word = prob_sample(my_histogram) return word
def hello_world(): my_file = open("./words.txt", "r") lines = clean(my_file) my_histogram = list_hist(lines) word = prob_sample(my_histogram) return word
def hello_world(): num_words = 10 sentence = [] my_file = ('source.txt') my_histogram = list_hist(my_file) for i in range(num_words): word = prob_sample(my_histogram) sentence += ' ' + word return sentence
import random from histogram import unique_words, frequency, list_hist def stoch(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' percentages = [] total_wc = 0 for item in histo: total_wc += int(item[1]) for item in histo: freq = frequency(item[0], histo) perc = freq / total_wc instance = (item[0], perc) percentages.append(instance) return percentages if __name__ == "__main__": source = 'one fish two fish red fish blue fish' print(stoch(list_hist(source)))
''' Input a histogram. Output a randomly chosen word from the histogram relative to its frequency in the body of text. ''' words = stoch(histo) # print(words) dart = random.randint(0, 100) counter = 0 word = None # for pair in words: # pair = word and freq in percent # if counter < dart: # counter += pair[1] # since words already in percent - we just keep adding til we hit the dart # word = pair[0] # else: # return while counter < dart: for pair in words: counter += pair[1] word = pair[0] return dart, counter, word if __name__ == "__main__": listo_histo = list_hist("source.txt") # dicto_histo = dict_hist("source.txt") # print(dicto_histo) print(prob_sample(listo_histo)) # print(prob_sample(dicto_histo))
import random from histogram import unique_words, frequency, list_hist def stoch(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' percentages = [] total_wc = 0 for item in histo: total_wc += int(item[1]) for item in histo: freq = frequency(item[0], histo) perc = freq / total_wc instance = (item[0], perc) percentages.append(instance) return percentages if __name__ == "__main__": source = 'one fish two fish red fish blue fish' listo_histo = list_hist("source.txt") print(stoch(list_hist(source)))
import random from histogram import unique_words, list_hist def stochastic(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' unique = unique_words(histo) # number of unique words print(unique) rand_num = random.randint( 1, unique) # random number in range of number of unique words print(rand_num) counter = 0 word = None # count until we hit the random number for item in histo: while counter < rand_num: counter += 1 word = item print(word[0]) return word[0] if __name__ == '__main__': source = 'one fish two fish red fish blue fish' stochastic(list_hist(source))
import random from histogram import unique_words, list_hist def stochastic(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' unique = unique_words(histo) # number of unique words print(unique) rand_num = random.randint(1, unique) # random number in range of number of unique words print(rand_num) counter = 0 word = None # count until we hit the random number for item in histo: if counter < rand_num: counter += 1 word = item print(word[0]) return word[0] if __name__ == '__main__': stochastic(list_hist(sample.txt))
import random import sample from histogram import unique_words, list_hist def stochastic(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' unique = unique_words(histo) # number of unique words print(unique) rand_num = random.randint( 1, unique) # random number in range of number of unique words print(rand_num) counter = 0 word = None # count until we hit the random number for item in histo: if counter < rand_num: counter += 1 word = item print(word[0]) return word[0] if __name__ == '__main__': stochastic(list_hist('sample.txt'))
total_wc = 0 for item in histo: total_wc += item[0] for item in histo: freq = freq(item[0], histo) perc = freq / total_wc instance = [item[0], perc] percentages.append(instance) return percentages if __name__ == "__main__": stochastic(list_hist(source.txt)) counter = 0 word = None # count until we hit the random number for item in histo: if counter < rand_num: counter += 1 word = item print(word[0]) return word[0] if __name__ == '__main__': source = 'one fish two fish red fish blue fish' stochastic(list_hist(source))
import random import sample from histogram import unique_words, list_hist def stochastic(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' unique = unique_words(histo) # number of unique words print(unique) rand_num = random.randint( 1, unique) # random number in range of number of unique words print(rand_num) counter = 0 word = None # count until we hit the random number for item in histo: if counter < rand_num: counter += 1 word = item print(word[0]) return word[0] if __name__ == '__main__': stochastic(list_hist(sample))
import random from histogram import unique_words, list_hist def stochastic(histo): ''' Stochastic sampling means taking an element from a given collection at random ''' unique = unique_words(histo) # number of unique words print(unique) rand_num = random.randint(1, unique) # random number in range of number of unique words print(rand_num) counter = 0 word = None # count until we hit the random number for item in histo: if counter < rand_num: counter += 1 word = item print(word[0]) return word[0] if __name__ == '__main__': stochastic(list_hist('source.txt'))