| 1 | # Translator.pm | ||
| 2 | # by Jim Smyser | ||
| 3 | # Copyright (C) 2000 by Jim Smyser | ||
| 4 | # $Id: Translator.pm,v 1.02 2000/06/08 11:09:07 jims Exp $ | ||
| 5 | |||
| 6 | |||
| 7 | package WWW::Search::Translator; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | WWW::Search::Translator - class for Translating languages | ||
| 12 | |||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use WWW::Search; | ||
| 17 | %opts = ( | ||
| 18 | lp => param(en_de), | ||
| 19 | ); | ||
| 20 | $query = "a whole bunch of english text to be translated to german"; | ||
| 21 | my $search = new WWW::Search('Translator'); | ||
| 22 | $search->native_query(WWW::Search::escape_query($query),\%opts); | ||
| 23 | while (my $result = $search->next_result()) | ||
| 24 | { | ||
| 25 | $p = $result->raw; | ||
| 26 | print "$p"; | ||
| 27 | } | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This is a simple no thrills class enabling users to translate | ||
| 32 | text through AV Translations F<http://babel.altavista.com>. | ||
| 33 | This translating via this method seems much faster than thru | ||
| 34 | browser interface. Makes a neat addition to any web page. | ||
| 35 | Translated text is returned thru $result->raw, there is no | ||
| 36 | url's or title $results like typical WWW::Search results, just | ||
| 37 | a stream of text that was translated from the query text entered. | ||
| 38 | |||
| 39 | SEE TRANSLATION OPTIONS and TIPS. | ||
| 40 | |||
| 41 | This class exports no public interface; all interaction should | ||
| 42 | be done through WWW::Search objects. | ||
| 43 | |||
| 44 | =head1 TRANSLATION OPTIONS | ||
| 45 | |||
| 46 | Pass either of the below listed value options along with the | ||
| 47 | query. There is only one $result returnd by this backend: raw. | ||
| 48 | Thus, translated text will be returnd via $result->raw | ||
| 49 | |||
| 50 | <option value="en_fr" >English to French</option> | ||
| 51 | <option value="en_de" >English to German</option> | ||
| 52 | <option value="en_it" >English to Italian</option> | ||
| 53 | <option value="en_pt" >English to Portuguese</option> | ||
| 54 | <option value="en_es" >English to Spanish</option> | ||
| 55 | <option value="fr_en" >French to English</option> | ||
| 56 | <option value="de_en" >German to English</option> | ||
| 57 | <option value="it_en" >Italian to English</option> | ||
| 58 | <option value="pt_en" >Portuguese to English</option> | ||
| 59 | <option value="es_en" >Spanish to English</option> | ||
| 60 | <option value="de_fr" >German to French</option> | ||
| 61 | <option value="fr_de" >French to German</option> | ||
| 62 | <option value="ru_en" >Russian to English</option> | ||
| 63 | |||
| 64 | =head1 TIPS | ||
| 65 | |||
| 66 | I would use textarea for both user input and printing | ||
| 67 | $result->url into. This way users can scroll the returned | ||
| 68 | text and have large area to type/paste text for input. | ||
| 69 | |||
| 70 | =head1 CHANGES | ||
| 71 | |||
| 72 | VERSION 1.02 | ||
| 73 | |||
| 74 | Got time to go back and look at what was needed to parse multi | ||
| 75 | lines of the text area. Everything looks OK now and this | ||
| 76 | backend will parse single and multi lines of translated text. | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | C<WWW::Search::Translator> is written and maintained | ||
| 81 | by Jim Smyser - <jsmyser@bigfoot.com>. | ||
| 82 | |||
| 83 | =head1 COPYRIGHT | ||
| 84 | |||
| 85 | WWW::Search Copyright (c) 1996-1998 University of Southern California. | ||
| 86 | All rights reserved. | ||
| 87 | |||
| 88 | THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
| 89 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
| 90 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 91 | |||
| 92 | |||
| 93 | =cut | ||
| 94 | #' | ||
| 95 | |||
| 96 | ##################################################################### | ||
| 97 | require Exporter; | ||
| 98 | @EXPORT = qw(); | ||
| 99 | @EXPORT_OK = qw(); | ||
| 100 | @ISA = qw(WWW::Search Exporter); | ||
| 101 | $VERSION = '1.02'; | ||
| 102 | |||
| 103 | use Carp (); | ||
| 104 | use WWW::Search(qw(generic_option strip_tags)); | ||
| 105 | use URI::Escape; | ||
| 106 | |||
| 107 | require WWW::SearchResult; | ||
| 108 | |||
| 109 | sub native_setup_search { | ||
| 110 | |||
| 111 | my($self, $native_query, $native_options_ref) = @_; | ||
| 112 | $self->{_debug} = $native_options_ref->{'search_debug'}; | ||
| 113 | $self->{_debug} = 2 if ($native_options_ref->{'search_parse_debug'}); | ||
| 114 | $self->{_debug} = 0 if (!defined($self->{_debug})); | ||
| 115 | $self->{agent_e_mail} = 'jsmyser@bigfoot.com'; | ||
| 116 | $self->user_agent('user'); | ||
| 117 | $native_query =~ s/\n+/ /g; | ||
| 118 | if (!defined($self->{_options})) { | ||
| 119 | $self->{_options} = { | ||
| 120 | 'search_url' => 'http://babel.altavista.com/translate.dyn', | ||
| 121 | 'urltext' => $native_query, | ||
| 122 | 'user' => 'avworld', | ||
| 123 | 'lp'=> 'en_fr' # default | ||
| 124 | }; | ||
| 125 | } | ||
| 126 | my $options_ref = $self->{_options}; | ||
| 127 | if (defined($native_options_ref)) | ||
| 128 | { | ||
| 129 | # Copy in new options. | ||
| 130 | foreach (keys %$native_options_ref) | ||
| 131 | { | ||
| 132 | $options_ref->{$_} = $native_options_ref->{$_}; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | # Process the options. | ||
| 136 | my($options) = ''; | ||
| 137 | foreach (sort keys %$options_ref) | ||
| 138 | { | ||
| 139 | next if (generic_option($_)); | ||
| 140 | $options .= $_ . '=' . $options_ref->{$_} . '&'; | ||
| 141 | } | ||
| 142 | chop $options; | ||
| 143 | $self->{_next_url} = $self->{_options}{'search_url'} .'?'. $self->hash_to_cgi_string($self->{_options}); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub native_retrieve_some { | ||
| 147 | my ($self) = @_; | ||
| 148 | print STDERR "**Translating......\n" if $self->{_debug}; | ||
| 149 | |||
| 150 | # Fast exit if already done: | ||
| 151 | return undef if (!defined($self->{_next_url})); | ||
| 152 | |||
| 153 | # Get some: | ||
| 154 | print STDERR "**Requesting (",$self->{_next_url},")\n" if $self->{_debug}; | ||
| 155 | my($response) = $self->http_request('POST', $self->{_next_url}); | ||
| 156 | $self->{response} = $response; | ||
| 157 | if (!$response->is_success) | ||
| 158 | { | ||
| 159 | return undef; | ||
| 160 | } | ||
| 161 | $self->{'_next_url'} = undef; | ||
| 162 | print STDERR "**Found Some\n" if $self->{_debug}; | ||
| 163 | # parse the output | ||
| 164 | my ($HEADER, $HITS, $HIT1, $GET, $END) = qw(HE HI H1 GT EN); | ||
| 165 | my $state = $HEADER; | ||
| 166 | my $hit = (); | ||
| 167 | my($raw) = ''; | ||
| 168 | my $hits_found = 0; | ||
| 169 | foreach ($self->split_lines($response->content())) | ||
| 170 | { | ||
| 171 | next if m@^$@; # short circuit for blank lines | ||
| 172 | print STDERR " * $state ===$_=== " if 2 <= $self->{'_debug'}; | ||
| 173 | |||
| 174 | # If AV returning many lines we have to do it this way | ||
| 175 | if (m|</font></b></td>|i) { | ||
| 176 | #print STDERR "**Beginning Line...\n" ; | ||
| 177 | $state = $HITS; | ||
| 178 | # if AV returning single line then it will have to be picked | ||
| 179 | # up this way | ||
| 180 | } elsif (m@^<form action=.*?method=get>@i) { | ||
| 181 | $state = $HIT1; | ||
| 182 | } | ||
| 183 | if ($state eq $HIT1 && m@^<font face=fixed><textarea.*?name="q">@i) | ||
| 184 | { | ||
| 185 | $state = $GET; | ||
| 186 | } | ||
| 187 | elsif ($state eq $GET && m@^(.+)@i) | ||
| 188 | { | ||
| 189 | if (defined($hit)) | ||
| 190 | { | ||
| 191 | push(@{$self->{cache}}, $hit); | ||
| 192 | }; | ||
| 193 | $hit = new WWW::SearchResult; | ||
| 194 | $raw .= $_; | ||
| 195 | $hit->raw($raw); | ||
| 196 | $state = $END; | ||
| 197 | # If results are multi lines we have to do this | ||
| 198 | } elsif ($state eq $HITS && m@^<td bgcolor=white>(.*)@i) { | ||
| 199 | if (defined($hit)) | ||
| 200 | { | ||
| 201 | push(@{$self->{cache}}, $hit); | ||
| 202 | }; | ||
| 203 | $hit = new WWW::SearchResult; | ||
| 204 | $raw .= $_ . " "; | ||
| 205 | $raw =~ s/<.*?>//g; | ||
| 206 | $hit->raw($raw); | ||
| 207 | $state = $HITS; | ||
| 208 | # Pick up all the text lines till we encounter a tag | ||
| 209 | } elsif ($state eq $HITS && m@^(.*?)@i) { | ||
| 210 | $raw .= $_ . " " if (not $raw =~ /<\//); | ||
| 211 | $hit->raw($raw) if (not $raw =~ /<\//); | ||
| 212 | $state = $HITS; | ||
| 213 | } elsif ($state eq $HITS && m/^<\//) { | ||
| 214 | #done | ||
| 215 | } | ||
| 216 | else | ||
| 217 | { | ||
| 218 | print STDERR "Nothing Seems Translated\n" if 2 <= $self->{_debug}; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | if (defined($hit)) | ||
| 222 | { | ||
| 223 | push(@{$self->{cache}}, $hit); | ||
| 224 | } | ||
| 225 | return $hits_found; | ||
| 226 | } | ||
| 227 | 1; | ||
| 228 |