Skip to content

Commit 0f94726

Browse files
committed
Adds --short
1 parent 9b7d421 commit 0f94726

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

wpm/commandline.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
import argparse
1515
import codecs
16+
import math
1617
import os
18+
import random
1719
import sys
1820

1921
from wpm.convert import wpm_to_cpm
@@ -60,6 +62,9 @@ def parse_args():
6062
argp.add_argument("--search", default=None, type=str,
6163
help="Put quotes/authors/titles matching case-insensitive text query first")
6264

65+
argp.add_argument("--short", default=False, action="store_true",
66+
help="Starts wpm with short texts")
67+
6368
opts = argp.parse_args()
6469

6570
if opts.version:
@@ -196,6 +201,36 @@ def search(quotes, query):
196201
if (query in text) or (query in author) or (query in title):
197202
yield quote.text_id
198203

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+
199234
def main():
200235
"""Main entry point for command line invocation."""
201236
try:
@@ -219,12 +254,16 @@ def main():
219254
print_stats(stats, opts.cpm)
220255
return
221256

257+
text_ids = None
258+
222259
if opts.search:
223260
text_ids = list(search(quotes, opts.search.lower()))
224261

225262
if not text_ids:
226263
print("No quotes matching %r" % opts.search)
227264
sys.exit(1)
265+
elif opts.short:
266+
text_ids = short_quotes_first(quotes)
228267
elif opts.id is not None:
229268
text_ids = [opts.id]
230269
else:

wpm/quotes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@ def put_to_front(self, text_ids):
8282
front = []
8383
back = []
8484

85+
# Calculate indices: Maps quote's text_id to internal index
86+
tid = {}
8587
for index in range(len(self.quotes)):
8688
quote = self[index]
89+
tid[quote.text_id] = index
8790

88-
if quote.text_id in text_ids:
89-
front.append(index)
90-
else:
91-
back.append(index)
91+
# Preserve text_ids order in front
92+
front = [tid[i] for i in text_ids]
93+
random.shuffle(front)
94+
back = [tid[i] for i in (set(tid.keys()) - set(text_ids))]
95+
# The whole quote indexes stuff badly needs rafactoring
9296

9397
random.shuffle(back)
9498
self.indices = front + back

0 commit comments

Comments
 (0)