Perl Batch Client

From Textserver wiki
Revision as of 12:02, 21 January 2016 by Padro (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
 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";