13
13
14
14
import argparse
15
15
import codecs
16
+ import math
16
17
import os
18
+ import random
17
19
import sys
18
20
19
21
from wpm .convert import wpm_to_cpm
@@ -60,6 +62,9 @@ def parse_args():
60
62
argp .add_argument ("--search" , default = None , type = str ,
61
63
help = "Put quotes/authors/titles matching case-insensitive text query first" )
62
64
65
+ argp .add_argument ("--short" , default = False , action = "store_true" ,
66
+ help = "Starts wpm with short texts" )
67
+
63
68
opts = argp .parse_args ()
64
69
65
70
if opts .version :
@@ -196,6 +201,36 @@ def search(quotes, query):
196
201
if (query in text ) or (query in author ) or (query in title ):
197
202
yield quote .text_id
198
203
204
+
205
+ def short_quotes_first (quotes , cutoff = 0.2 ):
206
+ """Returns text IDs of all quotes with shorter ones first (but still
207
+ randomized)."""
208
+
209
+ cutoff = cutoff / 0.5 # find absolute cutoff percentage based on avg (0.5)
210
+ words = 0
211
+
212
+ def word_length (text ):
213
+ return len (text .split (" " ))
214
+
215
+ # Find average number of words first
216
+ for quote in iter (quotes ):
217
+ quote = wpm .quotes .Quote .from_tuple (quote )
218
+ words += word_length (quote .text )
219
+
220
+ avg = words / len (quotes )
221
+ threshold = int (math .ceil (avg * cutoff ))
222
+
223
+ # Put short quotes i a randomized, starting bucket
224
+ short = []
225
+ for quote in iter (quotes ):
226
+ quote = wpm .quotes .Quote .from_tuple (quote )
227
+ if word_length (quote .text ) < threshold :
228
+ short .append (quote .text_id )
229
+
230
+ random .shuffle (short )
231
+ return short
232
+
233
+
199
234
def main ():
200
235
"""Main entry point for command line invocation."""
201
236
try :
@@ -219,12 +254,16 @@ def main():
219
254
print_stats (stats , opts .cpm )
220
255
return
221
256
257
+ text_ids = None
258
+
222
259
if opts .search :
223
260
text_ids = list (search (quotes , opts .search .lower ()))
224
261
225
262
if not text_ids :
226
263
print ("No quotes matching %r" % opts .search )
227
264
sys .exit (1 )
265
+ elif opts .short :
266
+ text_ids = short_quotes_first (quotes )
228
267
elif opts .id is not None :
229
268
text_ids = [opts .id ]
230
269
else :
0 commit comments