#!/usr/local/bin/perl ########################################### # Mike Schilli, 2003 (m@perlmeister.com) ########################################### use warnings; use strict; use 5.008; use WWW::Mechanize; use HTML::TableExtract; use Log::Log4perl qw(:easy); use Pod::Usage; use URI::URL; use Getopt::Std; my $color = sub { "" }; eval { require Term::ANSIColor; Term::ANSIColor->import(); $color = \&color; }; getopts("sf", \my %opts); my $url_dir = ""; $url_dir = "frde" if $opts{f}; $url_dir = "esde" if $opts{s}; defined $ARGV[0] or pod2usage("No argument given"); my $word = "@ARGV"; my $agent = WWW::Mechanize->new( autocheck => 1 ); $agent->env_proxy(); $agent->get("http://dict.leo.org/$url_dir"); if (!defined $agent->form_name("dict")) { die "Change in Website layout"; } $agent->set_fields( 'search' => $word ); if($opts{f}) { $agent->set_fields( 'lang' => 'fr' ); } elsif($opts{s}) { $agent->set_fields( 'lang' => 'es' ); } else { $agent->set_fields( 'lang' => 'de' ); } $agent->click_button( number => 1); $agent->{content} = $agent->response()->decoded_content(); if($agent->{content} =~ /similar words/i) { my $ret = $agent->find_link(url_regex => qr/search=/); my($url, $text) = @$ret; INFO "Following $url"; $agent->follow_link(url => $url); $word = $text; } #my $te = new HTML::TableExtract(); #$te->parse( $agent->{content} ); #print $te->tables_dump(); my $te = new HTML::TableExtract( depth => 1, count => 5 ); $te->parse($agent->{content}); my %result = (); binmode STDOUT, ":utf8"; # Examine all matching tables foreach my $ts ($te->table_states) { my $count = 0; foreach my $row ($ts->rows) { next if $count++ < 2; next unless $row->[3]; next if $row->[1] =~ /^\s*$/; print $color->('blue'); print "$row->[1]\n"; print $color->('reset'); print " $row->[3]\n"; } } __END__ =head1 NAME leo - Look up a word or phrase on dict.leo.org =head1 DOWNLOAD _SRC_HERE_ =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS # German/English English/German leo word ... # German/French French/German leo -f word ... # German/Spanish Spanish/German leo -s word ... =head1 DESCRIPTION B contacts the dictionary server on dict.leo.org, fills in the form field with the provided word or phrase and presses the submit button. It analyzes the HTML result and extracts the relevant information. dict.leo.org handles both German/English, English/German, German/French, and French/German translations. So does C, you can specify German or English words/phrases on the command line. With the C<-f> option, it will translate between German/French and French/German. dict.leo.org handles spelling errors gracefully by presenting a list of links to similar spelled words. C handles this case by clicking on the first link presented and returning this link's result. =head1 CONTRIBUTORS Thanks to =over 4 =item * Sven Hergenhahn for providing a fix to make 'leo' work with the latest dict.leo.org page layout (01/04/2005) =item * Christian Moeller for a patch to adapt leo to Leo's new HTML table layout =item * Volker Mertens for adapting it to WWW::Mechanize 1.22 =item * Dirk große Osterhues for Spanish switches and Term::ANSIColor (made optional). =back =head1 EXAMPLES $ leo ludicrous ludicrous aberwitzig ludicrous albern ludicrous drollig ludicrous irrsinnig ludicrous lächerlich $ leo flabberkasted flabbergasted entgeistert flabbergasted verblüfft to be flabbergasted baff sein to be flabbergasted Bauklötze staunen [coll.][fig.] to be flabbergasted platt sein $ leo depp dork der Depp dumb cluck [sl.] der Depp [coll.] fool der Depp git [coll.] der Depp moron der Depp oaf der Depp plonker [Brit.][sl.] der Depp prick [coll.] der Depp [coll.] retard [sl.][pej.] der Depp [sl.][pej.] schnook der Depp Leo also supports French: $ leo -f meister champion  m. der Meister maître  m. der Meister C'est en forgeant qu'on devient forgeron. Übung macht den Meister. And Spanish: $ leo -s paella la paella [culin.] die Paella arroz a la valenciana [culin.] die Paella paella de bogavante [culin.] Paella mit Hummer If Term::ANSIColor is installed, the original will be printed in blue, the translations in black. Make sure you have a UTF-8 Terminal if you want accented characters to be printed correctly. =head1 LEGALESE Copyright 2003-2007 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR Mike Schilli