api.pl 4.54 KB
Newer Older
priyank's avatar
priyank committed
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#!/usr/bin/env perl
use Dir::Self;
use strict;
use warnings;
use Data::Dumper;
use Mojolicious::Lite;
use lib __DIR__ . "/lib";
use ILMT::Translator qw(get_translator get_langpairs);
#use ILMT::HIN::PAN;
#use ILMT::HIN::MAR;
#use ILMT::HIN::URD;
#use ILMT::URD::HIN;
#use ILMT::PAN::HIN;
#use ILMT::HIN::TEL;
use ILMT::TEL::HIN;

plugin qw(Mojolicious::Plugin::ForkCall);

any '/:src/:tgt/translate' => sub {
    my $c = shift->render_later;
    $c->inactivity_timeout(3600);
    my %args = %{$c->req->params->to_hash};
    $args{'src_lang'} = $c->param('src');
    $args{'tgt_lang'} = $c->param('tgt');
    $args{'data'} = $args{'input'} = $args{'data'} // $args{'input'};
    $c->fork_call(
        sub {
            my (%args) = @_;
            my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
            return $translator->translate(%args);
        },
        [%args],
        sub {
            my ($c, $final_result) = @_;
            if (exists $args{"pretty"}) {
                my $final_string = join "\n", map { "$_:\n$final_result->{$_}" } keys %$final_result;
                $c->render(template => 'pretty', result => $final_string);
            } else {
                $c->render(json => $final_result);
            }
        }
    );
};

any '/:src/:tgt/:start/:end' => sub {
    print "inside start/end ............";
    my $c = shift->render_later;
    $c->inactivity_timeout(3600);
    my %args = %{$c->req->params->to_hash};
    $args{'src_lang'} = $c->param('src');
    $args{'tgt_lang'} = $c->param('tgt');
    $args{'data'} = $args{'input'} = $args{'data'} // $args{'input'};
    $c->fork_call(
        sub {
            my (%args) = @_;
            my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
            return $translator->partial_p($c->param('start'), $c->param('end'), %args);
        },
        [%args],
        sub {
            my ($c, $final_result) = @_;
            if (exists $args{"pretty"}) {
                my $final_string = join "\n", map { "$_:\n$final_result->{$_}" } keys %$final_result;
                $c->render(template => 'pretty', result => $final_string);
            } else {
                $c->render(json => $final_result);
            }
        }
    );
};


any '/partialtranslate/new/:src/:tgt/:start/:end/partial' => sub {
   print "inside partialtranslate/new ............";
    my $c = shift;
    my %args = %{$c->req->params->to_hash};
    $args{'src_lang'} = $c->param('src');
    $args{'tgt_lang'} = $c->param('tgt');
    $args{'data'} = $args{'input'} = $args{'data'} // $args{'input'};
    my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
    my $final_result = $translator->partial_p($c->param('start'), $c->param('end'), %args);
    #$c->render(json => $final_result);
    if (exists $args{"pretty"}) {
                my $final_string = join "\n", map { "$_:\n$final_result->{$_}" } keys %$final_result;
                $c->render(template => 'pretty', result => $final_string);
            } else {
                $c->render(json => $final_result);
            }
};


any '/mytranslate/new/api/:src/:tgt/mytranslate' => sub {
    print "inside mytranslate ............";
    my $c = shift;
   
    my %args = %{$c->req->params->to_hash};
    $args{'src_lang'} = $c->param('src');
    $args{'tgt_lang'} = $c->param('tgt');
    $args{'data'} = $args{'input'} = $args{'data'} // $args{'input'};
    my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
    my $final_result = $translator->translate(%args);
    #$c->render(json => $final_result);
    if (exists $args{"pretty"}) {
                my $final_string = join "\n", map { "$_:\n$final_result->{$_}" } keys %$final_result;
                $c->render(template => 'pretty', result => $final_string);
            } else {
                $c->render(json => $final_result);
            }
};



any '/:src/:tgt/' => sub {
    my $c = shift;
    my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
    $c->render(text => scalar @{$translator->{seq}});
};

any '/:src/:tgt/modules' => sub {
    my $c = shift;
    my $translator = get_translator(uc($c->param('src')), uc($c->param('tgt')));
    my @modules = map { lc($_) } @{$translator->{seq}};
    $c->render(json => \@modules);
};

any '/langpairs' => sub {
    my $c = shift;
    my %langpairs = get_langpairs();
    print Dumper(\%langpairs);
    $c->render(json => \%langpairs);
};

get '/' => sub {
    my $c = shift;
    $c->reply->static('index.html');
};

app->start;
__DATA__

@@ pretty.html.ep
<pre><%= $result %></pre>