blob: 61d48ebaadde7bde73fa6be595455afb59fcbdfb [file] [log] [blame]
Marc Kupietz5e7f20a2020-02-17 18:17:11 +01001#!/usr/bin/env perl
2use strict;
3use warnings;
4use POSIX;
Marc Kupietz6a79cad2021-03-19 16:26:58 +01005use Log::Any '$log';
6use Log::Any::Adapter;
7use Pod::Usage;
8use Getopt::Long qw(GetOptions :config no_auto_abbrev);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +01009use Encode;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010010
11my $MAX_SENTENCE_LENGTH=10000;
12my $COMMENT_START="#";
Marc Kupietza2680b92021-10-11 17:24:28 +020013my $COMMENT_END="";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010014
15my $test=0;
16my $text_no=0;
17my %opts;
18my %plain_texts;
Marc Kupietzd8455832021-02-11 17:30:29 +010019my %sentence_ends;
Marc Kupietzd0bf2772022-06-26 19:27:58 +020020my %metadata;
21
22my $offsets = 1;
23my $comments = 1;
24my $extract_metadata = 0;
25my @extract_metadata_regex;
26
27my $lm_training_data = 0;
Marc Kupietzd8455832021-02-11 17:30:29 +010028
Marc Kupietz4cc243a2021-10-11 17:15:16 +020029our $VERSION = '0.4.1.9000';
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010030
Marc Kupietz0ab8a2c2021-03-19 16:21:00 +010031our $VERSION_MSG = "\nkorapxml2conllu - v$VERSION\n";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010032
Marc Kupietz6a79cad2021-03-19 16:26:58 +010033use constant {
34 # Set to 1 for minimal more debug output (no need to be parametrized)
35 DEBUG => $ENV{KORAPXMLCONLLU_DEBUG} // 0
36};
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010037
Marc Kupietz6a79cad2021-03-19 16:26:58 +010038GetOptions(
39 'sigle-pattern|p=s' => \(my $sigle_pattern = ''),
Marc Kupietzeb7d06a2021-03-19 16:29:16 +010040 'extract-attributes-regex|e=s' => \(my $extract_attributes_regex = ''),
Marc Kupietz15c84fd2021-10-12 12:20:27 +020041 's-bounds-from-morpho' => \(my $s_bounds_from_morpho = 0),
Marc Kupietz6a79cad2021-03-19 16:26:58 +010042 'log|l=s' => \(my $log_level = 'warn'),
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +020043 'columns|c=n' => \(my $columns = 10),
Marc Kupietzd0bf2772022-06-26 19:27:58 +020044 'word2vec|lm-training-data!' => \$lm_training_data,
45 'token-separator|s=s' => \(my $token_separator = "\n"),
46 'offsets!' => \$offsets,
47 'comments!' => \$comments,
48 'extract-metadata-regex|m=s@' => \@extract_metadata_regex,
Marc Kupietz6a79cad2021-03-19 16:26:58 +010049 'help|h' => sub {
50 pod2usage(
51 -verbose => 99,
52 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS|EXAMPLES',
53 -msg => $VERSION_MSG,
54 -output => '-'
55 )
56 },
57 'version|v' => sub {
58 pod2usage(
59 -verbose => 0,
60 -msg => $VERSION_MSG,
61 -output => '-'
62 );
63 }
64);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010065
Marc Kupietzd0bf2772022-06-26 19:27:58 +020066if (@extract_metadata_regex) {
67 $extract_metadata = 1;
Marc Kupietzd0bf2772022-06-26 19:27:58 +020068}
69
Marc Kupietz6a79cad2021-03-19 16:26:58 +010070# Establish logger
71binmode(STDERR, ':encoding(UTF-8)');
72Log::Any::Adapter->set('Stderr', log_level => $log_level);
73$log->notice('Debugging is activated') if DEBUG;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010074
Marc Kupietzd0bf2772022-06-26 19:27:58 +020075if ($lm_training_data) {
76 $columns = 1;
77 $comments = 0;
78 $offsets = 0;
79 $token_separator = " ";
80}
81
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010082my $docid="";
83my ($current_id, $current_from, $current_to, $token);
84my $current;
85my ($unknown, $known) = (0, 0);
86my @current_lines;
87my %processedFilenames;
88my $zipsiglepattern = (defined($ENV{ZIPSIGLEPATTERN})? $ENV{ZIPSIGLEPATTERN} : "");
Marc Kupietzd8455832021-02-11 17:30:29 +010089my $baseOnly;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +010090my %extras;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010091
92my ($ID_idx, $FORM_idx, $LEMMA_idx, $UPOS_idx, $XPOS_idx, $FEATS_idx, $HEAD_idx, $DEPREC_idx, $DEPS_idx, $MISC_idx) = (0..9);
93
Marc Kupietzc7d1b932020-09-23 13:17:17 +020094my $UNZIP = `sh -c 'command -v unzip'`;
95chomp $UNZIP;
96
97
98if ($UNZIP eq '') {
99 warn('No unzip executable found in PATH.');
100 return 0;
101};
102
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100103foreach my $morpho_zip (@ARGV) {
104 die "cannot open $morpho_zip" if(! -r $morpho_zip);
105 my $data_zip = $morpho_zip;
106 if ($data_zip !~ /\.zip/ && $data_zip =~ /\.conllu?/i) {
107 open(CONLL, "<$data_zip") or die "cannot open $data_zip";
108 while(<CONLL>) {
109 print;
110 }
111 close(CONLL);
112 next;
113 }
Marc Kupietzfdd2d122021-10-12 10:42:34 +0200114 $data_zip =~ s/\.([^\/.]+)\.zip$/.zip/;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100115 my $foundry = $1;
116 die "cannot open data file $data_zip corresponding to $morpho_zip" if(! -r $data_zip);
117
118 my $first=1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100119 my @conll = ("_") x 10;
120 my $filename;
121
Marc Kupietzd8455832021-02-11 17:30:29 +0100122 $baseOnly = $morpho_zip eq $data_zip;
123 my ($morphoOrTokenCommand, $plaintextAndStructureCommand);
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200124 my $zip_content_pattern = "[sd][ta]*";
125 if ($extract_metadata) {
126 $zip_content_pattern = "[sdh][tae]*";
127 }
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100128 if (!$baseOnly) {
129 $morphoOrTokenCommand = "$UNZIP -c $morpho_zip '*/${sigle_pattern}*/*/*/morpho.xml' $zipsiglepattern |";
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200130 if ($extract_attributes_regex || !$s_bounds_from_morpho) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200131 $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/$zip_content_pattern.xml' $zipsiglepattern |";
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100132 } else {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200133 $log->debug("Not reading structure information.");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100134 $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/data.xml' $zipsiglepattern |";
135 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100136 } else {
137 $foundry = "base";
Marc Kupietzf1fdc192021-10-08 13:29:59 +0200138 $morphoOrTokenCommand = "$UNZIP -l $morpho_zip '*/${sigle_pattern}*/*/*/morpho.xml' $zipsiglepattern";
139 if (`$morphoOrTokenCommand` !~ /morpho\.xml/) {
140 $morphoOrTokenCommand =~ s/morpho\.xml/tokens.xml/;
141 } else {
142 $baseOnly = 0;
143 }
144 $morphoOrTokenCommand =~ s/-l/-c/;
145 $morphoOrTokenCommand .= ' |';
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200146 $plaintextAndStructureCommand = "$UNZIP -c $data_zip " . "'*/${sigle_pattern}*/*/$zip_content_pattern.xml' " . "$zipsiglepattern |";
Marc Kupietzd8455832021-02-11 17:30:29 +0100147 }
148
149 open (MORPHO_OR_TOKENPIPE, $morphoOrTokenCommand) or die "cannot unzip $morpho_zip";
150 open (PLAINTEXTPIPE, $plaintextAndStructureCommand) or die "cannot unzip $data_zip";
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200151 print "$COMMENT_START foundry = $foundry$COMMENT_END\n" if ($comments);
Marc Kupietzd8455832021-02-11 17:30:29 +0100152 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz30c41b12020-09-22 14:32:34 +0200153 if (/^ inflating: (.*)/) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100154 $filename=$1;
Marc Kupietzd8455832021-02-11 17:30:29 +0100155 while($processedFilenames{$filename} && !eof(MORPHO_OR_TOKENPIPE)) {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100156 $log->warn("$filename already processed");
Marc Kupietzd8455832021-02-11 17:30:29 +0100157 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100158 last if(/\s+inflating:\s+(.*)/);
159 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100160 $filename=$1 if(!eof(MORPHO_OR_TOKENPIPE) && /\s+inflating:\s+(.*)/);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100161 }
Marc Kupietz30c41b12020-09-22 14:32:34 +0200162 } elsif(m@^\s*<layer\s+.*docid="([^"]+)"@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100163 last if($test && $text_no++ > 3);
164 if(!$first) {
165 closeDoc(0);
166 }
167 $processedFilenames{$filename}=1;
168 $docid=$1;
169 @current_lines=();
170 $known=$unknown=0;
171 $current="";
172 if ($first) {
173 $first = 0;
174 }
175 if(!fetch_plaintext($docid)) { # skip this text
Marc Kupietzd8455832021-02-11 17:30:29 +0100176 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100177 last if(m@</layer>@);
178 }
179 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200180 print STDOUT "$COMMENT_START filename = $filename$COMMENT_END\n$COMMENT_START text_id = $docid$COMMENT_END\n" if($comments);
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100181 $log->debug("Analyzing $docid");
Marc Kupietz30c41b12020-09-22 14:32:34 +0200182 } elsif (m@^\s*<f\s+.*name="([^"]+)">([^<]+)</f>@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100183 if ($1 eq "lemma") {
184 $conll[$LEMMA_idx] = $2;
185 $conll[$LEMMA_idx] =~ s/[\t\n\r]//g; # make sure that lemmas never contain tabs or newlines
186 if($conll[$LEMMA_idx] eq 'UNKNOWN') {
187 $conll[$LEMMA_idx] = "--";
188 $unknown++;
189 } else {
190 $known++;
191 }
192 } elsif ($1 eq 'pos' || $1 eq "ctag") {
193 $unknown++;
194 $conll[$XPOS_idx] = $conll[$UPOS_idx] = $2;
195 } elsif ($1 eq 'msd') {
196 $conll[$FEATS_idx] = $2;
197 } elsif ($1 eq 'certainty') {
198 $conll[$MISC_idx] = $2;
199 }
200 } elsif (/<span /) {
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100201 my $last_from = $current_from // -1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100202 ($current_id) = /id="[^0-9]*([^\"]*)"/;
203 ($current_from) = /from="([^\"]*)"/;
204 ($current_to) = /to="([^\"]*)"/;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100205 if($extract_attributes_regex) {
206 for (my $i = $last_from + 1; $i <= $current_from; $i++) {
207 if ($extras{$docid}{$i}) {
208 $current .= $extras{$docid}{$i};
209 undef $extras{$docid}{$i};
210 }
211 }
212 }
Marc Kupietz1db65e52021-07-31 23:38:07 +0200213# $log->debug("found span: $current_id $current_from $current_to");
Marc Kupietz7e71a822020-06-22 17:14:30 +0200214 $token = substr($plain_texts{$docid}, $current_from, $current_to - $current_from);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100215 if (!defined $token) {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100216 $log->warn("could not retrieve token for $docid at $current_from-$current_to/", length($plain_texts{$docid}), " - ending with: ", substr($plain_texts{$docid},length($plain_texts{$docid})-10));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100217 $token = "_";
218 }
219 $token=~s/[\t\n\r]//g; # make sure that tokens never contain tabs or newlines
220 @conll = ("_") x 10;
221 $conll[$FORM_idx] = encode("utf-8", $token);
Marc Kupietzd8455832021-02-11 17:30:29 +0100222 if($baseOnly) {
223 my @vals = ($current_from, $current_to);
Marc Kupietz1db65e52021-07-31 23:38:07 +0200224# $log->debug("joining : ", join(" ", @vals));
Marc Kupietzd8455832021-02-11 17:30:29 +0100225 push @current_lines, \@vals;
226 $known++;
227 $conll[$ID_idx] = $#current_lines+1;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200228 if ($columns == 1) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200229 $current .= "$conll[1]$token_separator" ;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200230 } else {
231 $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns
232 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100233 fetch_plaintext($docid);
234 if ($sentence_ends{$docid}{$current_to}) {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200235 $log->debug("Using sentence end for $docid \@$current_to");
Marc Kupietzd8455832021-02-11 17:30:29 +0100236 $current .= "\n";
237 printTokenRanges();
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200238 if ($extract_metadata) {
239 for (my $i = 0; $i < @extract_metadata_regex; $i++) {
240 print "$metadata{$docid}[$i]\t";
241 }
242 }
243 $current =~ s/ $//;
Marc Kupietzd8455832021-02-11 17:30:29 +0100244 print STDOUT $current;
245 $current = "";
246 $known = 0;
247 $unknown = 0;
248 @current_lines = ();
249 }
250 }
Marc Kupietz30c41b12020-09-22 14:32:34 +0200251 } elsif (m@^\s*</fs>@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100252 my @vals = ($current_from, $current_to);
Marc Kupietz1db65e52021-07-31 23:38:07 +0200253# $log->debug("joining : ", join(" ", @vals));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100254 push @current_lines, \@vals;
255 # convert gathered information to CONLL
256 $conll[$ID_idx] = $#current_lines+1;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200257 if ($columns == 1) {
258 $current .= "$conll[1]\n";
259 } else {
260 $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns
261 }
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200262 if($sentence_ends{$docid}{$current_to} || ($s_bounds_from_morpho && $conll[$XPOS_idx] eq '$.' || ($conll[$XPOS_idx] eq 'SENT' && $token eq '.')) || $known + $unknown >= $MAX_SENTENCE_LENGTH) {
263 $log->debug("Using sentence end for $docid \@$current_to");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100264 $current .= "\n";
265 if($known + $unknown > 0) { # only print sentence if it contains some words
266 printTokenRanges();
267 print STDOUT $current;
268 }
269 $current=""; $known=0; $unknown=0;
270 @current_lines = ();
271 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100272 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100273 last if (m@</span>@); # only consider first interpretation
274 }
275 }
276 }
277 $current .= "\n";
278 closeDoc(1);
Marc Kupietzd8455832021-02-11 17:30:29 +0100279 close(MORPHO_OR_TOKENPIPE);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100280 close(PLAINTEXTPIPE);
281}
282exit;
283
284sub printTokenRanges {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200285 return if(!$offsets);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100286 print "$COMMENT_START start_offsets = ", $current_lines[0]->[0];
287 foreach my $t (@current_lines) {
288 print STDOUT " $t->[0]";
289 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200290 print "$COMMENT_END\n$COMMENT_START end_offsets = ", $current_lines[$#current_lines]->[1] if($comments);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100291 foreach my $t (@current_lines) {
292 print STDOUT " $t->[1]";
293 }
Marc Kupietza2680b92021-10-11 17:24:28 +0200294 print "$COMMENT_END\n";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100295}
296
297sub closeDoc {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100298 $log->debug("closing doc");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100299 if($known + $unknown > 0) { # only parse a sentence if it has some words
300 chomp $current;
301 chomp $current;
302 chomp $current;
303 $current .= "\n\n";
304 printTokenRanges();
305 print STDOUT $current;
306 }
307}
308
309# read data.xml to figure out the tokens
310# (ideally tokens should also be in in morpho.xml, but they are not)
311sub fetch_plaintext {
312 my ($target_id) = @_;
313 my $docid;
314 my $text_started=0;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100315 my $text_count = 0;
Marc Kupietzd8455832021-02-11 17:30:29 +0100316 my ($current_id, $current_from, $current_to);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100317
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200318 if($plain_texts{$target_id} && ($s_bounds_from_morpho || $sentence_ends{$target_id}) && (!$extract_metadata || $metadata{$target_id})) {
Marc Kupietz1db65e52021-07-31 23:38:07 +0200319 $log->debug("Already got $target_id");
Marc Kupietzd8455832021-02-11 17:30:29 +0100320 return 1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100321 }
322 while(<PLAINTEXTPIPE>) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200323 if ($extract_metadata) {
324 if (/<textSigle>([^<]+)/) {
325 $docid = $1;
326 $docid =~ s@/@_@;
327 $log->debug("textsigle=$docid");
328 }
329 for (my $i=0; $i < @extract_metadata_regex; $i++) {
330 if ($_ =~ /$extract_metadata_regex[$i]/) {
331 $metadata{$docid}[$i]=$1;
332 }
333 }
334 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100335 if(/<raw_text[^>]+docid="([^"]*)/) {
336 $docid=$1;
Marc Kupietz1db65e52021-07-31 23:38:07 +0200337 $log->debug("Getting plain text for $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100338 $text_started=0;
Marc Kupietzd8455832021-02-11 17:30:29 +0100339 } elsif(/<layer[^>]+docid="([^"]*)/) {
340 $docid=$1;
Marc Kupietzd8455832021-02-11 17:30:29 +0100341 } elsif(m@<span @) {
342 ($current_id) = /id="[^0-9]*([^\"]*)"/;
343 ($current_from) = /from="([^\"]*)"/;
344 ($current_to) = /to="([^\"]*)"/;
345 } elsif(m@<f\s[^>]*>s</f>@) {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200346 if ($s_bounds_from_morpho) {
347 $log->debug("Ignoring sentence end for $docid \@$current_to because of --s-bounds-from-morpho");
348 } else {
349 $log->debug("Found sentence end for $docid \@$current_to");
350 $sentence_ends{$docid}{$current_to} = 1;
351 }
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100352 } elsif($extract_attributes_regex && m@<f\sname="name"[^>]*>([^<]+)</f>@) {
353 my $current_element = $1;
Marc Kupietz1db65e52021-07-31 23:38:07 +0200354 $log->debug("Looking for matching attributes in $docid");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100355 while(<PLAINTEXTPIPE>) {
356 last if(m@</fs>@);
357 if(m@<f\sname="([^"]+)"[^>]*>([^<]+)</f>@) {
358 my $current_node = "$current_element/$1";
359 my $value = $2;
360 if ($current_node =~ /$extract_attributes_regex/) {
Marc Kupietz1db65e52021-07-31 23:38:07 +0200361 $log->debug("Found matching attribute: $docid - $current_node = $value");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100362 $extras{$docid}{$current_from} .= "# $current_node = $value\n";
363 }
364 }
365 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100366 } elsif (m@<text>(.*)</text>@) {
367 $_= decode("utf-8", $1, Encode::FB_DEFAULT);
368 s/&lt;/</go;
369 s/&gt;/>/go;
370 s/&amp;/&/go;
371 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
372 $plain_texts{$docid} = $_;
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200373 last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id} && (!$extract_metadata || $metadata{$target_id})));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100374 } elsif (m@<text>(.*)@) {
375 $_= decode("utf-8", $1, Encode::FB_DEFAULT);
376 s/&lt;/</go;
377 s/&gt;/>/go;
378 s/&amp;/&/go;
379 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
380 $plain_texts{$docid} = "$_ ";
381 $text_started=1;
382 } elsif ($text_started && m@(.*)</text>@) {
383 $_= decode("utf-8", $1, Encode::FB_DEFAULT);
384 s/&lt;/</go;
385 s/&gt;/>/go;
386 s/&amp;/&/go;
387 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
388 $plain_texts{$docid} .= $_;
389 $text_started=0;
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200390 last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id} && (!$extract_metadata || $metadata{$target_id})));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100391 } elsif ($text_started) {
392 chomp;
393 $_ = decode("utf-8", $_, Encode::FB_DEFAULT) . ' ';
394 s/&lt;/</go;
395 s/&gt;/>/go;
396 s/&amp;/&/go;
397 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
398 $plain_texts{$docid} .= $_;
399 }
400 }
Marc Kupietz1db65e52021-07-31 23:38:07 +0200401 $log->debug("Got plain text for $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100402 if(defined($ENV{PLAINTEXTFILTER})) {
403 if ($plain_texts{$docid} !~ $ENV{PLAINTEXTFILTER}) {
404 $plain_texts{$docid} = undef;
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100405 $log->info("Skipping $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100406 return(undef);
407 } else {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100408 $log->debug("Using $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100409 }
410 }
411 return(1);
412}
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100413
414=pod
415
416=encoding utf8
417
418=head1 NAME
419
420korapxml2conllu - Conversion of KorAP-XML zips to CoNLL-U
421
422=head1 SYNOPSIS
423
424 korapxml2conllu zca15.tree_tagger.zip > zca15.conllu
425
426=head1 DESCRIPTION
427
428C<korapxml2conllu> is a script to Convert L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml> base or morpho zips to CoNLL(-U) format with all information necessary
429 for reconstruction in comment lines.
430
431=head1 INSTALLATION
432
433 $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git
434
435=head1 OPTIONS
436
437=over 2
438
439=item B<--sigle-pattern|-p>
440
441Convert only texts from the KorAP XML zip files with folder names (i.e. sigles) matching the glob pattern.
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100442
443=item B<--extract-attribute-pattern|-e>
444
445Extract element/attribute regular expressions to comments.
446
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200447=item B<--columns>=I<int> | B<-c> I<int>
448
449Print n columns (default: 10). If n=1, only the token itself is printed.
450
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200451=item B<--s-bounds-from-morpho>
452
Marc Kupietz8eb468e2022-06-29 11:16:42 +0200453Get sentence boundary information from tagger output rather than from s annotation in structure.xml files.
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200454
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200455=item B<--token-separator>=I<string>
456
457Token separator.
458
459=item B<--(no)comments>
460
461Switch comment printing on or off (default: on).
462
463=item B<--(no)offsets>
464
465Switch offsets printing on or off (default: on).
466
467=item B<--word2vec>
468
469Print output in word2vec (tokenized) one sentence per line format.
470
471=item B<--extract-metadata-regex|-m>
472
473Can be used to extract and print strings from the text headers. Currently only works together with --word2vec option.
474For example:
475
476 korapxml2conllu -m '<textSigle>([^<.]+)' -m '<creatDate>([^<]{7})' --word2vec t/data/wdf19.zip
477
478Will print the document sigle, year and month of the creation date and one sentence per line, separated by tabs.
479
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100480=item B<--help|-h>
481
482Print help information.
483
484=item B<--version|-v>
485
486Print version information.
487
488
489=item B<--log|-l>
490
491Loglevel for I<Log::Any>. Defaults to C<warn>.
492
493=back
494
495=head1 EXAMPLES
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100496
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200497=head2 Extract to CoNNL-U with posting and div ids:
498
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100499 korapxml2conllu -e '(posting/id|div/id)' t/data/wdf19.zip
500
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200501=head2 Extract to word2vec input format metadata columns:
502
503 korapxml2conllu --word2vec t/data/wdf19.zip
504
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100505=head1 COPYRIGHT AND LICENSE
506
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200507Copyright (C) 2021-2022, L<IDS Mannheim|https://www.ids-mannheim.de/>
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100508
509Author: Marc Kupietz
510
511Contributors: Nils Diewald
512
513L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
514Corpus Analysis Platform at the
515L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
516member of the
517L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
518
519This program is free software published under the
520L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.