-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabelize
executable file
·83 lines (63 loc) · 1.8 KB
/
babelize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;
use HTML::Parser;
use Getopt::Std;
=head1 NAME
B<babelize> - bastardize languages using babelfish
=head1 SYNOPSIS
B<babelize> [B<-a>] [B<-e> I<string>] [I<file>]
=head1 DESCRIPTION
B<babelize> uses L<http://tashian.com/multibabel/> in order to perform some
heavy bastardization of the words in the file I<file>, or the string I<string>,
if B<-e> option is given. If the B<-a> option is given, Asian languages are
used in the process as well.
=cut
our $opt_a = 0;
our $opt_e;
getopts ('ae:');
$opt_a = $opt_a ? "yes" : undef;
my $text;
if (defined $opt_e)
{
$text = $opt_e;
} else {
my $fname = shift;
my $file;
if (defined $fname)
{
open ($file, "<", $fname) or die "Could not open $fname: $!";
} else {
$file = \*STDIN;
}
{
local $/;
$text = <$file>;
}
}
my $mech = WWW::Mechanize->new;
my $p = HTML::Parser->new();
$mech->get ("http://tashian.com/perl/translate-babel.cgi");
unless ($mech->success) { die $mech->status() }
$mech->submit_form (fields => { english_text => $text, asian => $opt_a });
while (1)
{
my $is_tt = 0;
my @results;
# The data seems to be stored in <tt> tags, so we grab them.
$p->handler(text => sub { if ($is_tt) { push @results, shift } }, 'dtext');
$p->handler(start => sub { return unless shift eq "tt"; $is_tt = 1; }, 'tagname');
$p->handler(end => sub { return unless shift eq "tt"; $is_tt = 0; }, 'tagname');
$p->parse($mech->content);
print join "\n", @results, "\n";
print "Try again? (y/N) ";
last unless (<> =~ m/^y/i);
$mech->submit_form();
}
=head1 BUGS
None known, poke me if you find something.
=head1 AUTHOR & LICENSE
Copyright © 2009, Vsevolod Kozlov <[email protected]>
You can do anything with this code, but I disclaim all responsibility that I
can under the applicable law.