-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadRunner.py
195 lines (130 loc) · 3.72 KB
/
ReadRunner.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
# coding: utf-8
# In[216]:
# Import dem libraries.
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
from newspaper import Article
# In[218]:
def getLeft(word):
length = len(word)
if length == 1:
return ""
if length == 2:
return ""
leftHalf = word[0:length//2]
return leftHalf
def getMiddle(word):
length = len(word)
if length == 0:
return ""
if length == 1:
return word
if length == 2:
return word[0]
centerChar = word[length//2]
return centerChar
def getRight(word):
length = len(word)
if length == 1:
return ""
if length == 2:
return word[1]
rightHalf = word[length//2 + 1:]
return rightHalf
# In[219]:
def genVid(words, WPM):
# ----- Set up Plotting Area -----
fig, ax = plt.subplots(figsize=(5, 3))
ax.set(xlim=(-3, 3), ylim=(-1, 1))
plt.xticks([])
plt.yticks([])
ax.set(xlim=(-1, 1), ylim=(-1, 1))
# ----- TUNABLE PARAMETERS -----
CLOSENESS = .05
FONTSIZE = 25
WPM = WPM
# ----- TEXT INITIALIZATION -----
logo = ax.text(-.95,
.95,
"ReadRunner",
ha='left',
va='top',
color = "purple",
fontsize=15)
wpm = ax.text(.95,
.95,
str(WPM) + " WPM",
ha='right',
va='top',
color = "red",
fontsize=10)
leftText = ax.text(-CLOSENESS,
0,
"left",
ha='right',
va='center',
fontsize=FONTSIZE)
middleText = ax.text(0,
0,
"left center",
ha='center',
va='center',
fontsize=FONTSIZE,
color='red')
rightText = ax.text(CLOSENESS,
0,
"right",
ha='left',
va='center',
fontsize=FONTSIZE)
# ----- Function to update -----
def animate(i):
leftText.set_text(getLeft(words[i]))
middleText.set_text(getMiddle(words[i]))
rightText.set_text(getRight(words[i]))
# ----- Animation call -----
frames = len(words)
interval = (1000)*(60/frames)*(frames)/(WPM)
anim = FuncAnimation(
fig, animate, interval=interval, frames=frames)
# ----- Show the Animation -----
return(HTML(anim.to_html5_video()))
# ----- Save the Animation -----
# anim.save('speedread.mp4')
fig.withdraw()
ax.withdraw()
fig.close()
plt.close()
print("Speed read saved!")
# anim.save('filename.gif', writer='imagemagick')
# In[ ]:
# Parse Words
def parseURL(url):
article = Article(url)
article.download()
article.parse()
text = article.text.strip()
return(text)
# Processing
def parseWords(rawText):
rawText = rawText.replace("\r", " ")
rawText = rawText.replace("\n", " ")
words = rawText.split(" ")
return words
# In[246]:
def readRunner(url, WPM):
return genVid(parseWords(parseURL(url)), WPM = WPM)
# In[257]:
def readRunners(hi="4"):
# Generate video
print("Welcome to ReadRunner! \r\n Making reading quicker than ever.")
rawText = input("Enter news article: ")
WPM = int(input("Enter WPM: "))
return readRunner(rawText, WPM)
#readRunnerNB()
# In[ ]:
# In[ ]: