Difference between revisions of "Perl Batch Client"
From Textserver wiki
(Created page with "<syntaxhighlight lang="perl" line="1" > </syntaxhighlight>") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
<syntaxhighlight lang="perl" line="1" > | <syntaxhighlight lang="perl" line="1" > | ||
+ | #! /usr/bin/perl | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # Example client to submit a batch request to TextServer | ||
+ | # SERVICENAME service, and wait for the job to be finished. | ||
+ | # | ||
+ | # Input must be a ZIP file containing one or more text files to analyze | ||
+ | # Output will be a ZIP file containing results for each input file in | ||
+ | # the requested format (XML, json, conll) | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | use strict; | ||
+ | |||
+ | # import needeed libraries | ||
+ | use HTTP::Request::Common; | ||
+ | use LWP::UserAgent; | ||
+ | use XML::Twig; | ||
+ | use XML::LibXML; | ||
+ | |||
+ | # get query parameters | ||
+ | print "Input ZIP file: "; | ||
+ | my $fname=<>; chomp $fname; | ||
+ | print "Language: "; | ||
+ | my $lang=<>; chomp $lang; | ||
+ | print "Output format (xml,json,conll,naf): "; | ||
+ | my $out=<>; chomp $out; | ||
+ | print "TextServer username: "; | ||
+ | my $user=<>; chomp $user; | ||
+ | print "TextServer password: "; | ||
+ | my $pwd=<>; chomp $pwd; | ||
+ | |||
+ | # URL for the requested service | ||
+ | my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"; | ||
+ | my $service = "SERVICENAME"; | ||
+ | |||
+ | # create user agent | ||
+ | my $ua = LWP::UserAgent->new; | ||
+ | |||
+ | # send request to server | ||
+ | my $resp = $ua->post($TextServer_URL."/processQuery/".$service, | ||
+ | Content_Type => 'form-data', | ||
+ | Content => [ | ||
+ | username => $user, | ||
+ | password => $pwd, | ||
+ | file => [$fname], | ||
+ | language => $lang, | ||
+ | output => $out, | ||
+ | interactive => "0" | ||
+ | ] | ||
+ | ); | ||
+ | |||
+ | # check for success in connection | ||
+ | die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success; | ||
+ | |||
+ | # Server response should include a job tokenID, retrieve it | ||
+ | my $twig= new XML::Twig(); | ||
+ | $twig->parse($resp->decoded_content); | ||
+ | my $tkid = $twig->root->text("job_token_id"); | ||
+ | print "Job sumbitted. Token id=$tkid\n"; | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 12:02, 21 January 2016
1 #! /usr/bin/perl
2
3 ########################################################################
4 #
5 # Example client to submit a batch request to TextServer
6 # SERVICENAME service, and wait for the job to be finished.
7 #
8 # Input must be a ZIP file containing one or more text files to analyze
9 # Output will be a ZIP file containing results for each input file in
10 # the requested format (XML, json, conll)
11 #
12 ########################################################################
13
14 use strict;
15
16 # import needeed libraries
17 use HTTP::Request::Common;
18 use LWP::UserAgent;
19 use XML::Twig;
20 use XML::LibXML;
21
22 # get query parameters
23 print "Input ZIP file: ";
24 my $fname=<>; chomp $fname;
25 print "Language: ";
26 my $lang=<>; chomp $lang;
27 print "Output format (xml,json,conll,naf): ";
28 my $out=<>; chomp $out;
29 print "TextServer username: ";
30 my $user=<>; chomp $user;
31 print "TextServer password: ";
32 my $pwd=<>; chomp $pwd;
33
34 # URL for the requested service
35 my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
36 my $service = "SERVICENAME";
37
38 # create user agent
39 my $ua = LWP::UserAgent->new;
40
41 # send request to server
42 my $resp = $ua->post($TextServer_URL."/processQuery/".$service,
43 Content_Type => 'form-data',
44 Content => [
45 username => $user,
46 password => $pwd,
47 file => [$fname],
48 language => $lang,
49 output => $out,
50 interactive => "0"
51 ]
52 );
53
54 # check for success in connection
55 die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success;
56
57 # Server response should include a job tokenID, retrieve it
58 my $twig= new XML::Twig();
59 $twig->parse($resp->decoded_content);
60 my $tkid = $twig->root->text("job_token_id");
61 print "Job sumbitted. Token id=$tkid\n";