-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBabelProject.py
More file actions
80 lines (65 loc) · 1.91 KB
/
BabelProject.py
File metadata and controls
80 lines (65 loc) · 1.91 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 16:10:42 2019
@author: lu
"""
from library_of_babel import LoB
import random
import string
import matplotlib.pyplot as plt
import numpy as np
lob=LoB()
keyword="trash"
address=lob.search(keyword)
print address
page= lob.getPage(address)
index=page.find(keyword)
print page[index:index+len(keyword)+10]
keyword="garbage"
address=lob.search(keyword)
#print address
page= lob.getPage(address)
index=page.find(keyword)
#print page[index:index+len(keyword)+10]
babelString=""
for i in range(0,100):
randomString = ''.join([random.choice(string.ascii_lowercase) for n in xrange(16)])
try:
address=lob.search(randomString)
#print address
page= lob.getPage(address)
babelString+=page
except:
print "cant find"
def letterFreq(wholeString,yLabel="Letter Frequencies",title="title"):
alphabeltString=string.ascii_lowercase
print alphabeltString
alphabetList=[]
wholeString= filter(str.isalpha, wholeString)
for letter in alphabeltString:
alphabetList.append(letter)
letterCntDict=dict()
letterFreqDict=dict()
for letter in alphabeltString:
letterCntDict[letter]=wholeString.count(letter)
letterFreqDict[letter]=float(wholeString.count(letter))/len(wholeString)
sorted_x = sorted(letterCntDict.items(), key=lambda x: x[1])
keyList=[]
cntList=[]
freqList=[]
for tup in sorted_x:
keyList.append(tup[0])
cntList.append(int(tup[1]))
freqList.append(float(tup[1])/len(wholeString))
objects = keyList
y_pos = np.arange(len(objects))
performance = freqList
plt.figure()
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel(yLabel)
plt.title(title)
plt.show()
return letterFreqDict
letterFreq(babelString.lower(),title="Letter Frequency of 100 babel pages")