forked from rossgoodwin/cutup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutup.py
60 lines (48 loc) · 1.28 KB
/
cutup.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
56
57
58
59
60
from sys import argv
script, book, ll = argv
# Open single page text file (original source)
f = open(book, "r")
text = f.read()
f.close()
# Set line length
lineLength = int(ll)
# Split text into word tokens
tokens = text.split()
# Split tokens into lines
curLine = []
lines = [] # 2D list
charCount = 0
for t in tokens:
charCount += len(t)+1
if charCount <= lineLength+1:
curLine.append(t)
else:
lines.append(curLine)
curLine = []
charCount = len(t)+1
curLine.append(t)
# Make horizontal cut
hCutPt = len(lines)//2
hSections = [lines[:hCutPt], lines[hCutPt:]]
# Make vertical cut
sections = [[] for i in range(4)] # 3D list
for i in range(4):
for l in hSections[i//2]:
sections[i].append([])
for i in range(2):
lineCount = 0
for l in hSections[i]:
charCount = 0
for w in l:
charCount += len(w)+1
if charCount <= lineLength/2 + 1:
sections[0+i*2][lineCount].append(w)
else:
sections[1+i*2][lineCount].append(w)
lineCount+=1
# Rearrange per Burroughs' instructions
sections.reverse()
# Print result with line breaks
for i in [0, 2]:
for j in range(len(sections[i])):
print(f' '.join(sections[i][j]), ' '.join(sections[i+1][j]))