You are here

MPT REST API usage example for Perl

The following Perl code allows you to send the input text (from standard input stream or file(s) specified on the command line to Connexor Machinese Phrase Tagger web service.
#!/usr/bin/perl -w
use strict;

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use Getopt::Long;

my $lang;

# Read command line options
&GetOptions("lang=s"=>\$lang);
# If no language option specified, default is English.
$lang="en" unless $lang;
# Check language code validity
die "Illegal language code: $lang\n" unless $lang=~/^(da|de|en|es|fi|fr|it|nl|no|sv)$/;

my $ua=LWP::UserAgent->new();

my $text;

my $authkey='YourAuthenticationKey';

die "Please change the authentication key string in this script.\n" if $authkey eq 'YourAuthenticationKey';

# Read input
while(<>) {
    $text.=$_;
}

my $url= "http://cxserv2.nlpengine.net/serv/tech/fdg1/$lang/";

# Send HTTP request
my $req=POST $url,
      [AuthKey=>$authkey,
       text=>$text];
my $result = $ua->request($req);
# Print output
if($result->is_success) {
    print $result->content, "\n";
} else {
    print $result->status_line;
}

(Download this program)

Note: You must adjust the authentication key string to match the key assigned to you.

If you want to run this client on Windows platform, we suggest you to first download and install Strawberry Perl. Then it is possible to run this script from the Windows PowerShell using commands like: echo "This is a test." | perl mpt-webapi.pl --lang=en