Monday, August 8, 2011

Emacs lisp functions for word lookup using web services

2011-08-08 Mon 19:47

Without any commentary about Xah Lee besides this preemptory caveat that there is alot of commentary about him, there are some useful functions from his website http://xahlee.org/emacs/emacs\_lookup\_ref.html. The two used here are both lookups based on the word under the cursor for either a definition or wikipedia.

In the original definition of his function lookup-word-definition the web service is hard-coded. In preparation for making the particular web service configurable we need to know what might be available and a sense of the API. Xah Lee also had a list of online dictionaries (http://xahlee.org/PageTwo\_dir/Vocabulary\_dir/dictionary\_tools.html) along with an example for the word 'curlicue'.

American Heritage Dictionary (AHD)
http://www.answers.com/main/ntquery?s=curlicue
AHD 2
http://education.yahoo.com/reference/dictionary/entry/curlicue
AHD 3
http://www.yourdictionary.com/curlicue
wiktionary
http://en.wiktionary.org/wiki/curlicue
AHD, Random House, …
http://dictionary.reference.com/browse/curlicue
Merriam Webster Collegiate
http://m-w.com/dictionary/curlicue
Longman
http://www.ldoceonline.com/search/?q=curlicue
Compact Oxford English Dictionary
http://www.askoxford.com/concise\_oed/curlicue
Oxford Advanced Learner's Dictionary
http://www.oxfordadvancedlearnersdictionary.com/dictionary/curlicue
Open Source Dictionaries
http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=curlicue
Google dictionary
http://www.google.com/dictionary?q=curlicue
  • "Google Dictionary is no longer available."

First we declare a new variable, tcb/lookup-word-definition-dictquery, that points to the dictionary and prepares the query under the assumption that the word to be found is the very last part of the string.

(defcustom tcb/lookup-word-definition-dictquery
  "http://m-w.com/dictionary/"
  "Dictionary and query API used in lookup-word-definition."
)

Now we define the function itself using the new variable.

(defun lookup-word-definition ()
  "Look up the current word's definition in a browser.
If a region is active (a phrase), lookup that phrase."
 (interactive)
 (let (myword myurl)
   (setq myword
         (if (and transient-mark-mode mark-active)
             (buffer-substring-no-properties (region-beginning) (region-end))
           (thing-at-point 'symbol)))

  (setq myword (replace-regexp-in-string " " "%20" myword))
  (setq myurl (concat tcb/lookup-word-definition-dictquery myword))

  (browse-url myurl)
  ;; (w3m-browse-url myurl) ;; if you want to browse using w3m
   ))

Another useful function is a wikipedia lookup.

(defun lookup-wikipedia ()
  "Look up the word under cursor in Wikipedia.
If there is a text selection (a phrase), use that

This command switches you to your browser."
 (interactive)
 (let (myword myurl)
   (setq myword
         (if (and transient-mark-mode mark-active)
             (buffer-substring-no-properties (region-beginning) (region-end))
           (thing-at-point 'symbol)))

  (setq myword (replace-regexp-in-string " " "_" myword))
  (setq myurl (concat "http://en.wikipedia.org/wiki/" myword))
  (browse-url myurl)
   ))

--
R|Burt〉