Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Papoteur committed Oct 20, 2021
2 parents 1c4c963 + f97ab58 commit 2b2cc68
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 83 deletions.
5 changes: 5 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Changelog
#########

- 2021/10/05: Fix bug where quickly running begin/end would leave dictation enabled.
- 2021/07/08: Add ``--sample-rate``, optionally set the sample rate used for recording.
- 2021/06/25: Add ``--idle-time``, optionally idle to avoid high CPU usage for no perceptual gain (fixes #6).
- 2021/06/07: Add ``--delay-exit``, convenient when pushed to talk is used.
- 2021/06/07: Improve recording logic to capture more of the end of the recording before exiting.
- 2021/05/30: Fix error with ``xdotool`` mistaking text as arguments.
- 2021/05/30: Fix adding numbers with "and", "one and two" now resolve to "1 and 2" not "3".
- 2021/05/30: Add numeric scales up to 'centillion' (10**303).
Expand Down
73 changes: 73 additions & 0 deletions examples/begin_end_commands/nerd-dictation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# User configuration file typically located at `~/.config/nerd-dictation/nerd-dictation.py`

# This examples shows how explicit start/end commands can be implemented.
#
# This assumes dictation is always running in the background,
# special commands are spoken to start/end dictation which are excluded

# Global, track when dictation is active.
is_active = False

# -----------------------------------------------------------------------------
# Constants

# Commands to use.
START_COMMAND = ("start", "dictation")
FINISH_COMMAND = ("finish", "dictation")


# -----------------------------------------------------------------------------
# Utility Functions

def match_words_at_index(haystack_words, haystack_index, needle_words):
"""
Check needle_words is in haystack_words at haystack_index.
"""
return (
(needle_words[0] == haystack_words[haystack_index]) and
(haystack_index + len(needle_words) <= len(haystack_words)) and
(needle_words[1:] == haystack_words[haystack_index + 1 : haystack_index + len(needle_words)])
)


# -----------------------------------------------------------------------------
# Main Processing Function

def nerd_dictation_process(text):
global is_active

words_input = tuple(text.split(" "))
words = []

i = 0

# First check if there is text prior to having begun/ended dictation.
# The part should always be ignored.
if is_active:
while i < len(words_input):
if match_words_at_index(words_input, i, START_COMMAND):
i += len(START_COMMAND)
break
i += 1
if i == len(words_input):
i = 0
# Else keep the advance of 'i', since it skips text before dictation started.

while i < len(words_input):
word = words_input[i]
if is_active:
if match_words_at_index(words_input, i, FINISH_COMMAND):
is_active = False
i += len(FINISH_COMMAND)
continue
else:
if match_words_at_index(words_input, i, START_COMMAND):
is_active = True
i += len(START_COMMAND)
continue

if is_active:
words.append(word)
i += 1

return " ".join(words)
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ def nerd_dictation_process(text):

words[i] = w

# Strip any words that were replaced with empty strings.
words[:] = [w for w in words if w]

return " ".join(words)
4 changes: 2 additions & 2 deletions hacking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Style
-----

- Auto formatting is handled with black by running:
``black nerd-dictator``
``black nerd-dictation``
- Ensure correct type annotations by running:
``mypy --strict nerd-dictator``.
``mypy --strict nerd-dictation``.
- Check for errors with:
``pylint nerd-dictation --disable=C0103,C0111,C0301,C0302,C0415,E0401,E0611,I1101,R0801,R0902,R0903,R0912,R0913,R0914,R0915,R1705,W0212,W0703``

Expand Down
Loading

0 comments on commit 2b2cc68

Please sign in to comment.