9 Comments

Command Line Dictionary

If you are a regular user of the command line, a command line dictionary and thesaurus can come in very handy. I like to use a command line dictionary because: 1) You don’t have to navigate several server pages, or a drop box before entering a search; 2) You don’t have to be on the internet to do a search.

Sdcv

StarDict has created a command line-only version of their client that (like their full-featured version) has access to a host of different dictionaries. I’d been hassling with my own scripts creating a command line dictionary (you’ll know if you read this post previously) so many thanks to StarDict for making this alot easier.

Sdcv is in most distributions’ repositories so add it however you need to with your package manager. Then (if your repositories have them) install the dictionaries you need (this post focuses on the English WordNet and Moby Thesaurus dictionaries). In Arch Linux you can do this by:

pacman -S sdcv
yaourt -S stardict-wordnet stardict-moby-thesaurus

If your repositories don’t have them, you’ll need to add manually. You can do this by going to the StarDict files page and downloading them. Then extract the archive and place them in the StarDict ‘dic’ folder. For example:

tar xvf stardict-dictd-moby-thesaurus-2.4.2.tar.bz2
mv stardict-dictd-moby-thesaurus-2.4.2/* /usr/share/stardict/dic/

The folder may be different depending on your distro so don’t just run this command :).

To use sdcv you need to specify the dictionary:

sdcv -u WordNet hello
sdcv -u "Moby Thesaurus II" hello

You can list the dictionaries with the ‘sdcv -l’ command.

Sdcv Scripts

To ease the process of entering the command and to give better formatting, I’ve created a couple scripts. You’ll need aspell installed (on most distributions it already is) of your language type to give misspelled word suggestions.

Define

#!/bin/bash
# define - command line dictionary

# Program name from it's filename
prog=${0##*/}

# Text color variables
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) #     red
bldwht=${txtbld}$(tput setaf 7) #     white
txtrst=$(tput sgr0)             # Reset
warn=${bldred}!${txtrst}

sedbldwht='\c[[1;37m'           # Sed white  - bold
sedtxtrst='\c[[0m'              #     text reset

# Display usage if full argument isn't given
if [[ -z "$@" ]]; then
  echo " $prog <word-to-lookup> - command line dictionary"
  exit
fi

# Suggest possible words if not in dictionary, otherwise define
wordchecknum=$(echo "$1" | aspell -a | sed '1d' | wc -m)
wordcheckprnt=$(echo "$1" | aspell -a | sed '1d' | sed 's/^.*: //')

if [[ $wordchecknum -gt "3" ]]; then
  echo -e "$warn ${bldwht}"$1"${txtrst} is not in the dictionary.  Possible alternatives:"
  echo -e "\n$wordcheckprnt" | fmt -w 76 | sed 's/^/  /g'
  echo
  exit
fi

# Lookup word and reformat/highlight
sdcv -u "WordNet" $1 | \
# lookup, delete extrenous first lines, delete last empty line
sed '1,3d' | sed '/^*$/d' | \
# print more obvious word type
sed "h; :b; \$b ; N; /^${1}\n     n/ {h;x;s// ${sedbldwht}Noun${sedtxtrst}\n/; bb}; \$b ; P; D" | \
sed "h; :b; \$b ; N; /^     v/ {h;x;s// ${sedbldwht}Verb${sedtxtrst}\n/; bb}; \$b ; P; D" | \
sed "h; :b; \$b ; N; /^     adv/ {h;x;s// ${sedbldwht}Adverb${sedtxtrst}\n/; bb}; \$b ; P; D" | \
sed "h; :b; \$b ; N; /^     adj/ {h;x;s// ${sedbldwht}Adjective${sedtxtrst}\n/; bb}; \$b ; P; D" | \
# reformat to left (ok, there's gotta be a better way to do this)
sed 's/^     / /g' | \
sed 's/^      /    /g' | \
sed 's/^      /    /g' | \
sed 's/^     /    /g' | \
# rename single entry for conformity
sed 's/^ : / 1: /g' | sed "/^${1}.*/d" | sed "s/^ \[/    \[/g" | \
# and coloring
sed "s/${1}/${sedbldwht}${1}${sedtxtrst}/g"

