Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Grab Dictionary.com Definition

by mousey (Scribe)
on Aug 17, 2002 at 06:01 UTC ( #190822=snippet: print w/replies, xml ) Need Help??
Description: feed it a word, and it spits back the definition.
sub getWord {
  my ($word) = @_;
  my $content = get("http://www.dictionary.com/cgi-bin/dict.pl?term=$w
+ord");

  if ($content =~ /<!-- resultItemStart -->/) {
    my @content = '200';
    my $temp;
    while ($content =~ /<!-- resultItemStart -->/) {
      $content =~ s/.*?<!-- resultItemStart -->(.*?)<!-- resultItemEnd
+ -->//s;
      $temp = $1;
      while ($temp =~ /<(li|dd).*>.*<\/\1>/is) {
        $temp =~ s#.*?<(li|dd).*?>(.*?)</\1>##is;
        $_ = $2;
        s/(<.*?>|\&.*?\;)//sg;
        s/^\s+(.*)\s+$/$1/;
        push(@content, $_);
      }
    }
    return @content;
  } elsif ($content =~ /<!-- resultListStart -->/) {
    ($content) = $content =~ /<!-- resultListStart -->(.*?)<!-- result
+ListEnd -->/s;
    $content =~ s/<.*?>//gs;
    $content =~ s/^.*?Suggestions:\s+(.*?)\n\n.*$/$1/s;
    my @content = split(/\n/, $content);
    unshift(@content, '404');
    return @content;
  } else {
    return undef;
  }
}
Replies are listed 'Best First'.
•Re: Grab Dictionary.com Definition
by merlyn (Sage) on Aug 17, 2002 at 06:57 UTC
(jeffa) Re: Grab Dictionary.com Definition
by jeffa (Bishop) on Aug 17, 2002 at 15:00 UTC
    Not bad ... here is the one that i reinvented (before i discovered Net::Dict - merlyn++ yet again):
    use strict; use POSIX; use LWP::Simple; my $html = get('http://www.dictionary.com/search?q=' . shift); my ($def) = $html =~ /<!-- Content -->(.*)<!-- End content -->/s; $def =~ s/(<\/table>(?!.*<\/table>)).*$/$1/sg; print html2text($def); sub html2text { my $html = shift; my $tmp_file = tmpnam() . '.html'; open(FH,'>',$tmp_file) || die $!; print FH $html; close FH; my $text = `lynx -dump $tmp_file`; $text =~ s/References\n.*//s; unlink $tmp_file; return $text; }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-12-03 23:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?