|
1 | 1 | import time
|
2 | 2 |
|
3 |
| -from PyQt5.QtCore import Qt, QTimer, pyqtSlot |
| 3 | +from PyQt5.QtCore import Qt, QTimer |
4 | 4 |
|
5 | 5 | from plover.gui_qt.tool import Tool
|
6 |
| - |
7 | 6 | from textstat.textstat import textstat
|
| 7 | + |
| 8 | +from plover_wpm_meter.strokes_meter_ui import Ui_StrokesMeter |
| 9 | +from plover_wpm_meter.textstat.textstat import textstat |
8 | 10 | from plover_wpm_meter.wpm_meter_ui import Ui_WpmMeter
|
9 | 11 |
|
10 | 12 |
|
11 |
| -class PloverWpmMeter(Tool, Ui_WpmMeter): |
12 |
| - TITLE = "WPM Meter" |
13 |
| - ROLE = "wpm_meter" |
14 |
| - |
15 |
| - _TIMEOUTS = { |
16 |
| - "wpm1": 10, |
17 |
| - "wpm2": 60, |
18 |
| - } |
19 |
| - |
| 13 | +class BaseMeter(Tool): |
20 | 14 | def __init__(self, engine):
|
21 | 15 | super().__init__(engine)
|
22 |
| - self.setupUi(self) |
23 | 16 |
|
24 | 17 | self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
|
25 |
| - self.wpm_method.addItem("NCRA", "ncra") |
26 |
| - self.wpm_method.addItem("Traditional", "traditional") |
| 18 | + self.setupUi(self) |
27 | 19 |
|
28 | 20 | self._timer = QTimer()
|
29 | 21 | self._timer.setInterval(1000)
|
30 | 22 | self._timer.setTimerType(Qt.PreciseTimer)
|
31 |
| - self._timer.timeout.connect(self._update_wpms) |
| 23 | + self._timer.timeout.connect(self.on_timer) |
32 | 24 | self._timer.start()
|
33 | 25 |
|
34 |
| - self._chars = [] |
35 |
| - engine.signal_connect("translated", self._on_translation) |
36 |
| - |
37 | 26 | self.restore_state()
|
38 | 27 | self.finished.connect(self.save_state)
|
39 | 28 |
|
40 |
| - def _on_translation(self, old, new): |
| 29 | + self.chars = [] |
| 30 | + engine.signal_connect("translated", self.on_translation) |
| 31 | + |
| 32 | + def on_translation(self, old, new): |
41 | 33 | for action in old:
|
42 | 34 | remove = len(action.text)
|
43 | 35 | if remove > 0:
|
44 |
| - self._chars = self._chars[:-remove] |
45 |
| - self._chars += _timestamp_chars(action.replace) |
| 36 | + self.chars = self.chars[:-remove] |
| 37 | + self.chars += _timestamp_items(action.replace) |
46 | 38 |
|
47 | 39 | for action in new:
|
48 | 40 | remove = len(action.replace)
|
49 | 41 | if remove > 0:
|
50 |
| - self._chars = self._chars[:-remove] |
51 |
| - self._chars += _timestamp_chars(action.text) |
| 42 | + self.chars = self.chars[:-remove] |
| 43 | + self.chars += _timestamp_items(action.text) |
| 44 | + |
| 45 | + def on_timer(self): |
| 46 | + raise NotImplementedError() |
| 47 | + |
| 48 | + |
| 49 | +class PloverWpmMeter(BaseMeter, Ui_WpmMeter): |
| 50 | + TITLE = "WPM Meter" |
| 51 | + ROLE = "wpm_meter" |
| 52 | + |
| 53 | + _TIMEOUTS = { |
| 54 | + "wpm1": 10, |
| 55 | + "wpm2": 60, |
| 56 | + } |
| 57 | + |
| 58 | + def __init__(self, engine): |
| 59 | + super().__init__(engine) |
| 60 | + self.strokes = [] |
| 61 | + self.wpm_method.addItem("NCRA (by syllables)", "ncra") |
| 62 | + self.wpm_method.addItem("Traditional (by characters)", "traditional") |
| 63 | + self.wpm_method.addItem("Spaces (by whitespace)", "spaces") |
52 | 64 |
|
53 |
| - @pyqtSlot() |
54 |
| - def _update_wpms(self): |
| 65 | + def on_timer(self): |
55 | 66 | max_timeout = max(self._TIMEOUTS.values())
|
56 |
| - self._chars = _filter_old_chars(self._chars, max_timeout) |
| 67 | + self.chars = _filter_old_items(self.chars, max_timeout) |
57 | 68 | for name, timeout in self._TIMEOUTS.items():
|
58 |
| - chars = _filter_old_chars(self._chars, timeout) |
| 69 | + chars = _filter_old_items(self.chars, timeout) |
59 | 70 | wpm = _wpm_of_chars(chars, method=self.wpm_method.currentData())
|
60 | 71 | getattr(self, name).display(str(wpm))
|
61 | 72 |
|
62 | 73 |
|
63 |
| -def _timestamp_chars(chars): |
| 74 | +class PloverStrokesMeter(BaseMeter, Ui_StrokesMeter): |
| 75 | + TITLE = "Strokes Meter" |
| 76 | + ROLE = "strokes_meter" |
| 77 | + |
| 78 | + _TIMEOUTS = { |
| 79 | + "strokes1": 10, |
| 80 | + "strokes2": 60, |
| 81 | + } |
| 82 | + |
| 83 | + def __init__(self, engine): |
| 84 | + super().__init__(engine) |
| 85 | + self.strokes_method.addItem("NCRA (by syllables)", "ncra") |
| 86 | + self.strokes_method.addItem("Traditional (by characters)", |
| 87 | + "traditional") |
| 88 | + self.strokes_method.addItem("Spaces (by whitespace)", "spaces") |
| 89 | + self.actions = [] |
| 90 | + |
| 91 | + # By default, the QLCDNumbers will just display "0", without a decimal |
| 92 | + # point, on initial render. Render them ourselves so that we don't |
| 93 | + # switch from "0" to "0.00" after a second. |
| 94 | + self.on_timer() |
| 95 | + |
| 96 | + def on_translation(self, old, new): |
| 97 | + super().on_translation(old, new) |
| 98 | + if len(old) > 0: |
| 99 | + self.actions = self.actions[:-len(old)] |
| 100 | + self.actions += _timestamp_items(new) |
| 101 | + |
| 102 | + def on_timer(self): |
| 103 | + max_timeout = max(self._TIMEOUTS.values()) |
| 104 | + self.chars = _filter_old_items(self.chars, max_timeout) |
| 105 | + self.actions = _filter_old_items(self.actions, max_timeout) |
| 106 | + for name, timeout in self._TIMEOUTS.items(): |
| 107 | + chars = _filter_old_items(self.chars, timeout) |
| 108 | + num_strokes = len(_filter_old_items(self.actions, timeout)) |
| 109 | + strokes_per_word = _spw_of_chars( |
| 110 | + num_strokes, |
| 111 | + chars, |
| 112 | + method=self.strokes_method.currentData() |
| 113 | + ) |
| 114 | + getattr(self, name).display("{:0.2f}".format(strokes_per_word)) |
| 115 | + |
| 116 | + |
| 117 | +def _timestamp_items(items): |
64 | 118 | current_time = time.time()
|
65 |
| - return [(c, current_time) for c in chars] |
| 119 | + return [(i, current_time) for i in items] |
66 | 120 |
|
67 | 121 |
|
68 |
| -def _filter_old_chars(chars, timeout): |
| 122 | +def _filter_old_items(items, timeout): |
69 | 123 | current_time = time.time()
|
70 |
| - return [(c, t) for c, t in chars |
| 124 | + return [(i, t) for i, t in items |
71 | 125 | if (current_time - t) <= timeout]
|
72 | 126 |
|
73 | 127 |
|
74 |
| -def _words_in_text(string, method): |
| 128 | +def _words_in_chars(chars, method): |
| 129 | + text = "".join(c for c, _ in chars) |
75 | 130 | if method == "ncra":
|
76 | 131 | # The NCRA defines a "word" to be 1.4 syllables, which is the average
|
77 | 132 | # number of syllables per English word.
|
78 | 133 | syllables_per_word = 1.4
|
79 | 134 | # For some reason, textstat returns syllable counts such as a
|
80 | 135 | # one-syllable word like "the" being 0.9 syllables.
|
81 |
| - syllables_in_text = textstat.syllable_count(string) / 0.9 |
| 136 | + syllables_in_text = textstat.syllable_count(text) / 0.9 |
82 | 137 | return syllables_in_text * (1 / syllables_per_word)
|
83 | 138 | elif method == "traditional":
|
84 | 139 | # Formal definition; see https://en.wikipedia.org/wiki/Words_per_minute
|
85 |
| - return len(string) / 5 |
| 140 | + return len(text) / 5 |
| 141 | + elif method == "spaces": |
| 142 | + return len([i for i in text.split() if i]) |
86 | 143 | else:
|
87 | 144 | assert False, "bad wpm method: " + method
|
88 | 145 |
|
89 | 146 |
|
90 |
| -def _wpm_of_chars(chars, method): |
91 |
| - if not chars: |
92 |
| - return 0 |
93 |
| - |
| 147 | +def _time_interval_of_chars(chars): |
94 | 148 | start_time = min(t for _, t in chars)
|
95 | 149 | current_time = time.time()
|
96 | 150 | time_interval = current_time - start_time
|
97 | 151 | time_interval = max(1, time_interval)
|
| 152 | + return time_interval |
98 | 153 |
|
99 |
| - text = "".join(c for c, _ in chars) |
100 |
| - num_words = _words_in_text(text, method) |
| 154 | + |
| 155 | +def _wpm_of_chars(chars, method): |
| 156 | + num_words = _words_in_chars(chars, method) |
| 157 | + if not num_words: |
| 158 | + return 0 |
| 159 | + |
| 160 | + time_interval = _time_interval_of_chars(chars) |
101 | 161 | num_minutes = time_interval / 60
|
102 | 162 | num_words_per_minute = num_words / num_minutes
|
103 | 163 | return int(round(num_words_per_minute))
|
| 164 | + |
| 165 | + |
| 166 | +def _spw_of_chars(num_strokes, chars, method): |
| 167 | + num_words = _words_in_chars(chars, method) |
| 168 | + if not num_words: |
| 169 | + return 0 |
| 170 | + |
| 171 | + return num_strokes / num_words |
0 commit comments