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\_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 (\_dir/Vocabulary\_dir/dictionary\_tools.html) along with an example for the word 'curlicue'.

American Heritage Dictionary (AHD)
AHD, Random House, …
Merriam Webster Collegiate
Compact Oxford English Dictionary\_oed/curlicue
Oxford Advanced Learner's Dictionary
Open Source Dictionaries*&Query=curlicue
Google dictionary
  • "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
  "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."
 (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."
 (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 "" myword))
  (browse-url myurl)


Friday, July 15, 2011

Testing org-googlecl after host replacement

For the last 18 months the home computer network, and especially my own machine, have deteriorated to agonizing usability. In these past few days I have enlivened the systems and things are running well. This particular post is a test of the blogging capability from emacs orgmode that is made possible by the wonderful package org-googlecl and googlecl.


Saturday, March 12, 2011

Access to URL has been blocked over 3G because of an account filter

2011-03-11 FRI 11:56

If I ever get the message "Access to URL has been blocked" surfing the web over 3G, I'll call customer service to ask if the phone number has filters applied. Here is the short story that leads to this conclusion.

We have a Samsung Captivate smartphone running the Android v2.1 operating system. Browsing the internet worked over both wireless and the 3G data network. Then came a day when 3G browsing no longer worked but wireless did. Neither research nor 'random walks through solution space' (as a friend is wont to say) yielded a solution so we went to the AT&T store for help. Even though they had not heard of this problem they were helpful and attempted some resolution to no avail.

After 20 minutes of failed attempts they still did not give up but rather brainstormed and checked assumptions. One such assumption was that the phone account itself had the proper permissions to browse the web over 3G. Since this capability worked before and we had not requested it to be disabled, it didn't seem likely that this was a problem. However, a call to customer service revealed that the phone line was indeed filtered in a way to block data, including web access, over 3G. This configuration was incorrect and never requested, but a plausible explanation is that when we requested data blocking for our daughter's line my wife's line was inadvertently blocked as well.

So, if you are getting the "Access to URL has been blocked" message, see if the line is somehow filtered.

Troubleshooting Notes

Here are some troubleshooting observations and actions we tried that failed.

  • All internet activity was successful when connected to a wireless network.
  • Some internet activity was successful when not connected to wireless but going through 3G
    • Examples are weather, local news
    • These activities had a phone application associated with them
  • Reset browser settings
  • Put into flight mode for a few minutes then return to normal mode
  • Remove battery then put it back
  • Use a different browser


Wednesday, January 19, 2011

How do I rename multiple files efficiently using only the bash shell?

2010-09-10 Fri 17:36

As a concrete example, let us consider the problem that we often have uploading pictures to certain sites (e.g. ebay). The photos from our camera all have .JPG extension. However, when we attempt to locate those pictures from some web sites, no photos are found. This is because something in the chain of events only lower case extensions, such as .jpg, are allowed.

The task is to change all files in a directory (e.g. /DATA01/Pictures/EBAY/2010-09-10) from upper case (.JPG) to lower case (.jpg) extensions.

cd /DATA01/Pictures/EBAY/2010-09-10
for i in *.JPG; do mv "$i" "${i/.JPG}".jpg; done


If the task is a bit more complex will do well to use another standard Unix tool with regular expression capability (such as sed). For example, if we have files with a date in the name (e.g. 20101210) and want to change the date (e.g. to 20110119), then here is solution.

for i in *20101210*; do j=`echo $i | sed s/20101210/20110119/`; mv $i $j; done

Still simple.