| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 1 | package KorAP::XML::Batch::File; |
| 2 | use KorAP::XML::Krill; |
| Akron | b9c3381 | 2020-10-21 16:19:35 +0200 | [diff] [blame] | 3 | # use Mojo::Log; |
| 4 | use Log::Any qw($log); |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 5 | use IO::Compress::Gzip; |
| 6 | use IO::File; |
| 7 | use strict; |
| 8 | use warnings; |
| 9 | |
| Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 10 | # Constructor |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 11 | sub new { |
| 12 | my $class = shift; |
| 13 | my %param = @_; |
| 14 | |
| 15 | bless { |
| Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 16 | cache => $param{cache} // undef, |
| 17 | meta_type => $param{meta_type} || 'I5', |
| 18 | overwrite => $param{overwrite}, |
| 19 | foundry => $param{foundry} || 'Base', |
| 20 | layer => $param{layer} || 'Tokens', |
| 21 | anno => $param{anno} || [[]], |
| Akron | b9c3381 | 2020-10-21 16:19:35 +0200 | [diff] [blame] | 22 | log => $param{log} || $log, |
| Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 23 | koral => $param{koral}, |
| Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 24 | non_word_tokens => $param{non_word_tokens}, |
| Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 25 | non_verbal_tokens => $param{non_verbal_tokens}, |
| Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 26 | gzip => $param{gzip} // 0 |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 27 | }, $class; |
| 28 | }; |
| 29 | |
| Akron | cdf0e00 | 2016-07-08 16:42:04 +0200 | [diff] [blame] | 30 | # Process a file |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 31 | sub process { |
| Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 32 | my ($self, $input, $output) = @_; |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 33 | |
| Akron | cdf0e00 | 2016-07-08 16:42:04 +0200 | [diff] [blame] | 34 | if (!$self->{overwrite} && $output && -e $output) { |
| 35 | $self->{log}->debug($output . ' already exists'); |
| Akron | 13d5662 | 2016-10-31 14:54:49 +0100 | [diff] [blame] | 36 | return -1; |
| Akron | cdf0e00 | 2016-07-08 16:42:04 +0200 | [diff] [blame] | 37 | }; |
| 38 | |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 39 | # Create and parse new document |
| 40 | $input =~ s{([^/])$}{$1/}; |
| 41 | my $doc = KorAP::XML::Krill->new( |
| 42 | path => $input, |
| 43 | meta_type => $self->{meta_type}, |
| Akron | b05b842 | 2019-12-11 13:47:57 +0100 | [diff] [blame] | 44 | cache => $self->{cache}, |
| 45 | log => $self->{log} |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 46 | ); |
| 47 | |
| 48 | # Parse document |
| 49 | unless ($doc->parse) { |
| 50 | $self->{log}->warn(($output // $input) . " can't be processed - no document data"); |
| 51 | return; |
| 52 | }; |
| 53 | |
| 54 | # Get tokenization |
| 55 | my $tokens = KorAP::XML::Tokenizer->new( |
| 56 | path => $doc->path, |
| 57 | doc => $doc, |
| 58 | foundry => $self->{foundry}, |
| 59 | layer => $self->{layer}, |
| Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 60 | name => 'tokens', |
| Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 61 | non_word_tokens => $self->{non_word_tokens}, |
| 62 | non_verbal_tokens => $self->{non_verbal_tokens} |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 63 | ); |
| 64 | |
| 65 | # Unable to process base tokenization |
| 66 | unless ($tokens->parse) { |
| Akron | f021ad6 | 2019-03-08 17:25:59 +0100 | [diff] [blame] | 67 | $self->{log}->error(($output // $input) . " can't be processed - " . $tokens->error); |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 68 | return; |
| 69 | }; |
| 70 | |
| 71 | foreach (@{$self->{anno}}) { |
| 72 | $tokens->add(@$_); |
| 73 | }; |
| 74 | |
| 75 | my $file; |
| Akron | 6aed056 | 2020-08-07 16:46:00 +0200 | [diff] [blame] | 76 | my $print_text = $tokens->to_json($self->{koral}); |
| Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 77 | |
| 78 | # There is an output file given |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 79 | if ($output) { |
| Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 80 | |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 81 | if ($self->{gzip}) { |
| Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 82 | $file = IO::Compress::Gzip->new($output, TextFlag => 1, Minimal => 1); |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 83 | } |
| 84 | else { |
| Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 85 | $file = IO::File->new($output, "w"); # '>:encoding(UTF-8)'); # "w"); |
| Akron | 158bd50 | 2020-04-02 12:28:00 +0200 | [diff] [blame] | 86 | # Unable to open for writing |
| 87 | }; |
| 88 | |
| 89 | # Output not opened |
| 90 | unless (defined $file) { |
| 91 | $self->{log}->error('Unable to open ' . $output . ' for writing'); |
| 92 | return; |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
| Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 95 | # Write to output |
| 96 | unless ($file->print($print_text)) { |
| 97 | $self->{log}->error('Unable to write to ' . $file); |
| 98 | }; |
| 99 | |
| 100 | # Flush pending data |
| 101 | # $file->flush if $self->{gzip}; |
| 102 | |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 103 | $file->close; |
| 104 | } |
| 105 | |
| Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 106 | # Direct output to STDOUT |
| Akron | 405f0c5 | 2016-07-07 17:56:16 +0200 | [diff] [blame] | 107 | else { |
| 108 | print $print_text . "\n"; |
| 109 | }; |
| 110 | |
| 111 | return 1; |
| 112 | }; |
| 113 | |
| 114 | 1; |
| Akron | cdf0e00 | 2016-07-08 16:42:04 +0200 | [diff] [blame] | 115 | |
| 116 | __END__ |
| 117 | |
| 118 | =pod |
| 119 | |
| 120 | =encoding utf8 |
| 121 | |
| 122 | =head1 NAME |
| 123 | |
| 124 | KorAP::XML::Batch::File - Process multiple files with identical setup |
| 125 | |
| 126 | |
| 127 | =head1 SYNOPSIS |
| 128 | |
| 129 | |
| 130 | # Create Converter Object |
| 131 | my $converter = KorAP::XML::Batch::File->new( |
| 132 | overwrite => 1, |
| 133 | gzip => 1 |
| 134 | ); |
| 135 | |
| 136 | $converter->process('/my/data' => 'my-output.gz'); |
| 137 | |
| 138 | =head1 DESCRIPTION |
| 139 | |
| 140 | Set up the configuration for a corpus and process |
| 141 | multiple texts with the same configuration. |
| 142 | |
| 143 | =head1 METHODS |
| 144 | |
| 145 | Construct a new converter object. |
| 146 | |
| 147 | my $converter = KorAP::XML::Batch::File->new( |
| 148 | overwrite => 1, |
| 149 | gzip => 1 |
| 150 | ); |
| 151 | |
| 152 | |
| 153 | =head2 new |
| 154 | |
| 155 | =over 2 |
| 156 | |
| 157 | =item cache |
| 158 | |
| 159 | A L<Cache::FastMmap> compatible cache object. |
| 160 | |
| 161 | =item meta_type |
| 162 | |
| 163 | Meta data type to be parsed. Defaults to C<I5>, |
| 164 | also supports all classes in the C<KorAP::XML::Meta> namespace. |
| 165 | |
| 166 | =item overwrite |
| 167 | |
| 168 | Overwrite existing files! |
| 169 | Defaults to C<false>. |
| 170 | |
| 171 | =item foundry |
| 172 | |
| 173 | The foundry to use for tokenization, |
| 174 | defaults to C<Base>. |
| 175 | |
| 176 | =item layer |
| 177 | |
| 178 | The layer to use for tokenization, |
| 179 | defaults to C<Tokens>. |
| 180 | |
| 181 | =item anno |
| 182 | |
| 183 | my $converter = KorAP::XML::Batch::File->new( |
| 184 | anno => [ |
| 185 | ['CoreNLP', 'Morpho'], |
| 186 | ['OpenNLP', 'Morpho'] |
| 187 | ] |
| 188 | ); |
| 189 | |
| 190 | An array reference of array references, |
| 191 | containing annotation layers as foundry-layer |
| 192 | pairs to parse. |
| 193 | The list is empty by default. |
| 194 | |
| 195 | =item log |
| 196 | |
| Akron | b9c3381 | 2020-10-21 16:19:35 +0200 | [diff] [blame] | 197 | A L<Log::Any> compatible log object. |
| Akron | cdf0e00 | 2016-07-08 16:42:04 +0200 | [diff] [blame] | 198 | |
| Akron | cdf0e00 | 2016-07-08 16:42:04 +0200 | [diff] [blame] | 199 | =item gzip |
| 200 | |
| 201 | Compress the output using Gzip. |
| 202 | This will be ignored, if the output is undefined |
| 203 | (i.e. C<STDOUT>). |
| 204 | Defaults to C<false>. |
| 205 | |
| 206 | =back |
| 207 | |
| 208 | =head2 process |
| 209 | |
| 210 | $converter->process('/mydoc/'); |
| 211 | $converter->process('/mydoc/', '/myoutput.gzip'); |
| 212 | |
| 213 | Process a file and pass to a chosen output. |
| 214 | The first argument is mandatory and |
| 215 | represents the path to the KorapXML text files. |
| 216 | The second argument is optional and |
| 217 | represents a file path to write. |
| 218 | If the second argument is not given, |
| 219 | the process will write to C<STDOUT> |
| 220 | (in that case, the C<gzip> parameter is ignored). |
| 221 | |
| 222 | =head1 AVAILABILITY |
| 223 | |
| 224 | https://github.com/KorAP/KorAP-XML-Krill |
| 225 | |
| 226 | |
| 227 | =head1 COPYRIGHT AND LICENSE |
| 228 | |
| 229 | Copyright (C) 2015-2016, L<IDS Mannheim|http://www.ids-mannheim.de/> |
| 230 | Author: L<Nils Diewald|http://nils-diewald.de/> |
| 231 | |
| 232 | KorAP::XML::Krill is developed as part of the |
| 233 | L<KorAP|http://korap.ids-mannheim.de/> |
| 234 | Corpus Analysis Platform at the |
| 235 | L<Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 236 | member of the |
| 237 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/> |
| 238 | and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project, |
| 239 | funded by the |
| 240 | L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>. |
| 241 | |
| 242 | KorAP::XML::Krill is free software published under the |
| 243 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-Krill/master/LICENSE>. |
| 244 | |
| 245 | =cut |