NLPPlatform/Document.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 178 CC 15 SCAR 30.1
CC
15
Cov
0%
get_language
42.0
get_documentRecords
20.0
get_language_from_data
20.0
CRAP 20.3
1 package Alvis::NLPPlatform::Document;
2  
3 use strict;
4 use warnings;
5  
6 use Lingua::Identify;
7  
8 use Data::Dumper;
9  
10 our $VERSION=$Alvis::NLPPlatform::VERSION;
11  
12 # use YAML qw( Dump );
13  
14 sub getnamespace
15 {
16 my $file = shift;
17  
18 my $line;
19 my $xmlns = undef;
20  
21 open FILE, $file;
22 binmode(FILE);
23  
24 while(($line=<FILE>)){
25 if ($line =~ /xmlns=\"?([^\"]+)\"?/) {
26 $xmlns = $1;
27 next;
28 }
29 };
30 close FILE;
31  
32 return($xmlns);
33 }
34  
35 sub get_documentRecords
36 {
37 my $xmlalvisfile=shift;
38  
39 my $doc;
40 my $Parser=XML::LibXML->new();
41  
42  
43 my $doc_list = "";
44  
45 eval
46 {
47 $doc=$Parser->parse_string($xmlalvisfile);
48 };
49 if ($@)
50 {
51 warn "Parsing the doc failed: $@. Trying to get the IDs..\n";
52 eval
53 {
54 $xmlalvisfile=~s/<documentRecord\s(xmlns=[^\s]+)*\sid\s*=\s*\"([^\"]*?)\">/&unparseable_id($2)/esgo;
55 };
56 }
57 else
58 {
59 if ($doc)
60 {
61  
62 my $root=$doc->documentElement();
63 for my $rec_node ($root->getChildrenByTagName('documentRecord'))
64 {
65 my $id=$rec_node->getAttribute("id");
66 if (defined($id))
67 {
68 $doc_list .= $rec_node->toString();
69 }
70 else
71 {
72 my $rec_str=$rec_node->toString();
73 $rec_str=~s/\n/ /sgo;
74 warn "No id for record $rec_str\n";
75 }
76 }
77 }
78 else
79 {
80 my $doc_str=$xmlalvisfile;
81 $doc_str=~s/\n/ /sgo;
82 warn "Parsing the doc failed. Doc: $doc_str\n";
83 }
84 }
85  
86 return $doc_list;
87 }
88  
89 sub get_language_from_file
90 {
91 my $xmlalvisfile=shift;
92 my $outfile = shift;
93 my $config = shift;
94  
95 print STDERR "Identifying the language from file ($xmlalvisfile)\n";
96  
97 my $doc;
98 my $Parser=XML::LibXML->new();
99  
100  
101 eval
102 {
103 $doc=$Parser->parse_file($xmlalvisfile);
104 };
105 if (!$@)
106 {
107 if ($doc)
108 {
109 my $xmlalvisdata = &get_language($doc);
110  
111  
112 open OUTPUT_FILE, ">$outfile";
113 binmode(OUTPUT_FILE, ":utf8");
114 print OUTPUT_FILE "$xmlalvisdata\n";
115 close(OUTPUT_FILE);
116 return($outfile);
117 }
118 else
119 {
120 warn "Parsing the doc failed.\n";
121 }
122 } else {
123 warn "Parsing the doc failed.\n";
124 print STDERR $@;
125 }
126  
127 return $outfile;
128 }
129  
130 sub get_language_from_data
131 {
132 my $xmlalvis=shift;
133  
134 # print STDERR $xmlalvis;
135  
136 print STDERR "Identifying the language from data\n";
137  
138 my $doc;
139 my $Parser=XML::LibXML->new();
140  
141  
142 eval
143 {
144 $doc=$Parser->parse_string($xmlalvis);
145 };
146 if (!$@)
147 {
148 if ($doc)
149 {
150 $xmlalvis = &get_language($doc);
151 }
152 else
153 {
154 warn "Parsing the doc failed. \n";
155 }
156 } else {
157 warn "Parsing the doc failed.\n";
158 if ($@ =~ /UTF-8/) {
159 warn "Not a UTF-8, assume to be a latin-1 document\n";
160 print STDERR "Converting in UTF8...\n";
161 Encode::from_to($xmlalvis, "iso-8859-1", "UTF-8");
162 print STDERR "done\n";
163 $xmlalvis = &get_language_from_data($xmlalvis);
164 }
165 }
166 # print STDERR $xmlalvis;
167 return $xmlalvis;
168 }
169  
170  
171 sub get_language
172 {
173 my ($doc) = @_;
174  
175 # print STDERR Dumper $doc;
176  
177 # print STDERR $doc->toString();
178  
179 print STDERR "In get_language\n";
180  
181 my $root=$doc->documentElement();
182 my $analysis_node;
183 my $property_node;
184 # print STDERR Dumper $root;
185 my $language_exists = 0;
186  
187 # print STDERR $root->nodeName() . "\n";
188  
189  
190 my @rec_node;
191 my $rec_node;
192  
193 if ($root->nodeName() eq "documentCollection") {
194 @rec_node = $root->getChildrenByTagName('documentRecord');
195 } else {
196 @rec_node = $root;
197 }
198  
199 # print STDERR Dumper $rec_node;
200 foreach $rec_node (@rec_node) {
201  
202 my @acq_data = $rec_node->getChildrenByTagName("acquisition");
203 $analysis_node = $acq_data[0]->getChildrenByTagName("analysis");
204 if ((defined $analysis_node) && (scalar(@$analysis_node) > 0)) {
205 foreach $property_node ($analysis_node->[0]->getChildrenByTagName("property")) {
206 if (($property_node->hasAttribute("name")) && ($property_node->getAttribute("name") eq "language")) {
207 print STDERR "Language found : " . $property_node->string_value() . "\n";
208 $language_exists = 1;
209 }
210 }
211 }
212 if ($language_exists == 0) {
213 my $can_doc = $acq_data[0]->getChildrenByTagName("canonicalDocument");
214 if (defined $can_doc) {
215 my $language = Lingua::Identify::langof($can_doc->string_value());
216 print STDERR "Language: $language\n";
217 my $property_node = XML::LibXML::Element->new("property");
218 $property_node->setAttribute( "name", "language" );
219 $property_node->appendTextNode($language);
220 my @attr = $property_node->attributes();
221
222 $analysis_node = XML::LibXML::Element->new("analysis");
223 $analysis_node->appendTextNode("\n\t\t\t");
224 $analysis_node->appendChild($property_node);
225 $analysis_node->appendTextNode("\n\t\t");
226
227 $acq_data[0]->appendTextNode("\t\t");
228 $acq_data[0]->appendChild($analysis_node);
229 $acq_data[0]->appendTextNode("\n\t");
230 } else {
231 print STDERR "no canonical document\n";
232 }
233 }
234 }
235 # print STDERR $doc->toString();
236 # print STDERR $root->toString();
237 return($doc->toString());
238 # return($rec_node->toString());
239 }
240  
241 1;
242  
243 __END__