Fixing commonly-misrecognized words in dictation, voice commands, and regular text
| audio, subed, emacs, speech-recognitionI edit a lot of transcripts, and I've also been dictating to my computer more and more for both prose and voice commands. Speech recognition is pretty accurate, but there are some terms that it tends to get wrong. There are also some terms that I find hard to say, like sometimes my brain will just hiccup and I'll end up saying "start screaming" instead of "start streaming". I want to make it really easy to correct a misrecognized word throughout my current buffer and save it for future edits.
One of the advantages of doing this kind of replacement within Emacs instead of a separate dictation tool is that I can use it for both editing text (like transcripts of other people's presentations) as well as pre-process the speech recognition results when I dictate or use voice commands to control Emacs. I can even use it to start gradually getting my transcripts to follow Emacs keybinding conventions, like C-c instead of "control c", and for finicky capitalization like LaTeX.
1. Description and demo
You can watch a description and a demo here. The dictionary part starts at about 00:37:01.403.
If you prefer a clip,
2. Saving the dictionary
First, we define a variable that we're going to save the changes to.
;; Use the saved version of this instead of forcing the reevaluation
;;;###autoload
(defcustom sacha-subed-common-edits
'("I"
"I've"
"I'm"
"Mendeley"
"JavaScript"
"RSS"
("stop section" "subsection")
("EmacsConf" "EmacsCon" "EmacsConf" "imaxconf")
("going to" "gonna")
("want to" "wanna")
("transient" "transit")
("C-c" "control c" "Ctrl+C")
("C-x" "control x" "Ctrl+X")
("C-f" "control f")
("" "uh" "um")
("Magit" "maggot")
("Emacs" "e-max" "emex" "emax" "bmx" "imax")
("Emacs News" "emacs news")
("Emacs Lisp" "emacs list")
("ivy" "iv")
("UI" "ui")
("TECO" "tico")
("org-roam" "orgrim" "orgrom" "Org Rome")
("non-nil" "non-nail")
("commits" "comets")
"SQL"
"arXiv"
"Montessori"
"SVG"
"YouTube" "GitHub" "GitLab" "OmegaT" "Linux" "SourceForge"
"LaTeX"
"Lisp"
"Org"
"IRC"
"Reddit"
"PowerPoint"
"SQLite"
"SQL"
"I'll"
("<f9>" "F-9" "f9")
"I'd"
"PDFs"
"PDF"
"ASCII"
("Spacemacs" "spacemax")
"Elisp"
"Reddit"
"TextMate"
"macOS"
"API"
"IntelliSense"
("EXWM" "axwm")
("Emacs's" "emax's")
("BIDI" "bd")
("Perso-Arabic" "personal arabic")
"Persian"
"URL"
"HTML"
("vdo.ninja" "Video Ninja"))
"Commonly-misrecognized words or words that need special capitalization."
:group 'sachac
:type '(repeat (choice string
(repeat string))))
3. Making changes with substitute.el
I like using Prot's substitute.el.
I bind substitute-target-in-buffer to C-; . I can either replace the current word or select
a couple of words if I think I need to be more specific.
I ignore "SPEAKER_…" because then I can use the same shortcut to easily change speaker identification in transcripts.
(defvar sacha-substitute-save-changes "SPEAKER_"
"Non-nil means save changes to `sacha-subed-common-edits'.
If this is a string, it is treated as a regular expression, and only
strings that do NOT match this regular expression will get saved.
If this is a function, call it with the target string. It should return
non-nil if the substitution should be saved.")
(defun sacha-substitute-save-common-edits (target sub count scope)
"Keep track of changes so I can add them to `sacha-subed-common-edits'."
(when (cond
((functionp sacha-substitute-save-changes)
(funcall sacha-substitute-save-changes target))
((stringp sacha-substitute-save-changes)
(not (string-match sacha-substitute-save-changes target)))
(t
sacha-substitute-save-changes))
(customize-save-variable
'sacha-subed-common-edits
(cons (list sub target)
sacha-subed-common-edits))))
(use-package substitute
:bind ("C-;" . substitute-target-in-buffer)
:config
(setq substitute-fixed-letter-case t)
(add-hook 'substitute-post-replace-functions #'substitute-report-operation)
(add-hook 'substitute-post-replace-functions #'sacha-substitute-save-common-edits))
customize-save-variable is in charge of saving the changes after I replace something with susbtitute.el.
If I want to remove or edit entries, I can use M-x customize-variable to change sacha-subed-common-edits.
4. Correcting the errors
Then I have some functions that make the changes automatically or interactively.
;;;###autoload
(defun sacha-subed-fix-common-errors ()
(interactive )
(let (done entry correction)
(while (and
(not done)
(setq entry (sacha-subed-find-next-fix-point)))
(setq correction (if (listp entry) (car entry) entry))
(if (let ((case-fold-search nil))
(looking-at (regexp-quote correction)))
(forward-word 1)
(if (called-interactively-p 'any)
(let* ((c (read-char (format "%s (yn.): " correction))))
(cond
((= c ?y) (replace-match correction t t))
((= c ?n) (goto-char (match-end 0)))
((= c ?j) (subed-mpv-jump-to-current-subtitle))
((= c ?.) (setq done t))))
(replace-match correction t t))))))
;;;###autoload
(defun sacha-subed-fix-common-errors-from-start ()
"Change common errors in this buffer."
(interactive)
(goto-char (point-min))
(sacha-subed-fix-common-errors))
;;;###autoload
(defun sacha-subed-fix-common-errors-in-text (text)
"Change common errors in TEXT."
(interactive)
(with-temp-buffer
(insert text)
(goto-char (point-min))
(sacha-subed-fix-common-errors)
(buffer-string)))
;;;###autoload
(defun sacha-subed-find-next-fix-point ()
(when (re-search-forward
(format "\\_<\\(%s\\)\\_>"
(downcase
(string-join
(seq-mapcat
(lambda (o)
(if (listp o)
(if (string= (car o) "") (cdr o) o)
(list o)))
sacha-subed-common-edits)
"\\|")))
nil t)
(goto-char (match-beginning 0))
(seq-find
(lambda (o)
(if (listp o)
(seq-find (lambda (s)
(save-match-data
(string-match
(format "\\_<%s\\>" (downcase s))
(downcase (match-string 0))))) o)
(string= (downcase o) (downcase (match-string 0)))))
sacha-subed-common-edits)))
;;;###autoload
(defun sacha-subed-fix-common-error ()
(interactive)
(let ((entry (sacha-subed-find-next-fix-point)))
(replace-match (if (listp entry) (car entry) entry) t t)))
Then I can call sacha-subedf-fix-common-errors-in-text from my whisper-insert-text-at-point functions, and
sacha-subed-fix-common-errors-from-start from sacha-stream-prepare-transcript-for-editing.
5. Other ways to define the correction
Here's another function to define a correction if you don't want to use substitute.el.
;;;###autoload
(defun sacha-subed-add-common-edit (beg end replacement)
"Add this word to the misrecognized words."
(interactive
(let ((beg (if (region-active-p) (min (point) (mark))
(skip-syntax-backward "w")
(point)))
(end (if (region-active-p) (max (point) (mark))
(save-excursion (forward-word 1) (point)))))
(list beg end
(completing-read
(format "Replacement (%s): " (buffer-substring beg end))
(mapcar (lambda (o) (if (stringp o) o (car o))) sacha-subed-common-edits)))))
(customize-set-variable
'sacha-subed-common-edits
(cond
((member replacement sacha-subed-common-edits)
(cons (list replacement (buffer-substring-no-properties beg end))
(delete replacement sacha-subed-common-edits)))
((assoc replacement sacha-subed-common-edits)
(setcdr (assoc replacement sacha-subed-common-edits)
(append (list replacement) (cdr (assoc replacement sacha-subed-common-edits))))
sacha-subed-common-edits)
(t
(push (list replacement (buffer-substring-no-properties beg end))
sacha-subed-common-edits))))
(delete-region beg end)
(insert replacement))
At some point, I really should abstract this out to its own package, because it's just so useful. Other people have actually managed to get it to work, which is always a nice milestone. Anyway, here it is, in case it's helpful for you too!