The WordNet print formatting isn't the best and I used sed quite a bit to get it in decent shape. Anyone that can use sed more efficiently or know of a better way to do this please let me know.

Thes

#!/bin/bash
# thes - command line thesaurus

# Program name from it's filename
prog=${0##*/}

# Text color variables
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) #  red
bldwht=${txtbld}$(tput setaf 7) #  white
txtrst=$(tput sgr0)             # Reset
warn=${bldred}!${txtrst}

# Display usage if full argument isn't given
if [[ -z "$@" ]]; then
  echo " $prog <word> - command line thesaurus"
  exit
fi

# Suggest possible words if not in dictionary, otherwise define
wordchecknum=$(echo "$1" | aspell -a | sed '1d' | wc -m)
wordcheckprnt=$(echo "$1" | aspell -a | sed '1d' | sed 's/^.*: //')

if [[ $wordchecknum -gt "3" ]]; then
  echo -e "$warn ${bldwht}"$1"${txtrst} is not in the dictionary.  Possible alternatives:"
  echo -e "\n$wordcheckprnt" | fmt -w 76 | sed 's/^/  /g'
  echo
  exit
fi

# Lookup word and reformat/highlight
sdcv -u "Moby Thesaurus II" $1 | \
# lookup, delete extrenous first lines, delete last empty line
sed '1,3d' | sed '2d' | sed '/^*$/d' | \
# delete indent to reformat, add newlines at 76th character, and reident
sed 's/^   //g' | fmt -w 76 | sed 's/^/  /g'

About these ads

About Gen2ly

<3's linux

9 comments on “Command Line Dictionary

  1. A really simple way of doing it is this:

    function spell {
    echo $@ | ispell
    }

    Then you just go:
    $ spell tank
    @(#) International Ispell Version 3.2.06 08/01/01
    word: ok

    word:

  2. Thats a looks like a good way to check spelling. Unfortunately, I don’t have the know how to be able to put that into the script. I did however learn that aspell (default for Gnome installations) can do the task too.

    echo “$@” | aspell -a –lang=en_US

  3. If you put the aforementioned code into say ~/.bashrc then you can run it the way I posted. Or use aspell as you amended.

  4. Hi,
    Needed to modify the script a bit to get it working::

    #!/bin/bash
    elinks -dump -no-references http://wordnet.princeton.edu/perl/webwn?s=$1 | grep -i -w –colour=always -B 2 -A 5 $1 | sed ‘$d’

    As you can see I use elinks so I needed to add -no-references. Also note that searching for the plural form of a word might skip some results (since the dictionary might return the singular form, which wouldn’t match the regexp).

    Thanks!

  5. If I invoke Wordnet from the terminal, I can say, for instance

    wn rock -over

    and get a definition of rock that includes 7 noun senses and 1 verb sense. I’m going to guess that the above confusion about WordNet had to do with (A) it has this odd property where it wants to see the word to be defined before any command line arguments and (B) you didn’t install the terminal interface to it. Also, the documentation is geared toward other uses so the very simple “-over” to look up meanings of words is kind of buried.

  6. Ah, thanks zorg, yeah the documentation for ‘wn’ was a bit overwhelming and I missed that bit. Actually seems like a decent way to do things.

  7. Good stuff.

    Please fix the tagline above ;-)

    It’s its, not it’s; it’s is the shortened form of it is.
    The letter’s place is its place.

  8. [...] stardict : it has a command line client too https://linuxtidbits.wordpress.com/2008/01/30/command-line-dictionary/ [...]

  9. I have been exploring for a bit for any high quality articles or blog posts on this sort of space . Exploring in Yahoo I ultimately stumbled upon this website. Studying this info So i am glad to convey that I’ve an incredibly excellent uncanny feeling I found out exactly what I needed. I most surely will make certain to don?t put out of your mind this website and give it a look on a constant basis.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: