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 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: # 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 for pair in words: while counter < dart: counter += pair[1] word = pair[0] return dart, counter, word
def prob_sample(histo): words = stoch(histo) dart = random.randint(0, 100) counter = 0 word = None for pair in words: counter += pair[1] word = pair[0]
def prob_sample(histo): 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
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
def prob_sample(histo): ''' Input a histogram. Output a randomly chosen word from the histogram relative to its frequency. ''' words = stoch(histo) print(words) dart = random.randint(0, 100) counter = 0 word = None for pair in words: # pair = word, freq in percent word = pair[0] if counter <= dart: counter += pair[ 1] # since words already in percent - we just keep adding til we hit the dart else: return word
def prob_sample(histo): ''' Input a histogram. Output a randomly chosen word from the histogram relative to its frequency. ''' words = stoch(histo) # print(words) dart = random.randint(0, 100) counter = 0 # print(words) # print(dart) for pair in words: # pair = word, freq in percent # print(counter) # print(pair) counter += pair[1] # add til we cross the dart word = pair[0] if counter >= dart: return word
def prob_sample(histo): words = stoch(histo) dart = random.randint(0, 100)
def hello_world(): return stoch()