CAB/Analyzer/SynCoPe.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 197 CC 22 SCAR 45.3
CC
22
Cov
0%
spliceback
210.0
query
42.0
doAnalyze
6.0
CRAP 92.7
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::SynCoPe
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DTA chain: RPC-XML query of an existing SynCoPe server
6 ## + sets $tok->{mapclass}
7  
8 package DTA::CAB::Analyzer::SynCoPe;
9 use DTA::CAB::Analyzer ':child';
10 use DTA::CAB::Format::SynCoPe::CSV;
11 use RPC::XML::Client;
12 use Encode qw(encode_utf8 decode_utf8);
13 use XML::Parser;
14 use Carp;
15 use strict;
16 our @ISA = qw(DTA::CAB::Analyzer);
17  
18 ##======================================================================
19 ## Methods
20  
21 ## $obj = CLASS_OR_OBJ->new(%args)
22 ## + object structure, %args:
23 ## server => $url, ##-- xml-rpc server url (default: http://localhost:8081/RPC2)
24 ## label => $label, ##-- analysis label (default: 'syncope')
25 ## method => $method, ##-- xml-rpc analysis method (default: 'syncop.ne.analyse')
26 ## useragent => \@args, ##-- args for LWP::UserAgent behind RPC::XML::Client; default: [timeout=>60]
27 ##
28 ## + low-level object data:
29 ## client => $cli, ##-- XML::RPC client for this object
30 ## xp => $xp, ##-- XML::Parser for parsing responses
31 sub new {
32 my $that = shift;
33 my $asub = $that->SUPER::new(
34 ##-- analysis selection
35 label => 'syncope',
36 server => 'http://localhost:8081',
37 method => 'syncop.ne.analyse',
38 useragent=>[timeout=>60],
39  
40 ##-- low-level data
41 client => undef,
42 xp => undef,
43  
44 ##-- user args
45 @_
46 );
47 return $asub;
48 }
49  
50 ## $bool = $anl->doAnalyze(\%opts, $name)
51 ## + override: only allow analyzeSentences()
52 sub doAnalyze {
53 my $anl = shift;
54 return 0 if (defined($_[1]) && $_[1] ne 'Sentences');
55 return $anl->SUPER::doAnalyze(@_);
56 }
57  
58 ## $doc = $anl->Sentences($doc,\%opts)
59 ## + post-processing for 'moot' object
60 sub analyzeSentences {
61 my ($anl,$doc,$opts) = @_;
62 return $doc if (!$anl->enabled($opts));
63  
64 my $label = $anl->{label};
65 my $qsref = $anl->doc2qstr($doc);
66 my $rsp = $anl->query($qsref);
67 $anl->spliceback($doc,$rsp);
68  
69 return $doc;
70 }
71  
72 ##======================================================================
73 ## Local utilities: query preparaion
74  
75 ## \$querystr = $anl->doc2qstr($doc)
76 ## \$querystr = $anl->doc2qstr($doc,$docname)
77 ## + get query string for document
78 ## + child classes should override this
79 ## + default implementation is appropriate for Didakowski/Drotschmann ne-recognizer v1.2.0.4
80 ## + see http://odo.dwds.de/twiki/bin/view/DWDS/EigennamenErkennung for details
81 sub doc2qstr {
82 my ($anl,$doc,$docname) = @_;
83 my $fmt = DTA::CAB::Format::SynCoPe::CSV->new();
84 my $qstr = '';
85 $fmt->toString(\$qstr)->putDocument($doc)->flush();
86 return \$qstr;
87 }
88  
89 ##======================================================================
90 ## Local utilities: query
91  
92 ## $cli = $anl->client()
93 ## + returns xml-rpc client
94 sub client {
95 return $_[0]{client} if (defined($_[0]{client}));
96 return $_[0]{client} = RPC::XML::Client->new($_[0]{server}, useragent=>($_[0]{useragent}||[]));
97 }
98  
99 ## $response_stringref_or_undef = $anl->query(\$qstr)
100 ## + warns if something goes wrong
101 sub query {
102 my ($anl,$qsr) = @_;
103 local $RPC::XML::ENCODING = "UTF-8";
104 local $RPC::XML::FORCE_STRING_ENCODING = 1;
105 $$qsr = Encode::encode_utf8($$qsr) if (utf8::is_utf8($$qsr));
106 my $rsp = $anl->client->simple_request($anl->{method},$$qsr,'tab','xml(2)');
107 $anl->logwarn("Warning: XML-RPC analysis failed: $RPC::XML::ERROR") if (!defined($rsp));
108 $anl->logwarn("Warning: XML-RPC analysis failed: $rsp->{faultString}") if (UNIVERSAL::isa($rsp,'HASH') && $rsp->{faultString});
109 return $rsp && UNIVERSAL::isa($rsp,'ARRAY') ? \$rsp->[0] : (ref($rsp) ? $rsp : \$rsp);
110 }
111  
112 ##======================================================================
113 ## Local utilities: response
114  
115 ##----------------------------------------------------------------------
116 ## $doc = $anl->spliceback($doc, \$syncope_xml_response_string, %opts)
117 ## + %opts: (none)
118 sub spliceback {
119 my ($anl,$doc,$rsp,%opts) = @_;
120  
121 ##-- define the expat parser (we need $doc as a local callback!)
122 my $alabel = $anl->{label};
123 my ($_xp,$_elt,%_attrs);
124 my (%id2t,%id2n,@stack);
125  
126 ## undef = cb_start($expat,$elt,%attrs)
127 my ($w,$a,$starti,$endi, $si,$wi,$sid,$wid);
128 my $cb_start = sub {
129 ($_xp,$_elt,%_attrs) = @_;
130  
131 if ($_elt eq 'terminal') {
132 if (defined($_attrs{offset}) && defined($_attrs{'token-pos'})) {
133 ##-- new (>=2012-08-22)
134 ($si,$wi,$sid,$wid) = split(/ /,$_attrs{offset},4);
135 } elsif (defined($_attrs{line}) && defined($_attrs{pos})) {
136 ##-- old (<2012-08-22)
137 ($si,$wi) = @_attrs{qw(line pos)};
138 } else {
139 $anl->logconfess("spliceback(): could not parse terminal attributes for ", $_xp->original_string())
140 }
141 $w = $doc->{body}[$si]{tokens}[$wi];
142 $w->{$alabel} = $id2t{$_attrs{id}} = [ $a = { nid=>$_attrs{id} } ];
143 push(@stack,$a);
144 }
145 elsif ($_elt eq 'nonterminal') {
146 $a = $id2n{$_attrs{id}} = { nid=>$_attrs{id}, start=>$_attrs{start}, end=>$_attrs{end}, depth=>$_attrs{depth} };
147 $starti = ($a->{start}=~/^T([0-9]+)$/ ? $1 : undef);
148 $endi = ($a->{end} =~/^T([0-9]+)$/ ? $1 : undef);
149 foreach (grep {defined($_)} @id2t{defined($starti) && defined($endi) ? (map {"T$_"} ($starti..$endi)) : qw()}) {
150 push(@$_, $a);
151 }
152 push(@stack,$a);
153 }
154 elsif ($_elt =~ /^category(?:\-(?:open|close))?$/) {
155 $a->{cat} = $_attrs{name};
156 }
157 elsif ($_elt eq 'function') {
158 $a->{func} = $_attrs{name};
159 }
160 elsif ($_elt eq 'label') {
161 @$a{qw(labid label)} = @_attrs{qw(id name)};
162 $a->{label} = "#$a->{label}" if ($a->{label} !~ /^\#/);
163 }
164 };
165  
166 ## undef = cb_end($expat,$elt)
167 my $cb_end = sub {
168 pop(@stack) if ($_[1] eq 'terminal' || $_[1] eq 'nonterminal');
169 $a = $stack[$#stack];
170 };
171  
172 ## undef = cb_init($expat)
173 my $cb_init = sub {
174 @stack = %id2t = %id2n = %_attrs = qw();
175 $_elt = $_xp = $w = $a = $starti = $endi = undef;
176 };
177  
178 ## undef = cb_final($expat)
179 my $cb_final = sub {
180 @stack = %id2t = %id2n = %_attrs = qw();
181 $_elt = $_xp = $w = $a = $starti = $endi = undef;
182 };
183  
184 ##-- parser
185 my $xp = XML::Parser->new(
186 ErrorContext => 1,
187 ProtocolEncoding => 'UTF-8',
188 Handlers => {
189 Init => $cb_init,
190 Start => $cb_start,
191 End => $cb_end,
192 Final => $cb_final,
193 },
194 )
195 or $anl->logconfess("spliceback(): could not create XML::Parser");
196 $xp->parse(ref($rsp) ? $$rsp : $rsp);
197  
198 return $doc;
199 }
200  
201  
202 1; ##-- be happy
203  
204 __END__