« blib/lib/Lingua/Ogmios/DocumentRecord.pm
↑ Summary
blib/lib/Lingua/Ogmios/NLPWrappers/GeniaTagger.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::SectionIdentification; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | |||
| 7 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 8 | use Lingua::Ogmios::Annotations::Section; | ||
| 9 | |||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 18 | |||
| 19 | warn "[LOG] Creating a wrapper of the SectionIdentification\n"; | ||
| 20 | |||
| 21 | my $SectionIdentification = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 22 | |||
| 23 | $SectionIdentification->_input_filename($tmpfile_prefix . ".SectionIdentification.in"); | ||
| 24 | $SectionIdentification->_output_filename($tmpfile_prefix . ".SectionIdentification.out"); | ||
| 25 | |||
| 26 | return($SectionIdentification); | ||
| 27 | |||
| 28 | } | ||
| 29 | |||
| 30 | sub _processSectionIdentification { | ||
| 31 | my ($self) = @_; | ||
| 32 | |||
| 33 | warn "[LOG] SectionIdentification\n"; | ||
| 34 | |||
| 35 | my $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 36 | |||
| 37 | # signature type : '-- ' | ||
| 38 | # '_______' | ||
| 39 | # '-------' | ||
| 40 | |||
| 41 | # foreach $document (@{$self->_documentSet}) { | ||
| 42 | # foreach $token (@{$document->getAnnotations->getTokenLevel->getElements}) { | ||
| 43 | # # warn "$token: " . $token->getContent . ";\n"; | ||
| 44 | # # $corpus_in .= $token->getContent; | ||
| 45 | |||
| 46 | # # '-- ' | ||
| 47 | # if ($token->getContent eq "-") { | ||
| 48 | # if (($token->next->getContent eq "-") && ($token->next->next->getContent eq " ")) { | ||
| 49 | |||
| 50 | # } | ||
| 51 | # } | ||
| 52 | # # '------' | ||
| 53 | # if ($token->getContent eq "-") { | ||
| 54 | # my $minnb = 4; | ||
| 55 | |||
| 56 | # } | ||
| 57 | |||
| 58 | # # '_____' | ||
| 59 | # if ($token->getContent eq "_") { | ||
| 60 | # my $minnb = 4; | ||
| 61 | |||
| 62 | # } | ||
| 63 | |||
| 64 | |||
| 65 | # } | ||
| 66 | # } | ||
| 67 | # $corpus_in =~ s/[\x{A0}\x{2000}-\x{200B}]/ /go; | ||
| 68 | # $corpus_in =~ s/\x{0153}/oe/go; | ||
| 69 | # $corpus_in =~ s/\x{0152}/OE/go; | ||
| 70 | |||
| 71 | |||
| 72 | return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{SectionIdentification_CMD} . " " . $self->_input_filename . " " . $self->_output_filename))); | ||
| 73 | |||
| 74 | warn "[LOG]\n"; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _inputSectionIdentification { | ||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | my $document; | ||
| 81 | my $section; | ||
| 82 | my $i; | ||
| 83 | my $start_token; | ||
| 84 | my $end_token; | ||
| 85 | my $sectionForm; | ||
| 86 | |||
| 87 | my @lines; | ||
| 88 | |||
| 89 | warn "[LOG] making SectionIdentification input\n"; | ||
| 90 | |||
| 91 | open FILE_IN, ">" . $self->_input_filename; | ||
| 92 | binmode(FILE_IN, ":utf8"); | ||
| 93 | |||
| 94 | $self->{'Id2documents'} = {}; | ||
| 95 | |||
| 96 | foreach $document (@{$self->_documentSet}) { | ||
| 97 | $i = 0; | ||
| 98 | $self->{'Id2documents'}->{$document->getId} = $document; | ||
| 99 | foreach $section (@{$document->getAnnotations->getSectionLevel->getElements}) { | ||
| 100 | # for($i = 0; $i < scalar(@{$document->getAnnotations->getSectionLevel->getElements}); $i++) { | ||
| 101 | ($start_token, $end_token, $sectionForm) = $section->getForm($document->getAnnotations->getTokenLevel); | ||
| 102 | print FILE_IN "section = " . $section->getId . "\n"; | ||
| 103 | print FILE_IN "doc = " . $document->getId . "\n"; | ||
| 104 | print FILE_IN "start = " . $start_token->getFrom . "\n"; | ||
| 105 | print FILE_IN "end = " . $end_token->getTo . "\n"; | ||
| 106 | if (defined $section->type) { | ||
| 107 | print FILE_IN "type = " . $section->type . "\n"; | ||
| 108 | } | ||
| 109 | if (defined $section->parent_section) { | ||
| 110 | print FILE_IN "parent_section = " . $section->parent_section->getId . "\n"; | ||
| 111 | } | ||
| 112 | # @lines = split /\n/, $sectionForm; | ||
| 113 | # print FILE_IN "text (" . scalar(@lines) . ") = " . $sectionForm . "\n"; | ||
| 114 | my $pos = -1; | ||
| 115 | my $newlines = 0; | ||
| 116 | while(($pos = index($sectionForm, "\n", $pos + 1)) > -1) { | ||
| 117 | $newlines ++; | ||
| 118 | } | ||
| 119 | if ($newlines ==0) { | ||
| 120 | $newlines++; | ||
| 121 | } | ||
| 122 | # if ($pos != length($sectionForm)) { | ||
| 123 | # $newlines++; | ||
| 124 | # } | ||
| 125 | print FILE_IN "text ($newlines) = " . $sectionForm; | ||
| 126 | print FILE_IN "--\n"; | ||
| 127 | # if (($section->getFromOffset == $firsttoken->getFrom) && ($section->getToOffset == $lasttoken->getTo)) { | ||
| 128 | # $section->getForm($document->getAnnotations->getTokenLevel); | ||
| 129 | # ($start_token, $end_token, $sectionForm) = $section->getForm($document->getAnnotations->getTokenLevel); | ||
| 130 | $i++; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | close FILE_IN; | ||
| 134 | warn "[LOG] done\n"; | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | sub _outputSectionIdentification { | ||
| 139 | my ($self) = @_; | ||
| 140 | |||
| 141 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 142 | my $line; | ||
| 143 | my @sections; | ||
| 144 | my $size; | ||
| 145 | my $section_out; | ||
| 146 | my $document; | ||
| 147 | my $newSection; | ||
| 148 | |||
| 149 | my $tokenFrom; | ||
| 150 | my $tokenTo; | ||
| 151 | |||
| 152 | # addSection | ||
| 153 | |||
| 154 | open FILE_OUT, $self->_output_filename; | ||
| 155 | binmode(FILE_OUT, ":utf8"); | ||
| 156 | |||
| 157 | while($line = <FILE_OUT>) { | ||
| 158 | chomp $line; | ||
| 159 | if ($line =~ /^section = /o) { | ||
| 160 | # warn "$line\n"; | ||
| 161 | my $section = { | ||
| 162 | "section" => $', # ' | ||
| 163 | }; | ||
| 164 | $line = <FILE_OUT>; | ||
| 165 | chomp $line; | ||
| 166 | if ($line =~ /^doc = /o) { | ||
| 167 | $section->{"doc"} = $'; # ' | ||
| 168 | $line = <FILE_OUT>; | ||
| 169 | chomp $line; | ||
| 170 | } | ||
| 171 | if ($line =~ /^start = /o) { | ||
| 172 | # warn "$line\n"; | ||
| 173 | $section->{"start"} = $'; # ' | ||
| 174 | $line = <FILE_OUT>; | ||
| 175 | chomp $line; | ||
| 176 | } | ||
| 177 | if ($line =~ /^end = /o) { | ||
| 178 | $section->{"end"} = $'; # ' | ||
| 179 | $line = <FILE_OUT>; | ||
| 180 | chomp $line; | ||
| 181 | } | ||
| 182 | if ($line =~ /^type = /o) { | ||
| 183 | $section->{"type"} = $'; # ' | ||
| 184 | $line = <FILE_OUT>; | ||
| 185 | chomp $line; | ||
| 186 | } | ||
| 187 | if ($line =~ /^parent_section = /o) { | ||
| 188 | $section->{"parent_section"} = $'; # ' | ||
| 189 | $line = <FILE_OUT>; | ||
| 190 | chomp $line; | ||
| 191 | } | ||
| 192 | if ($line =~ /^title = /o) { | ||
| 193 | $section->{"title"} = $'; # ' | ||
| 194 | $line = <FILE_OUT>; | ||
| 195 | chomp $line; | ||
| 196 | } | ||
| 197 | if ($line =~ /^text \(([0-9]+)\) = /o) { | ||
| 198 | $size = $1; | ||
| 199 | # warn "$line\n"; | ||
| 200 | # warn $section->{"start"} . "\n"; | ||
| 201 | $section->{"text"} = [$']; # ' | ||
| 202 | $size--; | ||
| 203 | while($size > 0) { | ||
| 204 | $line = <FILE_OUT>; | ||
| 205 | chomp $line; | ||
| 206 | if (length($line) == 0) { | ||
| 207 | $line = ""; | ||
| 208 | } | ||
| 209 | push @{$section->{"text"}}, $line; | ||
| 210 | $size--; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | # warn "####\n"; | ||
| 214 | # warn "new Section" . $section->{'section'} . "\n"; | ||
| 215 | # warn $section->{"start"} . "\n"; | ||
| 216 | |||
| 217 | push @sections, $section; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | close(FILE_OUT); | ||
| 221 | |||
| 222 | # warn "*****\n"; | ||
| 223 | foreach $section_out (@sections) { | ||
| 224 | # warn "new Section" . $section_out->{'section'} . "\n"; | ||
| 225 | # warn $section_out->{"start"} . "\n"; | ||
| 226 | # warn $section_out->{"end"} . "\n"; | ||
| 227 | |||
| 228 | $document = $self->{'Id2documents'}->{$section_out->{'doc'}}; | ||
| 229 | if (!$document->getAnnotations->getSectionLevel->existsElement($section_out->{'section'})) { | ||
| 230 | |||
| 231 | if ($document->getAnnotations->getTokenLevel->existsElementFromIndex('from', $section_out->{'start'})) { | ||
| 232 | $tokenFrom = $document->getAnnotations->getTokenLevel->getElementFromIndex('from', $section_out->{'start'})->[0]; | ||
| 233 | # warn "$tokenFrom\n"; | ||
| 234 | } | ||
| 235 | if ($document->getAnnotations->getTokenLevel->existsElementFromIndex('to', $section_out->{'end'})) { | ||
| 236 | $tokenTo = $document->getAnnotations->getTokenLevel->getElementFromIndex('to', $section_out->{'end'})->[0]; | ||
| 237 | } | ||
| 238 | |||
| 239 | # warn "add section: " . $section_out->{'section'} . " : $tokenFrom : $tokenTo\n"; | ||
| 240 | |||
| 241 | $newSection = Lingua::Ogmios::Annotations::Section->new({ | ||
| 242 | 'from' => $tokenFrom, | ||
| 243 | 'to' => $tokenTo, | ||
| 244 | 'title' => $section_out->{'title'}, | ||
| 245 | 'type' => "narrative", | ||
| 246 | 'child_sections' => [], | ||
| 247 | # 'rank' => $rank++, | ||
| 248 | } | ||
| 249 | ); | ||
| 250 | |||
| 251 | # warn "newSection: $newSection\n"; | ||
| 252 | $document->getAnnotations->addSection($newSection, $section_out->{'parent_section'}); | ||
| 253 | # if ($document->getAnnotations->getSectionLevel->existsElement($section_out->{'parent_section'})) { | ||
| 254 | # $newSection->parent_section($document->getAnnotations->getSectionLevel->getElement($section_out->{'parent_section'})); | ||
| 255 | # push @{$newSection->parent_section->child_sections}, $newSection; | ||
| 256 | # } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | warn "[LOG] done\n"; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub run { | ||
| 263 | my ($self, $documentSet) = @_; | ||
| 264 | |||
| 265 | # Set variables according the the configuration | ||
| 266 | |||
| 267 | $self->_documentSet($documentSet); | ||
| 268 | |||
| 269 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 270 | |||
| 271 | $self->_inputSectionIdentification; | ||
| 272 | |||
| 273 | my $command_line = $self->_processSectionIdentification; | ||
| 274 | |||
| 275 | # if ($self->_position eq "last") { | ||
| 276 | # # TODO | ||
| 277 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 278 | warn "print no standard output\n"; | ||
| 279 | } else { | ||
| 280 | $self->_outputSectionIdentification; | ||
| 281 | } | ||
| 282 | # $self->_outputParsing; | ||
| 283 | |||
| 284 | |||
| 285 | # Put log information | ||
| 286 | |||
| 287 | my $information = { 'software_name' => $self->_config->name, | ||
| 288 | 'comments' => $self->_config->comments, | ||
| 289 | 'command_line' => $command_line, | ||
| 290 | 'list_modified_level' => [''], | ||
| 291 | }; | ||
| 292 | $self->_log($information); | ||
| 293 | |||
| 294 | |||
| 295 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 296 | # You should define a 'run' method for your wrapper\n"; | ||
| 297 | warn "[LOG] done\n"; | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | 1; | ||
| 302 | |||
| 303 | __END__ |