Results 1 to 5 of 5

Thread: A command line Google Dictionary

  1. #1
    Join Date
    May 2008
    Beans
    8

    Smile A command line Google Dictionary

    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. It shows word meaning, usage, various meanings from different sources, pronunciation, synonyms.

    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.

    For more details:- http://linux-junky.blogspot.com/2010...e-version.html

    Code:
    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";
    }
    Paste this in /home/your_username/.bashrc file at the end. And now open new terminal and type $dict word

    to see the word meaning.
    To delete a word: $dictdelete word
    Last edited by Shadyabhi; October 29th, 2010 at 01:05 AM. Reason: Changed the script

  2. #2
    Join Date
    Jun 2006
    Location
    ga
    Beans
    426

    Re: A command line Google Dictionary

    This script is pretty cool. Thanks. I modified it so I only have to type 'd' since I'm a bit lazy.
    Last edited by Kingsley; October 29th, 2010 at 01:00 AM.
    The moon will illuminate my room and soon I'm consumed by my doom.

  3. #3
    Join Date
    May 2008
    Beans
    8

    Re: A command line Google Dictionary

    Just updated the script. Its better now

  4. #4
    Join Date
    Aug 2007
    Beans
    34
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: A command line Google Dictionary

    Instead of creating a file for each definition, I just fetch the text and display it. I've also added less to the end of the command for better viewing.

    Code:
    function define {
        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 | less
    }
    Thanks for the code!

  5. #5
    Join Date
    May 2008
    Beans
    8

    Smile Re: A command line Google Dictionary

    Quote Originally Posted by ncwilde43 View Post
    Instead of creating a file for each definition, I just fetch the text and display it. I've also added less to the end of the command for better viewing.

    Code:
    function define {
        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 | less
    }
    Thanks for the code!
    I too used less in my code. BTW, the sole reason for saving all my fetched words on the local folder was because I am on a slow connection so even fetching just text becomes expensive.

    So, in case, I forget the exact meaning of the word I learnt the other day, I could simply retrieve from the local file rather than downloading again from the web.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •