Difference between revisions of "Perl Interactive Client"
From Textserver wiki
(Created page with "<syntaxhighlight lang="python" line="1" > </syntaxhighlight>") |
|||
Line 1: | Line 1: | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="perl" line="1" > |
+ | #! /usr/bin/perl | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # Example client to submit an interactive request to TextServer | ||
+ | # SERVICENAME service. | ||
+ | # Input may be a plain text or a text file. No ZIP files. | ||
+ | # Output will be in the requested format (XML, json, conll) | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | use strict; | ||
+ | |||
+ | # import needeed libraries | ||
+ | use HTTP::Request::Common; | ||
+ | use LWP::UserAgent; | ||
+ | |||
+ | # get query parameters | ||
+ | print "Text to analyze: "; | ||
+ | my $text=<>; chomp $text; | ||
+ | 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 $service = "SERVICENAME"; | ||
+ | my $URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/".$service; | ||
+ | |||
+ | # create user agent | ||
+ | my $ua = LWP::UserAgent->new; | ||
+ | |||
+ | # send request to server | ||
+ | my $resp = $ua->post($URL, | ||
+ | Content_Type => 'form-data', | ||
+ | Content => [ | ||
+ | username => $user, | ||
+ | password => $pwd, | ||
+ | text_input => $text, | ||
+ | language => $lang, | ||
+ | output => $out, | ||
+ | interactive => "1" | ||
+ | ] | ||
+ | ); | ||
+ | |||
+ | |||
+ | # handle errors | ||
+ | die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success; | ||
+ | |||
+ | # No error, appropriately process response | ||
+ | # (e.g. parsing XML or JSON, and doing clever stuff with the content) | ||
+ | print $resp->decoded_content."\n"; | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 13:01, 20 January 2016
1 #! /usr/bin/perl
2
3 ########################################################################
4 #
5 # Example client to submit an interactive request to TextServer
6 # SERVICENAME service.
7 # Input may be a plain text or a text file. No ZIP files.
8 # Output will be in the requested format (XML, json, conll)
9 #
10 ########################################################################
11
12 use strict;
13
14 # import needeed libraries
15 use HTTP::Request::Common;
16 use LWP::UserAgent;
17
18 # get query parameters
19 print "Text to analyze: ";
20 my $text=<>; chomp $text;
21 print "Language: ";
22 my $lang=<>; chomp $lang;
23 print "Output format (xml,json,conll,naf): ";
24 my $out=<>; chomp $out;
25 print "TextServer username: ";
26 my $user=<>; chomp $user;
27 print "TextServer password: ";
28 my $pwd=<>; chomp $pwd;
29
30 # URL for the requested service
31 my $service = "SERVICENAME";
32 my $URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/".$service;
33
34 # create user agent
35 my $ua = LWP::UserAgent->new;
36
37 # send request to server
38 my $resp = $ua->post($URL,
39 Content_Type => 'form-data',
40 Content => [
41 username => $user,
42 password => $pwd,
43 text_input => $text,
44 language => $lang,
45 output => $out,
46 interactive => "1"
47 ]
48 );
49
50
51 # handle errors
52 die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success;
53
54 # No error, appropriately process response
55 # (e.g. parsing XML or JSON, and doing clever stuff with the content)
56 print $resp->decoded_content."\n";