Google Dictionary - Command line version..

Published: by

  • Categories:

EDIT: Not working now due to change from the Google's side. Will fix it soon. :)

Well, this is not done using google's API. Its a simple bash script that takes a word to be searched as parameter…

Its a very simple bash script, nothing fancy.

Steps involved in fetching the meaning:-

  1. Use curl to fetch the page's HTML
  2. Pipe it to html2text that converts to text.
  3. The returned test is saved to $HOME/dict`.
  4. Also. a history file is made named $HOME/dict/dicthistory

The script saves all the meanings in /dict folder in home directory. If the folder is not present, then its created. To search for a word simply do "$dict word" & to delete a word, do "$dictdelete wird".

dict() { 
if [ ! -d $HOME/dict ];then 
mkdir $HOME/dict;
fi
cat $HOME/dict/dicthistory | grep " $1$" 1>/dev/null 2>/dev/null;
if [ $? -eq 0 ]
then
 less $HOME/dict/$1;
 return;
fi
 echo `date`" -> ""$1" >> $HOME/dict/dicthistory;
 curl -s "http://www.google.com/dictionary?aq=f&langpair=en|en&q="$1"&hl=en" | html2text -nobs | sed '1,/^ *Dictionary\]/d' | head -n -5 > $HOME/dict/$1;
 stat_data=`stat $HOME/dict/$1 -t | awk '{print $2}'`
 echo -n "Bytes transferred: "
 echo $stat_data
 sleep 1;
 less $HOME/dict/$1;
 echo "::Last searched words.."
 cat $HOME/dict/dicthistory;
}
dictdelete()
{
 rm $HOME/dict/$1;
 cat $HOME/dict/dicthistory | grep -v " $1$" > $HOME/dict/dicthistory.bk;
 mv $HOME/dict/dicthistory.bk $HOME/dict/dicthistory;
 echo ":: Requested item $1 successfully deleted";
}

I appended the above code to my ~.bashrc file.