-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunks.py
62 lines (41 loc) · 1.27 KB
/
chunks.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
61
62
from __future__ import with_statement
import json
###
import SimpleHTTPServer
import SocketServer
###
# def paragraphs(line):
# return [l.split('. ') for l in line.splitlines() if l]
#data = filter(lambda x: len(x) > 0, map(paragraphs, open('text-sample.txt')))
#data = [f.splitlines() for f in open('text-sample.txt') if len(f) > 1]
#data = [l for l in open('text-sample.txt', 'r')]
data = []
with open('text-sample.txt', 'r') as f:
for f in f.readlines():
if len(f) > 1:
line = f.rstrip().split('. ')
data.append(line)
#data = map(sentences, data)
print(len(data))
j = open('text-output.json', 'w')
j.write(json.dumps(data, sort_keys=True, indent=4))
j.close()
order = [0, 3, 5, 7, 9, 1, 2, 4, 6, 8, 10, 11]
def show_text(data, order=False):
text = []
if not order:
for l in data:
if isinstance(l, list):
text.append('. '.join(l))
else:
for pos in order:
text.append('. '.join(data[pos]))
return '\n\n'.join(text)
with open('text-output.txt', 'w') as f:
f.write(show_text(data, order))
###
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()