line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# =========================================================================== |
2
|
|
|
|
|
|
|
# Net::Calais |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Interface to OpenCalais web service |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Alessandro Ranellucci |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
package Net::Calais; |
9
|
1
|
|
|
1
|
|
12396
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
10
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
950
|
use HTTP::Request::Common; |
|
1
|
|
|
|
|
28754
|
|
|
1
|
|
|
|
|
77
|
|
13
|
1
|
|
|
1
|
|
974
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
24101
|
|
|
1
|
|
|
|
|
28
|
|
14
|
1
|
|
|
1
|
|
897
|
use XML::Writer; |
|
1
|
|
|
|
|
16435
|
|
|
1
|
|
|
|
|
613
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '1.02'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $CALAIS_URL = 'http://api.opencalais.com/enlighten/rest/'; |
19
|
|
|
|
|
|
|
our $SEMANTICPROXY_URL = 'http://service.semanticproxy.com/processurl/'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#-- |
22
|
|
|
|
|
|
|
sub new { |
23
|
0
|
|
|
0
|
1
|
|
my ($class, %params) = @_; |
24
|
0
|
0
|
|
|
|
|
die "Calais apikey required\n" unless $params{apikey}; |
25
|
0
|
|
|
|
|
|
my $self = { apikey => $params{apikey} }; |
26
|
0
|
0
|
|
|
|
|
$self->{ua} = LWP::UserAgent->new() or return undef; |
27
|
0
|
|
|
|
|
|
bless $self, $class; |
28
|
0
|
|
|
|
|
|
return $self; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
#-- |
31
|
|
|
|
|
|
|
sub enlighten { |
32
|
0
|
|
|
0
|
1
|
|
my __PACKAGE__ $self = shift; |
33
|
0
|
|
|
|
|
|
my ($content, %params) = @_; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# process user params and set some defaults |
36
|
0
|
|
|
|
|
|
my %request_params = (licenseID => $self->{apikey}, content => $content); |
37
|
0
|
|
|
|
|
|
my (%processingDirectives, %userDirectives) = (); |
38
|
0
|
|
0
|
|
|
|
$processingDirectives{'c:contentType'} = $params{contentType} || 'text/txt'; |
39
|
0
|
|
0
|
|
|
|
$processingDirectives{'c:outputFormat'} = $params{outputFormat} || 'XML/RDF'; |
40
|
0
|
|
|
|
|
|
for (qw(reltagBaseURL calculateRelevanceScore enableMetadataType discardMetadata)) { |
41
|
0
|
0
|
|
|
|
|
$processingDirectives{"c:$_"} = $params{$_} if $params{$_}; |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
|
for (qw(allowDistribution allowSearch externalID submitter)) { |
44
|
0
|
0
|
|
|
|
|
$userDirectives{"c:$_"} = $params{$_} if $params{$_}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# build the paramxXML parameter |
48
|
0
|
|
|
|
|
|
my $xmlWriter = XML::Writer->new( |
49
|
|
|
|
|
|
|
OUTPUT => \$request_params{paramsXML}, |
50
|
|
|
|
|
|
|
DATA_MODE => 1, |
51
|
|
|
|
|
|
|
DATA_INDENT => 0, |
52
|
|
|
|
|
|
|
ENCODING => 'UTF-8' |
53
|
|
|
|
|
|
|
); |
54
|
0
|
|
|
|
|
|
$xmlWriter->startTag('c:params', |
55
|
|
|
|
|
|
|
'xmlns:c' => 'http://s.opencalais.com/1/pred/', |
56
|
|
|
|
|
|
|
'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); |
57
|
0
|
|
|
|
|
|
$xmlWriter->startTag('c:processingDirectives', %processingDirectives); |
58
|
0
|
|
|
|
|
|
$xmlWriter->endTag('c:processingDirectives'); |
59
|
0
|
|
|
|
|
|
$xmlWriter->startTag('c:userDirectives', %userDirectives); |
60
|
0
|
|
|
|
|
|
$xmlWriter->endTag('c:userDirectives'); |
61
|
0
|
|
|
|
|
|
$xmlWriter->startTag('c:externalMetadata'); |
62
|
0
|
0
|
|
|
|
|
$xmlWriter->raw($params{externalMetadata}) if $params{externalMetadata}; |
63
|
0
|
|
|
|
|
|
$xmlWriter->endTag('c:externalMetadata'); |
64
|
0
|
|
|
|
|
|
$xmlWriter->endTag('c:params'); |
65
|
0
|
|
|
|
|
|
$xmlWriter->end; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# do REST request and return response |
68
|
0
|
|
|
|
|
|
my $response = $self->{ua}->request(POST $CALAIS_URL, \%request_params); |
69
|
0
|
0
|
|
|
|
|
if (!$response->is_success) { |
70
|
0
|
|
|
|
|
|
$self->{error} = $response->status_line; |
71
|
0
|
|
|
|
|
|
return undef; |
72
|
|
|
|
|
|
|
} |
73
|
0
|
|
|
|
|
|
return $response->content; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
#-- |
76
|
|
|
|
|
|
|
sub semanticproxy { |
77
|
0
|
|
|
0
|
1
|
|
my __PACKAGE__ $self = shift; |
78
|
0
|
|
|
|
|
|
my ($url, %params) = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
die "URL is required\n" unless $url; |
81
|
0
|
|
0
|
|
|
|
$params{output} ||= 'html'; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my $reqUrl = sprintf("%s%s/%s/%s", $SEMANTICPROXY_URL, $self->{apikey}, $params{output}, $url); |
84
|
0
|
|
|
|
|
|
my $response = $self->{ua}->request(GET $reqUrl); |
85
|
0
|
0
|
|
|
|
|
if (!$response->is_success) { |
86
|
0
|
|
|
|
|
|
$self->{error} = $response->status_line; |
87
|
0
|
|
|
|
|
|
return undef; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
return $response->content; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
#-- |
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
__END__ |