« blib/lib/Lingua/Ogmios/Annotations/LogProcessing.pm
↑ Summary
blib/lib/Lingua/Ogmios/NLPWrappers/AssertionDetection.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::BasicNER; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | |||
| 7 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 8 | use Lingua::Ogmios::Annotations::SemanticUnit; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 17 | |||
| 18 | warn "[LOG] Creating a wrapper of the BasicNER\n"; | ||
| 19 | |||
| 20 | |||
| 21 | my $BasicNER = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 22 | |||
| 23 | return($BasicNER); | ||
| 24 | |||
| 25 | } | ||
| 26 | |||
| 27 | sub BasicNER { | ||
| 28 | my ($self) = @_; | ||
| 29 | |||
| 30 | my $sentence; | ||
| 31 | my $document; | ||
| 32 | |||
| 33 | my $datere = q([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}); | ||
| 34 | |||
| 35 | my $daytime = q([0-2][0-4]:[0-5][0-9]:[0-5][0-9]); | ||
| 36 | |||
| 37 | my $age = q(([0-9]{1,3}[ \-](year[ \-]old|yo\b)|age [0-9]{1,3})); | ||
| 38 | |||
| 39 | my $percent = q(([+\-])?[0-9]+(\.)?[0-9]* ?%); | ||
| 40 | |||
| 41 | my $number = q((([0-9]+( , |,)?[0-9]{3})(?!%)|[0-9]+([.][0-9]+)?)); | ||
| 42 | |||
| 43 | my $unit = q( ?(?i)(mg|kg|g)(?=\b)); | ||
| 44 | |||
| 45 | my $dosage = "$number $unit"; | ||
| 46 | |||
| 47 | my $bloodpressure = "(?<ctxtbp2a>(?<ctxtbp1a>blood pressure". '(\\b[^\.]*?))'. "(?<bp1>$number/$number))" . '((?<ctxtbp2b>\\b[^\.]*?)'. "(?<bp2>$number/$number))?"; | ||
| 48 | |||
| 49 | my $pulse = "(pulse \\K$number|$number(?= beats))"; | ||
| 50 | |||
| 51 | my $cardinal = q((1st|2nd|3rd|[4-9]th|first|second|third|fourth|fifth|sixth|seventh|eighth|nineth)); | ||
| 52 | |||
| 53 | my $letterrednumber = q((one|two|three|four|five|six|seven|eight|nine|ten)); | ||
| 54 | my $times = "($letterrednumber|$number|$number-$number|$letterrednumber to $letterrednumber) (minutes|hours|days?|weeks?)"; | ||
| 55 | |||
| 56 | my $sentenceForm; | ||
| 57 | |||
| 58 | foreach $document (@{$self->_documentSet}) { | ||
| 59 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 60 | $sentenceForm = $sentence->getForm; | ||
| 61 | |||
| 62 | while ($sentenceForm =~ /$datere/gos) { | ||
| 63 | warn "=> $& (date - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 64 | |||
| 65 | $self->addSemanticEntity($&, | ||
| 66 | $sentence->refid_start_token->getFrom + length($`), | ||
| 67 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 68 | "date", | ||
| 69 | $sentence->refid_start_token, | ||
| 70 | $document | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | while ($sentenceForm =~ /$daytime/gos) { | ||
| 74 | warn "=> $& (daytime - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 75 | $self->addSemanticEntity($&, | ||
| 76 | $sentence->refid_start_token->getFrom + length($`), | ||
| 77 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 78 | "daytime", | ||
| 79 | $sentence->refid_start_token, | ||
| 80 | $document | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | while ($sentenceForm =~ /$age/ogs) { | ||
| 85 | warn "=> $& (age - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 86 | $self->addSemanticEntity($&, | ||
| 87 | $sentence->refid_start_token->getFrom + length($`), | ||
| 88 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 89 | "age", | ||
| 90 | $sentence->refid_start_token, | ||
| 91 | $document | ||
| 92 | ); | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | while ($sentenceForm =~ /$percent/gos) { | ||
| 97 | warn "=> $& (percent - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 98 | $self->addSemanticEntity($&, | ||
| 99 | $sentence->refid_start_token->getFrom + length($`), | ||
| 100 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 101 | "percent", | ||
| 102 | $sentence->refid_start_token, | ||
| 103 | $document | ||
| 104 | ); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | while ($sentenceForm =~ /$number/ogs) { | ||
| 109 | warn "=> $& (number - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 110 | $self->addSemanticEntity($&, | ||
| 111 | $sentence->refid_start_token->getFrom + length($`), | ||
| 112 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 113 | "number", | ||
| 114 | $sentence->refid_start_token, | ||
| 115 | $document | ||
| 116 | ); | ||
| 117 | |||
| 118 | } | ||
| 119 | while ($sentenceForm =~ /$dosage/ogs) { | ||
| 120 | warn "=> $& (dosage - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 121 | |||
| 122 | $self->addSemanticEntity($&, | ||
| 123 | $sentence->refid_start_token->getFrom + length($`), | ||
| 124 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 125 | "dosage", | ||
| 126 | $sentence->refid_start_token, | ||
| 127 | $document | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | |||
| 131 | while ($sentenceForm =~ m{$bloodpressure}ogs) { | ||
| 132 | warn "=> " . $+{bp1} . " " . $+{bp2} . " (blood pressure - from " . length($`) . " to " . (length($`) + length($+{bp1})) . " and from " . (length($`) + length($+{ctxtbp2a}) + length($+{ctxtbp2b})) . " to " . (length($`) + length($+{ctxtbp2a}) + length($+{ctxtbp2b}) + length($+{bp2})) . ")\n"; | ||
| 133 | |||
| 134 | $self->addSemanticEntity($+{bp1}, | ||
| 135 | $sentence->refid_start_token->getFrom + length($`) + length($+{ctxtbp1a}), | ||
| 136 | $sentence->refid_start_token->getFrom + length($`) + length($+{ctxtbp1a}) + length($+{bp1}), | ||
| 137 | "blood pressure", | ||
| 138 | $sentence->refid_start_token, | ||
| 139 | $document | ||
| 140 | ); | ||
| 141 | $self->addSemanticEntity($+{bp2}, | ||
| 142 | $sentence->refid_start_token->getFrom + (length($`) + length($+{ctxtbp2a}) + length($+{ctxtbp2b})), | ||
| 143 | $sentence->refid_start_token->getFrom + (length($`) + length($+{ctxtbp2a}) + length($+{ctxtbp2b}) + length($+{bp2})), | ||
| 144 | "blood pressure", | ||
| 145 | $sentence->refid_start_token, | ||
| 146 | $document | ||
| 147 | ); | ||
| 148 | |||
| 149 | |||
| 150 | } | ||
| 151 | while ($sentenceForm =~ m{$pulse}ogs) { | ||
| 152 | warn "=> $& (pulse - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 153 | |||
| 154 | $self->addSemanticEntity($&, | ||
| 155 | $sentence->refid_start_token->getFrom + length($`), | ||
| 156 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 157 | "pulse", | ||
| 158 | $sentence->refid_start_token, | ||
| 159 | $document | ||
| 160 | ); | ||
| 161 | } | ||
| 162 | while ($sentenceForm =~ m{$times}ogs) { | ||
| 163 | warn "=> $& (times - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 164 | $self->addSemanticEntity($&, | ||
| 165 | $sentence->refid_start_token->getFrom + length($`), | ||
| 166 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 167 | "times", | ||
| 168 | $sentence->refid_start_token, | ||
| 169 | $document | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | while ($sentenceForm =~ m{$cardinal}ogs) { | ||
| 174 | warn "=> $& (cardinal - from " . length($`) . " to " . (length($`) + length($&)) . ")\n"; | ||
| 175 | $self->addSemanticEntity($&, | ||
| 176 | $sentence->refid_start_token->getFrom + length($`), | ||
| 177 | $sentence->refid_start_token->getFrom + length($`) + length($&), | ||
| 178 | "cardinal", | ||
| 179 | $sentence->refid_start_token, | ||
| 180 | $document | ||
| 181 | ); | ||
| 182 | |||
| 183 | } | ||
| 184 | |||
| 185 | } | ||
| 186 | } | ||
| 187 | # exit; | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub addSemanticEntity { | ||
| 193 | my $self = shift; | ||
| 194 | my $NE_form = shift; | ||
| 195 | my $from = shift; | ||
| 196 | my $to = shift; | ||
| 197 | my $type = shift; | ||
| 198 | my $token = shift; | ||
| 199 | my $document = shift; | ||
| 200 | |||
| 201 | my @NE2tokens; | ||
| 202 | |||
| 203 | # my $from = $sentence->refid_start_token->getFrom + length($`); | ||
| 204 | # my $to = $sentence->refid_start_token->getFrom + length($`) + length($&); | ||
| 205 | # $NE_form = $&; | ||
| 206 | # $type = "date"; | ||
| 207 | my $refid_start_token; | ||
| 208 | |||
| 209 | while($token->getFrom != $from) { | ||
| 210 | $token = $token->next; | ||
| 211 | } | ||
| 212 | |||
| 213 | # @NE2tokens = (); | ||
| 214 | do { | ||
| 215 | # print " " . $token->getContent . "\n"; | ||
| 216 | push @NE2tokens, $token; | ||
| 217 | $token = $token->next; | ||
| 218 | } while($token->getFrom < $to); | ||
| 219 | |||
| 220 | my $namedEntity = Lingua::Ogmios::Annotations::SemanticUnit->newNamedEntity( | ||
| 221 | {'form' => $NE_form, | ||
| 222 | 'named_entity_type' => $type, | ||
| 223 | 'list_refid_token' => \@NE2tokens, | ||
| 224 | }); | ||
| 225 | |||
| 226 | |||
| 227 | return($document->getAnnotations->addSemanticUnit($namedEntity)); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | # sub _processBasicNER { | ||
| 232 | # my ($self, $lang) = @_; | ||
| 233 | |||
| 234 | # warn "[LOG] POS tagger\n"; | ||
| 235 | |||
| 236 | # # $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 237 | |||
| 238 | # # return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{TreeTagger_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename))); | ||
| 239 | |||
| 240 | # warn "[LOG]\n"; | ||
| 241 | # } | ||
| 242 | |||
| 243 | # sub _inputBasicNER { | ||
| 244 | # my ($self) = @_; | ||
| 245 | |||
| 246 | # warn "[LOG] making input\n"; | ||
| 247 | |||
| 248 | # warn "[LOG] done\n"; | ||
| 249 | # } | ||
| 250 | |||
| 251 | |||
| 252 | # sub _outputParsing { | ||
| 253 | # my ($self) = @_; | ||
| 254 | |||
| 255 | # warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 256 | |||
| 257 | # warn "[LOG] done\n"; | ||
| 258 | # } | ||
| 259 | |||
| 260 | sub run { | ||
| 261 | my ($self, $documentSet) = @_; | ||
| 262 | |||
| 263 | # Set variables according the the configuration | ||
| 264 | $self->_documentSet($documentSet); | ||
| 265 | |||
| 266 | warn "*** TODO: check if the level exists\n"; | ||
| 267 | |||
| 268 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 269 | |||
| 270 | $self->BasicNER; | ||
| 271 | |||
| 272 | # my $command_line = $self->_processBasicNER; | ||
| 273 | |||
| 274 | # if ($self->_position eq "last") { | ||
| 275 | # # TODO | ||
| 276 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 277 | warn "print no standard output\n"; | ||
| 278 | } else { | ||
| 279 | # $self->_outputParsing; | ||
| 280 | } | ||
| 281 | # $self->_outputParsing; | ||
| 282 | |||
| 283 | |||
| 284 | # Put log information | ||
| 285 | |||
| 286 | my $information = { 'software_name' => $self->_config->name, | ||
| 287 | 'comments' => $self->_config->comments, | ||
| 288 | 'command_line' => "", | ||
| 289 | 'list_modified_level' => ['semantic_unit_level'], | ||
| 290 | }; | ||
| 291 | $self->_log($information); | ||
| 292 | |||
| 293 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 294 | # You should define a 'run' method for your wrapper\n"; | ||
| 295 | warn "[LOG] done\n"; | ||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | |||
| 302 | __END__ |