-
Notifications
You must be signed in to change notification settings - Fork 0
/
stochastic_sampling.py
55 lines (40 loc) · 1.44 KB
/
stochastic_sampling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random, sys
# from class_methods.histogram import *
# from histogram import *
'''
<-- Functions -->
randomized_word()
- takes a 'source_text' argument.
- stores a text file in a listogram
- returns a word based on its frequency
test_word()
- calculates the total amount of each word based on it's frequency
'''
list = 'one fish two fish red fish blue fish'
def randomized_word(histogram):
total_count = 0 #find what this value is
chance = 0.0 #'chance' is the probability of getting a particular word
# listogram = list_of_list(histogram) #list of list of unique words and its frequency
random_num = random.random() #random number from 0 & 1
hist = histogram.items()
# print(hist)
#loops through listogram & add the total count of each word
for key, value in histogram.items():
# print("value", value[1])
total_count += value
for key, value in histogram.items():
chance += value / total_count #divide the words frequency with total count
if chance >= random_num: #check the chance of getting a particular based on their probability
return key
def test_word():
source = sys.argv[1]
test = {}
for i in range(10000):
random_word = randomized_word(source)
if random_word not in test:
test[random_word] = 1
else:
test[random_word] += 1
return test
if __name__ == "__main__":
print(test_word())