-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipf.py
executable file
·52 lines (43 loc) · 1.16 KB
/
zipf.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
#! /usr/bin/env python
import random
import matplotlib.pyplot as plt
def pick_char(l):
return l[random.randint(0,len(l)-1)]
def init():
alphabet = [chr(i) for i in range(0x61,0x7B)]
alphabet.append(chr(0x20))
return alphabet
def tuples2lists(l_tuples):
size_tuple = len(l_tuples[0])
l_lists = []
for i in range(size_tuple):
l_lists.append([])
for point in l_tuples:
for i in range(size_tuple):
l_lists[i].append(point[i])
return l_lists
def fill(num):
alphabet = init()
chars = [pick_char(alphabet) for i in range(num)]
text = ''.join(chars)
return text
def stats(text):
dictionary = {}
for word in text.split(' '):
if not dictionary.has_key(word):
dictionary[word] = 0
dictionary[word] +=1
results = []
for word in dictionary.keys():
results.append((dictionary[word],word))
results.sort()
return results
if __name__=="__main__":
text = fill(100000000)
results = stats(text)
l_lists = tuples2lists(results)
frequency = l_lists[0]
print frequency
frequency.reverse()
plt.loglog(frequency)
plt.show()