Get spoken definitions from the Meriam dictionary

| categories: emacs | tags:

Now that I can get Emacs to speak words , here is a new application of the idea. We use it to speak the definition of the word at point. We look up the definition here: http://www.dictionaryapi.com/account/index.htm

You may want to head straight to the video to see how this works here: https://www.youtube.com/watch?v=m529gXMrXZA

I had to get an API key for this. I suppose this key should be secret, but it could only be secure by obscurity in any kind of webapp and I don't anticipate using this much so here are the keys I got for the dictionary and thesaurus.

Key (Dictionary): 64f0950a-03b9-4315-9ba5-a73a964251ed Key (Thesaurus): ff0e39e2-b31f-4f17-833c-24e2875aad5d

(with-current-buffer
    (url-retrieve-synchronously
     (format
      "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/%s?key=%s"
      "synchronous"
      "64f0950a-03b9-4315-9ba5-a73a964251ed"))
  (buffer-substring url-http-end-of-headers (point-max)))
<?xml version="1.0" encoding="utf-8" ?>
<entry_list version="1.0">

: <entry id="synchronous"><ew>synchronous</ew><subj>AE-4b#CP-5#TL-5</subj><hw>syn*chro*nous</hw><sound><wav>synchr14.wav</wav><wpr>!siN-kru-nus</wpr></sound><pr>ˈsiŋ-krə-nəs, ˈsin-</pr><fl>adjective</fl><et>Late Latin <it>synchronos,</it> from Greek, from <it>syn-</it> + <it>chronos</it> time</et><def><date>1669</date> <sn>1</sn> <dt>:happening, existing, or arising at precisely the same time</dt> <sn>2</sn> <dt>:recurring or operating at exactly the same periods</dt> <sn>3</sn> <dt>:involving or indicating <fw>synchronism</fw></dt> <sn>4 a</sn> <dt>:having the same period</dt> <sd>also</sd> <dt>:having the same period and phase</dt> <sn>b</sn> <dt>:<sx>geostationary</sx></dt> <sn>5</sn> <dt>:of, used in, or being digital communication (as between computers) in which a common timing signal is established that dictates when individual bits can be transmitted and which allows for very high rates of data transfer</dt><ss>contemporary</ss></def><uro><ure>syn*chro*nous*ly</ure> <fl>adverb</fl></uro><uro><ure>syn*chro*nous*ness</ure> <fl>noun</fl></uro></entry> : <entry id="synchronous motor"><ew>synchronous motor</ew><subj>ME#EE</subj><hw>synchronous motor</hw><fl>noun</fl><def><date>1897</date><dt>:an electric motor having a speed strictly proportional to the frequency of the operating current</dt></def></entry>

</entry_list>

The idea is to query the url, get some xml back, and collect the definitions from it. Then, construct a string of the word, the number of definitions, then the definitions, and say it.

(defun speak-definition ()
  (interactive)
  (let* ((keyword (thing-at-point 'word))
         (api-key "64f0950a-03b9-4315-9ba5-a73a964251ed")
         (xml (with-current-buffer
                  (url-retrieve-synchronously
                   (format
                    "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/%s?key=%s"
                    keyword
                    api-key))
                (xml-parse-region url-http-end-of-headers (point-max))))
         (entries (xml-get-children (car xml) 'entry))
         (nentries (length entries))
         (defs (loop for entry in entries
                     collect (car (xml-get-children entry 'def))))
         (definition (format
                      "%s"
                      (concat
                       (format "%s has %s definition%s. "
                               keyword
                               nentries
                               (if (or (= 0 nentries)
                                       (> nentries 1))
                                   "s"
                                 ""))
                       (mapconcat
                        'identity
                        (loop for element in
                              (loop for def in defs
                                    collect (car (xml-get-children def 'dt)))
                              for i from 1
                              collect (format "%s %s" i (car (xml-node-children element))))
                        " ")))))
    (message definition)
    (do-applescript
     (format
      "say \"%s\"" definition))))
speak-definition

Let us try this out on a few words: asynchronous synchronous flibbity

I guess this would be helpful sometimes ;)

Copyright (C) 2015 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.10

Discuss on Twitter