3

The question is in the title.

  • Can yes-or-no-p be made to accept strings like " YES " and "No", not just "yes" and "no"?

---+ Background:

Standard yes-or-no-p accepts only "yes" or "no", and gives errors for other strings.

Unfortunately, when I use Dragon dictation and voice control with emacs, if I am answering a yes-or-no-p question, frequently Dragon will emit 1 or 2 extra spaces before, and sometimes capitalize.

I have seen Dragon send strings to yes-or-no-p like "Yes", " no", " NO "...

Currently I have to dictate something like "nospace yes press enter" to answer an emacs yes-or-no-p prompt. This can be annoying.

This problem does not arise with so-called speech-aware apps, where the app can inform Dragon that it is at the end of a sentence, or at the beginning of a line with no previous word on the same line, etc. Unfortunately, emacs is not speech-aware.

Q: what is the best way to fix this problem, so that yes-or-no-p accepts strings like "Yes", " no", " NO "... ?


Posting to https://emacs.stackexchange.com because the answer is almost certainly to write elisp code, since emacs is much more flexible than Dragon.


I will provide my own current answer separately, but of course I will approve better suggestions.


This is one of the of what will be several questions related to making emacs friendlier to speech recognition and voice control software. In particular Dragon speech recognition software from Nuance (now owned by Microsoft), because that is what I'm currently using, but I'm open to other speech recognition and voice control software packages.

2
  • I added speech-recognition as a tag (I thought emacs- is redundant here, but if you disagree let me know). I'll add some verbiage to the tag info eventually, but I think it's clear enough for now.
    – NickD
    Jan 12 at 12:35
  • Thanks, and no worries: clearly emacs- is redundant here. On my wiki where I discussed such issues (user interface and other) I tag it separately emacs and speech-recognition -- although I probably should tag it ` voice-control`. there is a difference between speech recognition and voice control, although it's fuzzy. Voice-control is for user interface features built upon speech recognition; speech-recognition is the actual accuracy of the algorithms, choosing command words that are suitably distinct, etc.
    – Krazy Glew
    Jan 12 at 19:28

2 Answers 2

1

Here is my current answer - a defalias substitution for yes-or-no-p

    (defun dragon-speech-tolerant-yes-or-no-p (prompt)
      "PROMPT yes/no, tolerating case and extra blank space
    that may have been emitted by Dragon speech recognition."
      (catch 'return
        (while t
          (let ((answer (read-string prompt)) )
        ;; let within let because want to be able to
        ;; switch-buffer out of read-string
        ;; with original value of case-fold-search
        (let ((case-fold-search t))
          (cond
            ((string-match "^[ ]*YES[ ]*$" answer) (throw 'return t))
            ((string-match "^[ ]*NO[ ]*$"  answer) (throw 'return nil))
            ))
        ;; loop, because EMACS doesn't do TCO (Tail Call Optimization)
        (message "Please answer yes or no, not '%s'" answer)
        (sit-for 2)
        ))
        ))
    
    (defalias 'yes-or-no-p 'dragon-speech-tolerant-yes-or-no-p)

With this I can answer a yes-or-no-p prompt by saying

  • "yes" followed by "press enter"

Which may not seem all that much of an improvement over the existing

  • "nospace yes press enter"

But it is considerably less frustrating.


I can go further, and have my-version-of-yes-or-no-p inform Dragon that it is running (by modifying the OS window / Emacs frame title), and then defining Dragon speech commands "yes" and "no" that operate only in that context, and emit only "yes" and "no".

By doing that I can eliminate the need to say "press enter" or "newline" will answer a yes-or-no-p question, and simply say "yes" or "no".

However, I am of mixed mind about this. Partly because I think it's a bit of a kluge to use the window/frame title to inform Dragon of emacs context. But also because there are so many other places that I have to say "press enter", that it is a surprising inconsistency not to, and sometimes I say it anyway and get an extra newline in the emacs buffer I was working in. I suspect that I'll use the slightly more verbose form for the moment, and go for the shorter form after I have used it for enough time that I get frustrated by saying the extra words.

1

I don't know if this is helpful, but you can use y-or-n-p globally for one-character responses:

Prior to Emacs 29:

(defalias 'yes-or-no-p 'y-or-n-p)

Emacs 29 and later:

(setopt use-short-answers t)

It may or may not be useful to know that y-or-n-p accepts SPC as well as y for confirmation. Similarly, it accepts DEL as well as n for denial.

2
  • Thanks, yes, the emacs wiki has lots of examples of mapping yes-or-no-p to y-or-n-p. <br/> However, I much prefer saying words like "yes" or "no" than a doing saying letters like Y or N - and I really, really, dislike saying numbers, e.g. for those packages that number options to allow you to choose. <br/> So even though saying "press Y" is shorter than saying "yes, press enter", I prefer the word "yes". And I suspect I will soon eliminate the "press enter" part. <br/> In fact, I suspect that I will go the other way map y-or-n-p to a version of yes-or-no-p.
    – Krazy Glew
    Jan 23 at 23:36
  • I had forgotten that y-or-n-p accepted space/delete as well as y/n. Recalls my glory days when I was a very fast emacs keyboard user. <br/> Emacs has a very powerful keyboard interface. Many packages have been optimized for keyboard, and pessimized for speech recognition. E.g. all of the toggle and cycle commands, in org-mode and elsewhere. Toggle saves a key binding, but as a voice control user it is preferable to have commands that explicitly turn a feature on or off. Toggle is unnatural for a speech interface, but efficient for a keyboard interface.
    – Krazy Glew
    Jan 23 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.