@shijitht

July 29, 2010

Create an offline command line dictionary using dict

Filed under: Perl — shijitht @ 9:35 pm

Most of the dictionary in our OS have online database and most of us don’t have a reliable internet connection. And the online database could be down when we need something urgent. A solution to it is to create an offline database from the online one you line. We need two things to start the process,

1) A file containing almost every English word (get online).
2) Application named dict which could be accessed from terminal as ” dict -d wn apple “.
It will show the meaning of apple from WordNet(wn) .So -d option is to select WordNet database (choose whatever meets your demand).

Now we are ready.
Connect to the net and run the following script


open(fr,"words");
open(fw,">>meanings");
@words = <fr> ;
$len = @words;
for($i=0;$i<$len;$i++) {
    $word = @words[$i];
    chop($word);
    system("dict -d wn $word > file 2>&1");
    open(fp,"file");
    my @file = <fp>;
    my @first = split(/ /,@file[0]);
    @first = split(//,@first[0]);
    if(@first[0] eq 'N') {
        print fw (">>".@words[$i]." - Meaning not present\n");
    } elsif(@first[0] eq 'C'){
        $i--;
    } else{
        $j = 4;
        print fw (">".@words[$i]."\n");
        while(@file[$j++]) {
            print fw (@file[$j]);
        }
    }
    undef @file;
    undef @first;
    close(fp);
}
close(fr);
close(fw);

This will create a file named meanings. It carries the meaning of all words in the file words.
So file meanings is our offline database. Copy the file meanings to a directory .dictionary in home.
The next script is to get the meaning of a word from the database we just created.


#! /usr/bin/perl
$file = "$ENV{'HOME'}/.dictionary/meanings";
$tmp =  "$ENV{'HOME'}/.dictionary/tmp";
open(fr,"$file");
@meaning = <fr>;
$word = lc($ARGV[0]);
$a = system("grep -n -w '>$word' $file > $tmp");
if($a == 0) {
	open(fw,"$tmp");
	$line = <fw>;
	@line = split(":",$line);
	$line_no = @line[0];
	while(@test[0] ne ">") {
		print @meaning[$line_no++];
		@test = @meaning[$line_no];
		@test = substr(@test[0],0,1);
	}
} else {
	print "Not present\n";
}
close(fr);
close(fw);

Make the script executable and run it by giving word as argument. Copy the above script to bin and make it command.
I give it the name d and use as “d apple” to show its meaning.
You can automate the above process using Makefile.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: