Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

webster client

by gregorovius (Friar)
on May 04, 2000 at 21:07 UTC ( #10239=sourcecode: print w/ replies, xml ) Need Help??

Category: web robots
Author/Contact Info Claudio Garcia
claudio.garcia@stanfordalumni.org
Description: This is a simple web client that will bring a word definition from the UCSD web dictionary into your X terminal.

If people are interested I also have a server-simpler client version that allows this program to be deployed on a large number of machines without having to install the LWP and HTML packages it uses on each of them.
Usage: % webster_client word
#!/usr/bin/perl -w

use HTML::TreeBuilder;
use HTML::FormatText;
use LWP::UserAgent;

my $ua = new LWP::UserAgent;
$ua->agent("Dictionary");

#
# Uncomment if behind a firewall
#
#$ua->proxy([ 'http' ], 'http://www-dms.esd.sgi.com:8080/');

#
# Create a new request.
#
my $req = new HTTP::Request GET => "http://work.ucsd.edu:5141/cgi-bin/
+http_webster?isindex=$ARGV[0]&method=exact";

#
# Pass Request to the user agent and get a response back.
#
my $res = $ua->request($req);

#
# Print outcome of the response.
#
if(! $res->is_success) {
  print "Failure to connect to server: " . $res->message;
} else {
  my $html = $res->content;
  my $p = HTML::TreeBuilder->new;
  $p->parse($html);
  my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin =
+> 60);
  my $result = $formatter->format($p);
  
  my @paragraphs = split /^\s+/m, $result;

  #
  # Print only the paragraphs where the word definition is
  #
  for(my $x=2; !($paragraphs[$x] =~ /^From WordNet/) and $x < 10; $x++
+) {
    print $paragraphs[$x]."\n";
  } 
}

Comment on webster client
Download Code
Replies are listed 'Best First'.
RE: webster client
by Anonymous Monk on May 17, 2000 at 10:59 UTC
    This looks great! One question though, would it be possible to modify this for a foreign language dictonary? I'd love to be able to type check dog and have it give me from an online german dictonary der Hund, -e
      Sure! I have a Spanish-English version already. If you point me to an on-line english-german dictionary I'll make it work with it.(or you may do it yourself and post the new version back)
        A german-english dictionary is avaible at dict.leo.org
        This is the german dictionary I prefer. Providing as much information on the word as possible would be the most useful, such as article, the plural form, etc etc. I'll take a look at it myself and post my results, but I'd love to see your spanish port.
        http://www.quickdic.de/e_dic.html

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://10239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2015-07-29 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls