#!/usr/local/bin/perl # -*- Mode: Perl -*- # Author : Dirk Vleugels # Created On : Tue Apr 20 12:07:10 1999 # Last Modified By: Sven Uebelacker (chewy@mathematik.uni-ulm.de) # Last Modified On: Thu, 26 Jul 2001 19:54:00 +0200 # Language : CPerl # (C) by Author # Eine package deklaration definiet in Perl einen Namespace. # Dieser Namespace dient auch als Klassenname! package LEO_Parser; # Einbinden der Klassen HTML::{Parser,Entities} use HTML::Parser; use HTML::Entities; use vars qw (@ISA); # Globale variablen Deklaration @ISA=(HTML::Parser); # Wie erben von HTML::Parser. @ISA == 'is a' # Deklaration einiger lexikalischer Variablen my $tr = 0; my ($num_result, $in_result_table, $new_word) = 0; my @erg = (); # Ueberschreiben der Methoden start/end/text aus HTML::Parser sub start { my $class = shift; my ($tag, $attr, $attrseq, $origtext) = @_; return unless $in_result_table; return unless ($tag eq "td"); $new_word ++; # new table data $tr++; } sub end { my $class = shift; my $tag = shift; if ($in_result_table and ($tag eq "table")) { # we're done with all translation results $in_result_table --; } return unless $in_result_table; if ($tag eq "td") { $tr --; } } sub text { my $class = shift; my $text = shift; my $decode = HTML::Entities::decode($text); $decode =~ s/^\s+//; # strip leading $decode =~ s/\s+$//; # and trailing whitespace return if ($decode =~ /^\s*$/); # empty string? if ($decode =~ /(\d+)\ssearch res/) { $num_result = $1; $in_result_table ++; if ($num_result == 0) { print "No translation result\n"; exit (0); } return; } elsif ($decode =~ /no result/) { print "No translation result\n"; exit (0); } return unless $in_result_table; if ($new_word) { $new_word --; push @erg, $decode; } elsif ($tr > 0) { # continuation text $erg[$#erg] .= " $decode"; } } # Wechsel in den 'main' Namespace. Die Klasse LEO_Parser haette auch # in einer separaten Datei abgespeichert werden koennen, dann haette # sie allerdings per 'use LEO_Parser;' hier eingebunden werden muessen. package main; use strict; use LWP::UserAgent; use vars qw ( $w $t ); # Die Dokumentation zu diesen Klassen erhaelt man durch aufruf # von 'perldoc LWP::UserAgent' bzw. 'perldoc HTTP::Request' my $argument = shift or die "Need search arument!"; ######################################################### # alt: # my $leo = "http://dict.leo.org/cgi-bin/dict/dict-search.pl"; #neu: my $leo = "http://dict.leo.org/"; ######################################################### my $url = URI::URL->new($leo); my $ua = LWP::UserAgent->new; $url-> query_form ( 'NAME' => "dict", 'search' => $argument ); $ua -> agent ("LeoCrawl/0.001 ". $ua->agent); #$ua -> proxy (['http', 'ftp'], 'http://knabber.ping.de:3128/');# Set HTTP/FTP Proxy my $req = HTTP::Request->new (GET => $url->as_string); my $res = $ua->request ($req); if (! $res->is_success) { print "Couldn't connect to LEO Server\n"; exit (0); } my $tree = LEO_Parser->new(); $tree->parse ($res->content()) or die "Cannot parse HTML tree"; print "\n"; # Zeilenvorschub while (@erg) { $w = shift @erg; $t = shift @erg; write; } # Ausgabe Format Anweisung. Zwei Spalten, linksbuendig format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $w,$t .