# # A command line dictionary for UNIX
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

A command line dictionary for UNIX

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© September 2009 Girish Venkatachalam
by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:

http://gayatri-hitech.com
http://spam-cheetah.com

UNIX geeks belong to the command line world. They want to do everything from google searches to flickr photo uploads from the almighty UNIX command line. Since most websites nowadays have a REST API or similar this is very much possible and even desirable in several cases.

Google has released pygoogle for google searches from the command line and many options exist for accessing flickr photographs. Nearly every online service including rememberthemilk todo lists, delicious bookmarks and of course mail services like gmail include a way to access and modify your online storage from the command line.

In this article we are going to look at a tiny project to access the English dictionary from the command line. There is a C client invoked using the dict(1) command on UNIX boxes. The dictionary protocol is a very simple text based protocol described in RFC2229.

I wrote this Python script just for fun. I feel that both the dictionary protocol and Python are useful tools to learn protocols and programming respectively. Especially if you are a beginner.

#!/usr/bin/env python

import socket,sys,re

if len(sys.argv)==1:
 sys.exit("Usage: " + sys.argv[0] + "  [dictionary server]")


pat = re.compile("250 ok")
pat2 = re.compile("\.|2\d\d");
pat3 = re.compile("1\d\d")
errpat = re.compile("552 no match");
errpat2 = re.compile("551");
#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the dictionary server on port 2628
# By default it connects to www.dict.org unless overridden by the
# command line
if len(sys.argv) == 3:
  s.connect((sys.argv[2], 2628))
else:
  s.connect(("www.dict.org", 2628))
s.send("define * " + '"' + sys.argv[1].strip() + '"' + "\r\n\r\n")
buf = ''

while 1:
    tmpbuf = s.recv(1024)
    if not tmpbuf:
        break
    buf += tmpbuf
    if errpat.search(tmpbuf):
       sys.stdout.write("No definitions found for " + '"' +
sys.argv[1].strip() + '"')
       s.send("match * lev " + '"' + sys.argv[1].strip() + '"' +
"\r\n\r\n")
       
       res = s.recv(80)
       if errpat.search(res) or errpat2.search(res):
         print
         s.send("quit\r\n")
         s.close()
         sys.exit()
       else:
         sys.stdout.write(", perhaps you mean:")
         suggestions = s.recv(2048)
         source = ''

         for line in suggestions.splitlines():
           if source != '':
              prev = source[0]
           else:
              prev = ''
           if pat.match(line) or pat2.match(line):
             break
           source = line.split(' ')
           if prev == source[0]:
             sys.stdout.write('  ' + source[1].strip('"'))
           else:
             sys.stdout.write('\n' + source[0] + ':' + '  ' +
source[1].strip('"'))
         print

         s.send("quit\r\n");
         s.close()
         sys.exit()
    if pat.search(tmpbuf):
        break


for line in buf.splitlines():
      if pat2.match(line):
        continue 
      if pat3.match(line):
        if line.find('" ') != -1:
          a = line.split('" ')
          print
          b = str(a[1]).split(' ');
          c = ' '.join(b[1:])
          d = c.split('"')
          print "From " + d[1] + ' ' + '[' + b[0] + ']' + ":\n" 
        else:
          a = line.split(' ')
          a[3] = "found"

          print ' '.join(a[1:]) 
        continue
      sys.stdout.write('  ' + line + "\n")
    
s.send("quit\r\n");
s.close()

Copy paste the above python code as dict.py and give execute permissions.

$ chmod +x dict.py
 

Now all you have to do to lookup a word is type this on the UNIX command line:

$ dict.py programming
 

This will lookup a dictionary server from the Internet and print the meanings along with thesaurus lookups to the console. You might wish to use a pager program like less(1) or redirect the output to a file.

Learning is easy when things are kept simple. Once we master the basics we can over time graduate to master advanced concepts. Happy programming.

References

  1. The dictionary protocol

running cheetah
SpamCheetah
Stop spam dead in its tracks!

If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> A command line dictionary for UNIX

4 comments


Inexpensive and informative Apple related e-books:

Take Control of IOS 11

Photos: A Take Control Crash Course

Take Control of the Mac Command Line with Terminal, Second Edition

iOS 8: A Take Control Crash Course

Take Control of Parallels Desktop 12





More Articles by © Girish Venkatachalam







Mon Sep 21 20:53:15 2009: 6950   TonyLawrence

gravatar
That's fun.


I still don't like Python, though :-)



Tue Sep 22 13:11:53 2009: 6952   BruceGarlock

gravatar
OK, who's gonna port it to perl :-)n Thanks very much for this. Learning Python has been on my todo list for sometime now, and this is a great beginner script that covers a lot of ground.



Fri Jan 15 10:53:13 2010: 7911   anonymous

gravatar


Thank you very much, I learnt a lot from it. And it makes me want to study Python, beautiful language.



Fri Jun 21 08:12:07 2013: 12141   emzap79

gravatar


really cool stuff, even if I don't have a clue how this script is working since I'm not confirm with python yet. What would I have to change to adapt this script to openthesaurus.de? Substituting www.dict.org with openthesaurus.de didn't work out for me? Since my motherlanguage is german I'd be more than happy to see this script working with a german thesaurus.

------------------------


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





The best of us would rather be popular than right. (Mark Twain)




Linux posts

Troubleshooting posts


This post tagged:

Girish

Programming



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode