Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions crawler.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import re
import string
import numpy as np
from collections import defaultdict

alphabet = string.ascii_uppercase + ' '


def cbpd_from_book(book):
text = sanitize(open(book, 'r').read())
g = {}
for c1 in alphabet:
g[c1] = {}
for c2 in alphabet:
g[c1][c2] = 0


g = defaultdict(lambda: defaultdict(int))

for i in range(0, len(text) - 1):
g[text[i]][text[i + 1]] += 1

for c1 in g.keys():
for c1 in g:
total = sum(g[c1].values())
for c2 in g[c1].keys():
g[c1][c2] = g[c1][c2] / total
for c2 in g[c1]:
g[c1][c2] /= total

return g

Expand Down