Skip to content

Commit

Permalink
fallback to shorter error if there are no enough parts
Browse files Browse the repository at this point in the history
  • Loading branch information
baverman committed Feb 4, 2011
1 parent ce69910 commit 63e0840
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions typetrainer/generator.py
Expand Up @@ -84,6 +84,9 @@ def make_choicer(self):

return ch

def count(self, part):
return sum(1 if part in p else 0 for p in self.parts)

def choice(self, rnd):
if not self.choicer:
self.choicer = self.make_choicer()
Expand Down
14 changes: 13 additions & 1 deletion typetrainer/tutors/common.py
Expand Up @@ -72,4 +72,16 @@ def strip_non_word_chars(self, string):
if c in self.word_chars:
result += c

return result
return result

def get_available_parts_for(self, key):
result = total = 0
for p in self.other.values():
result += p.count(key)
total += len(p.parts)

for p in self.first.values():
result += p.count(key)
total += len(p.parts)

return result / float(total)
11 changes: 8 additions & 3 deletions typetrainer/ui/main.py
Expand Up @@ -112,9 +112,14 @@ def on_type_entry_activate(self, *args):
errors += 1

err = self.get_error(self.typed_chars)
if err:
self.filler.change_distribution(err, 0.8, True)
self.retype_lb.set_text(err)
while err:
volume = self.filler.get_available_parts_for(err)
if volume > 0.005:
self.filler.change_distribution(err, 0.8, True)
self.retype_lb.set_text(err)
break

err = err[1:]
else:
self.filler.reset_distribution()
self.retype_lb.set_text('')
Expand Down

0 comments on commit 63e0840

Please sign in to comment.