| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 1 | package KorAP::Document; |
| 2 | use Mojo::Base -base; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 3 | use Mojo::ByteStream 'b'; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 4 | use Mojo::Util qw/encode/; |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 5 | use XML::Fast; |
| 6 | use Try::Tiny; |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 7 | use Carp qw/croak/; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 8 | use KorAP::Document::Primary; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 9 | use Log::Log4perl; |
| 10 | use KorAP::Log; |
| 11 | use Mojo::DOM; |
| 12 | use Data::Dumper; |
| Nils Diewald | d681eab | 2014-11-01 01:18:25 +0000 | [diff] [blame] | 13 | use File::Spec::Functions qw/catdir catfile catpath splitdir splitpath rel2abs/; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 14 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 15 | our @ATTR = qw/text_sigle |
| 16 | doc_sigle |
| 17 | corpus_sigle |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 18 | |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 19 | pub_date |
| 20 | title |
| 21 | sub_title |
| Nils Diewald | a96de62 | 2014-10-31 17:29:23 +0000 | [diff] [blame] | 22 | pub_place |
| 23 | author/; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 24 | |
| 25 | our @ADVANCED_ATTR = qw/publisher |
| 26 | editor |
| 27 | text_type |
| 28 | text_type_art |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 29 | text_type_ref |
| 30 | text_column |
| 31 | text_domain |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 32 | creation_date |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 33 | license |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 34 | pages |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 35 | file_edition_statement |
| 36 | bibl_edition_statement |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 37 | reference |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 38 | language |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 39 | |
| 40 | doc_title |
| 41 | doc_sub_title |
| 42 | doc_editor |
| 43 | doc_author |
| 44 | |
| 45 | corpus_author |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 46 | corpus_title |
| 47 | corpus_sub_title |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 48 | corpus_editor |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 49 | /; |
| 50 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 51 | # Removed: coll_title, coll_sub_title, coll_author, coll_editor |
| 52 | # Introduced: doc_title, doc_sub_title, corpus_editor, doc_editor, corpus_author, doc_author |
| 53 | |
| 54 | |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 55 | has 'path'; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 56 | has [@ATTR, @ADVANCED_ATTR]; |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 57 | |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 58 | has log => sub { |
| 59 | if(Log::Log4perl->initialized()) { |
| 60 | state $log = Log::Log4perl->get_logger(__PACKAGE__); |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 61 | }; |
| 62 | state $log = KorAP::Log->new; |
| 63 | return $log; |
| 64 | }; |
| 65 | |
| 66 | sub new { |
| 67 | my $class = shift; |
| 68 | my $self = bless { @_ }, $class; |
| Nils Diewald | d681eab | 2014-11-01 01:18:25 +0000 | [diff] [blame] | 69 | if (exists $self->{path}) { |
| 70 | $self->{path} = rel2abs($self->{path}); |
| 71 | if ($self->{path} !~ m!\/$!) { |
| 72 | $self->{path} .= '/'; |
| 73 | }; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 74 | }; |
| 75 | return $self; |
| 76 | }; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 77 | |
| 78 | # parse document |
| 79 | sub parse { |
| 80 | my $self = shift; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 81 | |
| Nils Diewald | 98767bb | 2014-04-25 20:31:19 +0000 | [diff] [blame] | 82 | my $data_xml = $self->path . 'data.xml'; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 83 | |
| Nils Diewald | 98767bb | 2014-04-25 20:31:19 +0000 | [diff] [blame] | 84 | my ($rt, $error, $file); |
| 85 | |
| 86 | my $unable = 'Unable to parse document ' . $self->path; |
| 87 | |
| 88 | unless (-e $data_xml) { |
| 89 | $self->log->warn($unable . ' - no data.xml found'); |
| 90 | $error = 1; |
| 91 | } |
| 92 | |
| 93 | else { |
| 94 | $file = b($data_xml)->slurp; |
| 95 | |
| 96 | try { |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 97 | local $SIG{__WARN__} = sub { |
| Nils Diewald | 98767bb | 2014-04-25 20:31:19 +0000 | [diff] [blame] | 98 | $error = 1; |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 99 | }; |
| 100 | $rt = xml2hash($file, text => '#text', attr => '-')->{raw_text}; |
| Nils Diewald | 98767bb | 2014-04-25 20:31:19 +0000 | [diff] [blame] | 101 | } |
| 102 | catch { |
| 103 | $self->log->warn($unable); |
| 104 | $error = 1; |
| 105 | }; |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | return if $error; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 109 | |
| Nils Diewald | 3ece630 | 2013-12-02 18:38:16 +0000 | [diff] [blame] | 110 | $self->log->debug('Parse document ' . $self->path); |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 111 | |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 112 | # Get document id and corpus id |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 113 | if ($rt && $rt->{'-docid'}) { |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 114 | $self->text_sigle($rt->{'-docid'}); |
| 115 | if ($self->text_sigle =~ /^(([^_]+)_[^\._]+?)\.\d+$/) { |
| 116 | $self->corpus_sigle($2); |
| 117 | $self->doc_sigle($1); |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 118 | } |
| 119 | else { |
| Nils Diewald | 3ece630 | 2013-12-02 18:38:16 +0000 | [diff] [blame] | 120 | croak $unable . ': ID not parseable'; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 121 | }; |
| 122 | } |
| 123 | else { |
| Nils Diewald | 3ece630 | 2013-12-02 18:38:16 +0000 | [diff] [blame] | 124 | croak $unable . ': No raw_text found or no ID'; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | # Get primary data |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 128 | my $pd = $rt->{text}; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 129 | if ($pd) { |
| Nils Diewald | 3cf08c7 | 2013-12-16 20:31:10 +0000 | [diff] [blame] | 130 | $self->{pd} = KorAP::Document::Primary->new($pd); |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 131 | } |
| 132 | else { |
| 133 | croak $unable; |
| 134 | }; |
| 135 | |
| Nils Diewald | d681eab | 2014-11-01 01:18:25 +0000 | [diff] [blame] | 136 | my @path = grep { $_ } splitdir($self->path); |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 137 | my @header; |
| 138 | |
| 139 | foreach (0..2) { |
| Nils Diewald | d681eab | 2014-11-01 01:18:25 +0000 | [diff] [blame] | 140 | unshift @header, '/' . catfile(@path, 'header.xml'); |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 141 | pop @path; |
| 142 | }; |
| 143 | my @type = qw/corpus doc text/; |
| 144 | foreach (@header) { |
| 145 | # Get corpus, doc and text meta data |
| 146 | my $type = shift(@type); |
| 147 | $self->_parse_meta($_, $type) if -e $_; |
| 148 | }; |
| 149 | |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 150 | return 1; |
| 151 | }; |
| 152 | |
| 153 | |
| 154 | # Primary data |
| 155 | sub primary { |
| 156 | $_[0]->{pd}; |
| 157 | }; |
| 158 | |
| Nils Diewald | a96de62 | 2014-10-31 17:29:23 +0000 | [diff] [blame] | 159 | #sub author { |
| 160 | # my $self = shift; |
| 161 | # |
| 162 | # # Set authors |
| 163 | # if ($_[0]) { |
| 164 | # return $self->{authors} = [ |
| 165 | # grep { $_ !~ m{^\s*u\.a\.\s*$} } split(/;\s+/, shift()) |
| 166 | # ]; |
| 167 | # } |
| 168 | # return ($self->{authors} // []); |
| 169 | #}; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 170 | |
| 171 | sub text_class { |
| 172 | my $self = shift; |
| 173 | if ($_[0]) { |
| 174 | return $self->{topics} = [ @_ ]; |
| 175 | }; |
| 176 | return ($self->{topics} // []); |
| 177 | }; |
| 178 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 179 | sub keywords { |
| 180 | my $self = shift; |
| 181 | if ($_[0]) { |
| 182 | return $self->{keywords} = [ @_ ]; |
| 183 | }; |
| 184 | return ($self->{keywords} // []); |
| 185 | }; |
| 186 | |
| 187 | |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 188 | sub _parse_meta { |
| 189 | my $self = shift; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 190 | my $header_xml = shift; |
| 191 | my $type = shift; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 192 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 193 | my $file = b($header_xml)->slurp->decode('iso-8859-1'); |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 194 | |
| 195 | my $dom = Mojo::DOM->new($file); |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 196 | |
| Nils Diewald | 682feb0 | 2013-11-29 22:48:40 +0000 | [diff] [blame] | 197 | my $analytic = $dom->at('analytic'); |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 198 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 199 | # There is an analytic element |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 200 | if ($analytic) { |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 201 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 202 | # Get title, subtitle, author, editor |
| 203 | my $title = $analytic->at('h\.title[type=main]'); |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 204 | my $sub_title = $analytic->at('h\.title[type=sub]'); |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 205 | my $author = $analytic->at('h\.author'); |
| 206 | my $editor = $analytic->at('editor'); |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 207 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 208 | $title = $title ? $title->all_text : undef; |
| 209 | $sub_title = $sub_title ? $sub_title->all_text : undef; |
| 210 | $author = $author ? $author->all_text : undef; |
| 211 | $editor = $editor ? $editor->all_text : undef; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 212 | |
| 213 | if ($type eq 'text') { |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 214 | $self->title($title) if $title; |
| 215 | $self->sub_title($sub_title) if $sub_title; |
| 216 | $self->editor($editor) if $editor; |
| 217 | $self->author($author) if $author; |
| 218 | } |
| 219 | elsif ($type eq 'doc') { |
| 220 | $self->doc_title($title) if $title; |
| 221 | $self->doc_sub_title($sub_title) if $sub_title; |
| 222 | $self->doc_author($author) if $author; |
| 223 | $self->doc_editor($editor) if $editor; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 224 | } |
| 225 | elsif ($type eq 'corpus') { |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 226 | $self->corpus_title($title) if $title; |
| 227 | $self->corpus_sub_title($sub_title) if $sub_title; |
| 228 | $self->corpus_author($author) if $author; |
| 229 | $self->corpus_editor($editor) if $editor; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 230 | }; |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 231 | }; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 232 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 233 | # Not in analytic |
| 234 | if ($type eq 'corpus') { |
| 235 | if (my $title = $dom->at('fileDesc > titleStmt > c\.title')) { |
| 236 | $self->corpus_title($title->all_text) if $title->all_text; |
| 237 | }; |
| 238 | } |
| 239 | |
| 240 | # doc title |
| 241 | elsif ($type eq 'doc') { |
| 242 | if (my $title = $dom->at('fileDesc > titleStmt > d\.title')) { |
| 243 | $self->doc_title($title->all_text) if $title->all_text; |
| 244 | }; |
| 245 | } |
| 246 | |
| 247 | # text title |
| 248 | elsif ($type eq 'text') { |
| 249 | unless ($self->title) { |
| 250 | if (my $title = $dom->at('fileDesc > titleStmt > t\.title')) { |
| 251 | $self->title($title->all_text) if $title->all_text; |
| 252 | }; |
| 253 | }; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | # Get PubPlace |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 257 | if (my $place = $dom->at('pubPlace')) { |
| 258 | $self->pub_place($place->all_text) if $place->all_text; |
| 259 | }; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 260 | |
| 261 | # Get Publisher |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 262 | if (my $publisher = $dom->at('imprint publisher')) { |
| 263 | $self->publisher($publisher->all_text) if $publisher->all_text; |
| 264 | }; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 265 | |
| 266 | my $mono = $dom->at('monogr'); |
| 267 | if ($mono) { |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 268 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 269 | # Get title, subtitle, author, editor |
| 270 | my $title = $mono->at('h\.title[type=main]'); |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 271 | my $sub_title = $mono->at('h\.title[type=sub]'); |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 272 | my $author = $mono->at('h\.author'); |
| 273 | my $editor = $mono->at('editor'); |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 274 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 275 | $title = $title ? $title->all_text : undef; |
| 276 | $sub_title = $sub_title ? $sub_title->all_text : undef; |
| 277 | $author = $author ? $author->all_text : undef; |
| 278 | $editor = $editor ? $editor->all_text : undef; |
| 279 | |
| 280 | if ($type eq 'text') { |
| 281 | $self->title($title) if $title; |
| 282 | $self->sub_title($sub_title) if $sub_title; |
| 283 | $self->editor($editor) if $editor; |
| 284 | $self->author($author) if $author; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 285 | } |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 286 | elsif ($type eq 'doc') { |
| 287 | $self->doc_title($title) if $title; |
| 288 | $self->doc_sub_title($sub_title) if $sub_title; |
| 289 | $self->doc_author($author) if $author; |
| 290 | $self->doc_editor($editor) if $editor; |
| 291 | } |
| 292 | elsif ($type eq 'corpus') { |
| 293 | $self->corpus_title($title) if $title; |
| 294 | $self->corpus_sub_title($sub_title) if $sub_title; |
| 295 | $self->corpus_author($author) if $author; |
| 296 | $self->corpus_editor($editor) if $editor; |
| Nils Diewald | 8e323ee | 2014-04-23 17:28:14 +0000 | [diff] [blame] | 297 | }; |
| 298 | }; |
| 299 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 300 | # Get text type |
| 301 | my $text_desc = $dom->at('textDesc'); |
| 302 | |
| 303 | if ($text_desc) { |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 304 | if (my $text_type = $text_desc->at('textType')) { |
| 305 | $self->text_type($text_type->all_text) if $text_type->all_text; |
| 306 | }; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 307 | |
| 308 | # Get text domain |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 309 | if (my $text_domain = $text_desc->at('textDomain')) { |
| 310 | $self->text_domain($text_domain->all_text) if $text_domain->all_text; |
| 311 | }; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 312 | |
| 313 | # Get text type art |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 314 | if (my $text_type_art = $text_desc->at('textTypeArt')) { |
| 315 | $self->text_type_art($text_type_art->all_text) if $text_type_art->all_text; |
| 316 | }; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 317 | |
| 318 | # Get text type art |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 319 | if (my $text_type_ref = $text_desc->at('textTypeRef')) { |
| 320 | $self->text_type_ref($text_type_ref->all_text) if $text_type_ref->all_text; |
| 321 | }; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | # Get pubDate |
| 325 | my $pub_date = $dom->find('pubDate[type=year]'); |
| 326 | $pub_date->each( |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 327 | sub { |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 328 | my $x = shift->parent; |
| 329 | my $year = $x->at("pubDate[type=year]"); |
| 330 | return unless $year; |
| 331 | |
| 332 | $year = $year ? $year->text : 0; |
| 333 | my $month = $x->at("pubDate[type=month]"); |
| 334 | $month = $month ? $month->text : 0; |
| 335 | my $day = $x->at("pubDate[type=day]"); |
| 336 | $day = $day ? $day->text : 0; |
| 337 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 338 | $year = 0 if $year !~ /^\d+$/; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 339 | $month = 0 if $month !~ /^\d+$/; |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 340 | $day = 0 if $day !~ /^\d+$/; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 341 | |
| 342 | my $date = $year ? ($year < 100 ? '20' . $year : $year) : '0000'; |
| 343 | $date .= length($month) == 1 ? '0' . $month : $month; |
| 344 | $date .= length($day) == 1 ? '0' . $day : $day; |
| 345 | $self->pub_date($date); |
| 346 | }); |
| 347 | |
| 348 | # creatDate |
| 349 | my $create_date = $dom->at('creatDate'); |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 350 | if ($create_date && $create_date->text) { |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 351 | $create_date = $create_date->all_text; |
| 352 | if (index($create_date, '-') > -1) { |
| 353 | $self->log->warn("Creation date ranges are not supported"); |
| 354 | ($create_date) = split /\s*-\s*/, $create_date; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 355 | } |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 356 | |
| 357 | $create_date =~ s{^(\d{4})$}{$1\.00}; |
| 358 | $create_date =~ s{^(\d{4})\.(\d{2})$}{$1\.$2\.00}; |
| 359 | if ($create_date =~ /^\d{4}\.\d{2}\.\d{2}$/) { |
| 360 | $create_date =~ tr/\.//d; |
| 361 | $self->creation_date($create_date); |
| 362 | }; |
| 363 | }; |
| 364 | |
| 365 | my $text_class = $dom->at('textClass'); |
| 366 | if ($text_class) { |
| 367 | # Get textClasses |
| 368 | my @topic; |
| 369 | |
| 370 | $text_class->find("catRef")->each( |
| 371 | sub { |
| 372 | my ($ign, @ttopic) = split('\.', $_->attr('target')); |
| 373 | push(@topic, @ttopic); |
| 374 | } |
| 375 | ); |
| 376 | $self->text_class(@topic) if @topic > 0; |
| 377 | |
| 378 | my @keywords = $text_class->find("h\.keywords > keyTerm")->each; |
| 379 | $self->keywords(@keywords) if @keywords > 0; |
| 380 | }; |
| 381 | |
| 382 | if (my $edition_statement = $dom->at('biblFull editionStmt')) { |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 383 | $self->bibl_edition_statement($edition_statement->all_text) |
| 384 | if $edition_statement->text; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | if (my $edition_statement = $dom->at('fileDescl editionStmt')) { |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 388 | $self->file_edition_statement($edition_statement->all_text) |
| 389 | if $edition_statement->text; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | if (my $file_desc = $dom->at('fileDesc')) { |
| 393 | if (my $availability = $file_desc->at('publicationStmt > availability')) { |
| 394 | $self->license($availability->all_text); |
| 395 | }; |
| 396 | }; |
| 397 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 398 | # Some meta data only available in the corpus |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 399 | if ($type eq 'corpus') { |
| 400 | if (my $language = $dom->at('profileDesc > langUsage > language[id]')) { |
| 401 | $self->language($language->attr('id')); |
| 402 | }; |
| 403 | } |
| 404 | |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 405 | # Some meta data only reevant from the text |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 406 | elsif ($type eq 'text') { |
| 407 | |
| 408 | if (my $reference = $dom->at('sourceDesc reference[type=complete]')) { |
| 409 | if (my $ref_text = $reference->all_text) { |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 410 | $ref_text =~ s!^[a-zA-Z0-9]+\/[a-zA-Z0-9]+\.\d+[\s:]\s*!!; |
| 411 | $self->reference($ref_text); |
| 412 | }; |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | my $column = $dom->at('textDesc > column'); |
| 416 | $self->text_column($column->all_text) if $column; |
| 417 | |
| 418 | if (my $pages = $dom->at('biblStruct biblScope[type="pp"]')) { |
| 419 | $pages = $pages->all_text; |
| 420 | if ($pages && $pages =~ m/(\d+)\s*-\s*(\d+)/) { |
| 421 | $self->pages($1 . '-' . $2); |
| 422 | }; |
| 423 | }; |
| Nils Diewald | 90410c2 | 2014-11-03 21:04:05 +0000 | [diff] [blame] | 424 | }; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 427 | |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 428 | sub to_string { |
| 429 | my $self = shift; |
| 430 | |
| 431 | my $string; |
| 432 | |
| 433 | foreach (@ATTR) { |
| 434 | if (my $att = $self->$_) { |
| 435 | $att =~ s/\n/ /g; |
| 436 | $att =~ s/\s\s+/ /g; |
| 437 | $string .= $_ . ' = ' . $att . "\n"; |
| 438 | }; |
| 439 | }; |
| 440 | |
| Nils Diewald | a96de62 | 2014-10-31 17:29:23 +0000 | [diff] [blame] | 441 | # if ($self->author) { |
| 442 | # foreach (@{$self->author}) { |
| 443 | # $_ =~ s/\n/ /g; |
| 444 | # $_ =~ s/\s\s+/ /g; |
| 445 | # $string .= 'author = ' . $_ . "\n"; |
| 446 | # }; |
| 447 | # }; |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 448 | |
| 449 | if ($self->text_class) { |
| 450 | foreach (@{$self->text_class}) { |
| 451 | $string .= 'text_class = ' . $_ . "\n"; |
| 452 | }; |
| 453 | }; |
| 454 | |
| 455 | return $string; |
| 456 | }; |
| 457 | |
| Nils Diewald | 044c41d | 2013-11-11 21:45:09 +0000 | [diff] [blame] | 458 | sub _k { |
| 459 | my $x = $_[0]; |
| 460 | $x =~ s/_(\w)/\U$1\E/g; |
| 461 | $x =~ s/id$/ID/gi; |
| 462 | return $x; |
| 463 | }; |
| 464 | |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 465 | |
| 466 | sub to_hash { |
| 467 | my $self = shift; |
| 468 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 469 | $self->parse unless $self->text_sigle; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 470 | |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 471 | my %hash; |
| 472 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 473 | foreach (@ATTR, @ADVANCED_ATTR) { |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 474 | if (my $att = $self->$_) { |
| 475 | $att =~ s/\n/ /g; |
| 476 | $att =~ s/\s\s+/ /g; |
| Nils Diewald | 044c41d | 2013-11-11 21:45:09 +0000 | [diff] [blame] | 477 | $hash{_k($_)} = $att; |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 478 | }; |
| 479 | }; |
| 480 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 481 | for (qw/text_class keywords/) { |
| 482 | my @array = @{ $self->$_ }; |
| 483 | next unless @array; |
| 484 | $hash{_k($_)} = join(' ', @array); |
| Nils Diewald | 37e5b57 | 2013-11-20 20:26:03 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
| Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 487 | return \%hash; |
| 488 | }; |
| 489 | |
| 490 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 491 | # Don't work that well |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 492 | sub _parse_meta_fast { |
| 493 | my $self = shift; |
| 494 | |
| 495 | # my $file = b($self->path . 'header.xml')->slurp->decode('iso-8859-1'); |
| 496 | my $file = b($self->path . 'header.xml')->slurp; |
| 497 | |
| 498 | my ($meta, $error); |
| Nils Diewald | 98767bb | 2014-04-25 20:31:19 +0000 | [diff] [blame] | 499 | my $unable = 'Unable to parse document ' . $self->path; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 500 | |
| 501 | try { |
| 502 | local $SIG{__WARN__} = sub { |
| 503 | $error = 1; |
| 504 | }; |
| Nils Diewald | 6a2a14b | 2015-06-17 20:34:24 +0000 | [diff] [blame^] | 505 | $meta = xml2hash( |
| 506 | $file, |
| 507 | text => '#text', |
| 508 | attr => '-', |
| 509 | array => ['h.title', 'imprint', 'catRef', 'h.author'] |
| 510 | )->{idsHeader}; |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 511 | } |
| 512 | catch { |
| 513 | $self->log->warn($unable); |
| 514 | $error = 1; |
| 515 | }; |
| 516 | |
| 517 | return if $error; |
| 518 | |
| 519 | my $bibl_struct = $meta->{fileDesc}->{sourceDesc}->{biblStruct}; |
| 520 | my $analytic = $bibl_struct->{analytic}; |
| 521 | |
| 522 | my $titles = $analytic->{'h.title'}; |
| 523 | foreach (@$titles) { |
| 524 | if ($_->{'-type'} eq 'main') { |
| 525 | $self->title($_->{'#text'}); |
| 526 | } |
| 527 | elsif ($_->{'-type'} eq 'sub') { |
| 528 | $self->sub_title($_->{'#text'}); |
| 529 | }; |
| 530 | }; |
| 531 | |
| 532 | # Get Author |
| 533 | if (my $author = $analytic->{'h.author'}) { |
| 534 | $self->author($author->[0]); |
| 535 | }; |
| 536 | |
| 537 | # Get pubDate |
| 538 | my $date = $bibl_struct->{monogr}->{imprint}; |
| 539 | my ($year, $month, $day) = (0,0,0); |
| 540 | foreach (@$date) { |
| Nils Diewald | 7b84722 | 2014-04-23 11:14:00 +0000 | [diff] [blame] | 541 | if ($date->{-type} eq 'year') { |
| 542 | $year = $date->{'#text'}; |
| 543 | } |
| 544 | elsif ($date->{-type} eq 'month') { |
| 545 | $month = $date->{'#text'}; |
| 546 | } |
| 547 | elsif ($date->{-type} eq 'day') { |
| 548 | $day = $date->{'#text'}; |
| 549 | }; |
| 550 | }; |
| 551 | |
| 552 | $year = 0 if $year !~ /^\d+$/; |
| 553 | $month = 0 if $month !~ /^\d+$/; |
| 554 | $day = 0 if $day !~ /^\d+$/; |
| 555 | |
| 556 | $date = $year ? ($year < 100 ? '20' . $year : $year) : '0000'; |
| 557 | $date .= length($month) == 1 ? '0' . $month : $month; |
| 558 | $date .= length($day) == 1 ? '0' . $day : $day; |
| 559 | |
| 560 | $self->pub_date($date); |
| 561 | |
| 562 | # Get textClasses |
| 563 | my @topic; |
| 564 | my $textClass = $meta->{profileDesc}->{textClass}->{catRef}; |
| 565 | foreach (@$textClass) { |
| 566 | my ($ign, @ttopic) = split('\.', $_->{'-target'}); |
| 567 | push(@topic, @ttopic); |
| 568 | }; |
| 569 | $self->text_class(@topic); |
| 570 | }; |
| 571 | |
| 572 | |
| 573 | |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 574 | 1; |
| 575 | |
| 576 | |
| 577 | __END__ |
| 578 | |
| 579 | =pod |
| 580 | |
| 581 | =head1 NAME |
| 582 | |
| 583 | KorAP::Document |
| 584 | |
| 585 | |
| 586 | =head1 SYNOPSIS |
| 587 | |
| 588 | my $doc = KorAP::Document->new( |
| 589 | path => 'mydoc-1/' |
| 590 | ); |
| 591 | |
| 592 | $doc->parse; |
| 593 | |
| 594 | print $doc->title; |
| 595 | |
| 596 | |
| 597 | =head1 DESCRIPTION |
| 598 | |
| 599 | Parse the primary and meta data of a document. |
| 600 | |
| 601 | |
| 602 | =head2 ATTRIBUTES |
| 603 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 604 | =head2 text_sigle |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 605 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 606 | $doc->text_sigle(75476); |
| 607 | print $doc->text_sigle; |
| 608 | |
| 609 | The unique identifier of the text. |
| 610 | |
| 611 | |
| 612 | =head2 doc_sigle |
| 613 | |
| 614 | $doc->doc_sigle(75476); |
| 615 | print $doc->doc_sigle; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 616 | |
| 617 | The unique identifier of the document. |
| 618 | |
| 619 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 620 | =head2 corpus_sigle |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 621 | |
| Nils Diewald | 840c924 | 2014-10-28 19:51:26 +0000 | [diff] [blame] | 622 | $doc->corpus_sigle(4); |
| 623 | print $doc->corpus_sigle; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 624 | |
| 625 | The unique identifier of the corpus. |
| 626 | |
| 627 | |
| 628 | =head2 path |
| 629 | |
| 630 | $doc->path("example-004/"); |
| 631 | print $doc->path; |
| 632 | |
| 633 | The path of the document. |
| 634 | |
| 635 | |
| 636 | =head2 title |
| 637 | |
| 638 | $doc->title("Der Name der Rose"); |
| 639 | print $doc->title; |
| 640 | |
| 641 | The title of the document. |
| 642 | |
| 643 | |
| 644 | =head2 sub_title |
| 645 | |
| 646 | $doc->sub_title("Natürlich eine Handschrift"); |
| 647 | print $doc->sub_title; |
| 648 | |
| 649 | The title of the document. |
| 650 | |
| 651 | |
| 652 | =head2 pub_place |
| 653 | |
| 654 | $doc->pub_place("Rom"); |
| 655 | print $doc->pub_place; |
| 656 | |
| 657 | The publication place of the document. |
| 658 | |
| 659 | |
| 660 | =head2 pub_date |
| 661 | |
| 662 | $doc->pub_place("19800404"); |
| 663 | print $doc->pub_place; |
| 664 | |
| 665 | The publication date of the document, |
| 666 | in the format "YYYYMMDD". |
| 667 | |
| 668 | |
| 669 | =head2 primary |
| 670 | |
| 671 | print $doc->primary->data(0,20); |
| 672 | |
| 673 | The L<KorAP::Document::Primary> object containing the primary data. |
| 674 | |
| 675 | |
| 676 | =head2 author |
| 677 | |
| 678 | $doc->author('Binks, Jar Jar; Luke Skywalker'); |
| 679 | print $doc->author->[0]; |
| 680 | |
| 681 | Set the author value as semikolon separated list of names or |
| 682 | get an array reference of author names. |
| 683 | |
| 684 | =head2 text_class |
| 685 | |
| 686 | $doc->text_class(qw/news sports/); |
| 687 | print $doc->text_class->[0]; |
| 688 | |
| 689 | Set the text class as an array or get an array |
| 690 | reference of text classes. |
| 691 | |
| 692 | |
| 693 | =head1 METHODS |
| 694 | |
| 695 | =head2 parse |
| 696 | |
| 697 | $doc->parse; |
| 698 | |
| 699 | Run the parsing process of the document |
| 700 | |
| 701 | |
| 702 | =cut |
| Nils Diewald | 62d1a97 | 2014-07-31 21:49:11 +0000 | [diff] [blame] | 703 | |
| 704 | |
| 705 | Deal with: |
| 706 | <attribute name="info"> |
| 707 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">kind of |
| 708 | information expressed by the given layer of annotation (there may, and often will, be |
| 709 | more than one)</documentation> |
| 710 | <list> |
| 711 | <oneOrMore> |
| 712 | <choice> |
| 713 | <value type="NCName">pos</value> |
| 714 | <value type="NCName">lemma</value> |
| 715 | <value type="NCName">msd</value> |
| 716 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">'msd' is |
| 717 | the traditional abbreviation for "morphosyntactic description", listing info on |
| 718 | e.g. tense, person, case, etc.</documentation> |
| 719 | <value type="NCName">dep</value> |
| 720 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">'dep' is |
| 721 | information about types of relations, used in dependency-style annotations; it is |
| 722 | an indication for the visualiser that word-to-word relationships should be |
| 723 | displayed</documentation> |
| 724 | <value type="NCName">lbl</value> |
| 725 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">'lbl' |
| 726 | indicates the presence of labels over dependency relations</documentation> |
| 727 | <value type="NCName">const</value> |
| 728 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">'const' |
| 729 | stands for 'constituency' or hierarchical, tree-based annotations; it is an |
| 730 | indication for the visualiser that it should display syntactic |
| 731 | trees</documentation> |
| 732 | <value type="NCName">cat</value> |
| 733 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">'cat' is |
| 734 | used for syntactic categories, as separate from pos; note that these sets need not |
| 735 | be disjoint (at the lexical level, they usually overlap), but the frontend prefers |
| 736 | to keep them separate. 'cat' will be found in the context of chunking or |
| 737 | hierarchical parsing and will characterise nodes; it may also be found in |
| 738 | dependency annotations, to indicate labels on nodes, as opposed to labels on arcs |
| 739 | (the latter are signalled by 'lbl')</documentation> |
| 740 | <value type="NCName">struct</value> |
| 741 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">all |
| 742 | non-linguistic information (headers, highlights, etc.)</documentation> |
| 743 | <value type="NCName">frag</value> |
| 744 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0" |
| 745 | >non-exhaustive coverage (when spanList/@fragmented="true")</documentation> |
| 746 | <value type="NCName">ne</value> |
| 747 | <documentation xmlns="http://relaxng.org/ns/compatibility/annotations/1.0">named |
| 748 | entities</documentation> |
| 749 | </choice> |
| 750 | </oneOrMore> |
| 751 | </list> |
| 752 | </attribute> |