blob: 8fd898b98623e120e129f3df9d77a19d10142141 [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;
68 @extract_metadata_regex = split(/,/,join(',',@extract_metadata_regex));
69}
70
Marc Kupietz6a79cad2021-03-19 16:26:58 +010071# Establish logger
72binmode(STDERR, ':encoding(UTF-8)');
73Log::Any::Adapter->set('Stderr', log_level => $log_level);
74$log->notice('Debugging is activated') if DEBUG;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010075
Marc Kupietzd0bf2772022-06-26 19:27:58 +020076if ($lm_training_data) {
77 $columns = 1;
78 $comments = 0;
79 $offsets = 0;
80 $token_separator = " ";
81}
82
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010083my $docid="";
84my ($current_id, $current_from, $current_to, $token);
85my $current;
86my ($unknown, $known) = (0, 0);
87my @current_lines;
88my %processedFilenames;
89my $zipsiglepattern = (defined($ENV{ZIPSIGLEPATTERN})? $ENV{ZIPSIGLEPATTERN} : "");
Marc Kupietzd8455832021-02-11 17:30:29 +010090my $baseOnly;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +010091my %extras;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010092
93my ($ID_idx, $FORM_idx, $LEMMA_idx, $UPOS_idx, $XPOS_idx, $FEATS_idx, $HEAD_idx, $DEPREC_idx, $DEPS_idx, $MISC_idx) = (0..9);
94
Marc Kupietzc7d1b932020-09-23 13:17:17 +020095my $UNZIP = `sh -c 'command -v unzip'`;
96chomp $UNZIP;
97
98
99if ($UNZIP eq '') {
100 warn('No unzip executable found in PATH.');
101 return 0;
102};
103
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100104foreach my $morpho_zip (@ARGV) {
105 die "cannot open $morpho_zip" if(! -r $morpho_zip);
106 my $data_zip = $morpho_zip;
107 if ($data_zip !~ /\.zip/ && $data_zip =~ /\.conllu?/i) {
108 open(CONLL, "<$data_zip") or die "cannot open $data_zip";
109 while(<CONLL>) {
110 print;
111 }
112 close(CONLL);
113 next;
114 }
Marc Kupietzfdd2d122021-10-12 10:42:34 +0200115 $data_zip =~ s/\.([^\/.]+)\.zip$/.zip/;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100116 my $foundry = $1;
117 die "cannot open data file $data_zip corresponding to $morpho_zip" if(! -r $data_zip);
118
119 my $first=1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100120 my @conll = ("_") x 10;
121 my $filename;
122
Marc Kupietzd8455832021-02-11 17:30:29 +0100123 $baseOnly = $morpho_zip eq $data_zip;
124 my ($morphoOrTokenCommand, $plaintextAndStructureCommand);
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200125 my $zip_content_pattern = "[sd][ta]*";
126 if ($extract_metadata) {
127 $zip_content_pattern = "[sdh][tae]*";
128 }
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100129 if (!$baseOnly) {
130 $morphoOrTokenCommand = "$UNZIP -c $morpho_zip '*/${sigle_pattern}*/*/*/morpho.xml' $zipsiglepattern |";
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200131 if ($extract_attributes_regex || !$s_bounds_from_morpho) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200132 $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/$zip_content_pattern.xml' $zipsiglepattern |";
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100133 } else {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200134 $log->debug("Not reading structure information.");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100135 $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/data.xml' $zipsiglepattern |";
136 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100137 } else {
138 $foundry = "base";
Marc Kupietzf1fdc192021-10-08 13:29:59 +0200139 $morphoOrTokenCommand = "$UNZIP -l $morpho_zip '*/${sigle_pattern}*/*/*/morpho.xml' $zipsiglepattern";
140 if (`$morphoOrTokenCommand` !~ /morpho\.xml/) {
141 $morphoOrTokenCommand =~ s/morpho\.xml/tokens.xml/;
142 } else {
143 $baseOnly = 0;
144 }
145 $morphoOrTokenCommand =~ s/-l/-c/;
146 $morphoOrTokenCommand .= ' |';
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200147 $plaintextAndStructureCommand = "$UNZIP -c $data_zip " . "'*/${sigle_pattern}*/*/$zip_content_pattern.xml' " . "$zipsiglepattern |";
Marc Kupietzd8455832021-02-11 17:30:29 +0100148 }
149
150 open (MORPHO_OR_TOKENPIPE, $morphoOrTokenCommand) or die "cannot unzip $morpho_zip";
151 open (PLAINTEXTPIPE, $plaintextAndStructureCommand) or die "cannot unzip $data_zip";
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200152 print "$COMMENT_START foundry = $foundry$COMMENT_END\n" if ($comments);
Marc Kupietzd8455832021-02-11 17:30:29 +0100153 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz30c41b12020-09-22 14:32:34 +0200154 if (/^ inflating: (.*)/) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100155 $filename=$1;
Marc Kupietzd8455832021-02-11 17:30:29 +0100156 while($processedFilenames{$filename} && !eof(MORPHO_OR_TOKENPIPE)) {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100157 $log->warn("$filename already processed");
Marc Kupietzd8455832021-02-11 17:30:29 +0100158 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100159 last if(/\s+inflating:\s+(.*)/);
160 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100161 $filename=$1 if(!eof(MORPHO_OR_TOKENPIPE) && /\s+inflating:\s+(.*)/);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100162 }
Marc Kupietz30c41b12020-09-22 14:32:34 +0200163 } elsif(m@^\s*<layer\s+.*docid="([^"]+)"@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100164 last if($test && $text_no++ > 3);
165 if(!$first) {
166 closeDoc(0);
167 }
168 $processedFilenames{$filename}=1;
169 $docid=$1;
170 @current_lines=();
171 $known=$unknown=0;
172 $current="";
173 if ($first) {
174 $first = 0;
175 }
176 if(!fetch_plaintext($docid)) { # skip this text
Marc Kupietzd8455832021-02-11 17:30:29 +0100177 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100178 last if(m@</layer>@);
179 }
180 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200181 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 +0100182 $log->debug("Analyzing $docid");
Marc Kupietz30c41b12020-09-22 14:32:34 +0200183 } elsif (m@^\s*<f\s+.*name="([^"]+)">([^<]+)</f>@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100184 if ($1 eq "lemma") {
185 $conll[$LEMMA_idx] = $2;
186 $conll[$LEMMA_idx] =~ s/[\t\n\r]//g; # make sure that lemmas never contain tabs or newlines
187 if($conll[$LEMMA_idx] eq 'UNKNOWN') {
188 $conll[$LEMMA_idx] = "--";
189 $unknown++;
190 } else {
191 $known++;
192 }
193 } elsif ($1 eq 'pos' || $1 eq "ctag") {
194 $unknown++;
195 $conll[$XPOS_idx] = $conll[$UPOS_idx] = $2;
196 } elsif ($1 eq 'msd') {
197 $conll[$FEATS_idx] = $2;
198 } elsif ($1 eq 'certainty') {
199 $conll[$MISC_idx] = $2;
200 }
201 } elsif (/<span /) {
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100202 my $last_from = $current_from // -1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100203 ($current_id) = /id="[^0-9]*([^\"]*)"/;
204 ($current_from) = /from="([^\"]*)"/;
205 ($current_to) = /to="([^\"]*)"/;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100206 if($extract_attributes_regex) {
207 for (my $i = $last_from + 1; $i <= $current_from; $i++) {
208 if ($extras{$docid}{$i}) {
209 $current .= $extras{$docid}{$i};
210 undef $extras{$docid}{$i};
211 }
212 }
213 }
Marc Kupietz1db65e52021-07-31 23:38:07 +0200214# $log->debug("found span: $current_id $current_from $current_to");
Marc Kupietz7e71a822020-06-22 17:14:30 +0200215 $token = substr($plain_texts{$docid}, $current_from, $current_to - $current_from);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100216 if (!defined $token) {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100217 $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 +0100218 $token = "_";
219 }
220 $token=~s/[\t\n\r]//g; # make sure that tokens never contain tabs or newlines
221 @conll = ("_") x 10;
222 $conll[$FORM_idx] = encode("utf-8", $token);
Marc Kupietzd8455832021-02-11 17:30:29 +0100223 if($baseOnly) {
224 my @vals = ($current_from, $current_to);
Marc Kupietz1db65e52021-07-31 23:38:07 +0200225# $log->debug("joining : ", join(" ", @vals));
Marc Kupietzd8455832021-02-11 17:30:29 +0100226 push @current_lines, \@vals;
227 $known++;
228 $conll[$ID_idx] = $#current_lines+1;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200229 if ($columns == 1) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200230 $current .= "$conll[1]$token_separator" ;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200231 } else {
232 $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns
233 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100234 fetch_plaintext($docid);
235 if ($sentence_ends{$docid}{$current_to}) {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200236 $log->debug("Using sentence end for $docid \@$current_to");
Marc Kupietzd8455832021-02-11 17:30:29 +0100237 $current .= "\n";
238 printTokenRanges();
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200239 if ($extract_metadata) {
240 for (my $i = 0; $i < @extract_metadata_regex; $i++) {
241 print "$metadata{$docid}[$i]\t";
242 }
243 }
244 $current =~ s/ $//;
Marc Kupietzd8455832021-02-11 17:30:29 +0100245 print STDOUT $current;
246 $current = "";
247 $known = 0;
248 $unknown = 0;
249 @current_lines = ();
250 }
251 }
Marc Kupietz30c41b12020-09-22 14:32:34 +0200252 } elsif (m@^\s*</fs>@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100253 my @vals = ($current_from, $current_to);
Marc Kupietz1db65e52021-07-31 23:38:07 +0200254# $log->debug("joining : ", join(" ", @vals));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100255 push @current_lines, \@vals;
256 # convert gathered information to CONLL
257 $conll[$ID_idx] = $#current_lines+1;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200258 if ($columns == 1) {
259 $current .= "$conll[1]\n";
260 } else {
261 $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns
262 }
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200263 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) {
264 $log->debug("Using sentence end for $docid \@$current_to");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100265 $current .= "\n";
266 if($known + $unknown > 0) { # only print sentence if it contains some words
267 printTokenRanges();
268 print STDOUT $current;
269 }
270 $current=""; $known=0; $unknown=0;
271 @current_lines = ();
272 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100273 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100274 last if (m@</span>@); # only consider first interpretation
275 }
276 }
277 }
278 $current .= "\n";
279 closeDoc(1);
Marc Kupietzd8455832021-02-11 17:30:29 +0100280 close(MORPHO_OR_TOKENPIPE);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100281 close(PLAINTEXTPIPE);
282}
283exit;
284
285sub printTokenRanges {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200286 return if(!$offsets);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100287 print "$COMMENT_START start_offsets = ", $current_lines[0]->[0];
288 foreach my $t (@current_lines) {
289 print STDOUT " $t->[0]";
290 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200291 print "$COMMENT_END\n$COMMENT_START end_offsets = ", $current_lines[$#current_lines]->[1] if($comments);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100292 foreach my $t (@current_lines) {
293 print STDOUT " $t->[1]";
294 }
Marc Kupietza2680b92021-10-11 17:24:28 +0200295 print "$COMMENT_END\n";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100296}
297
298sub closeDoc {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100299 $log->debug("closing doc");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100300 if($known + $unknown > 0) { # only parse a sentence if it has some words
301 chomp $current;
302 chomp $current;
303 chomp $current;
304 $current .= "\n\n";
305 printTokenRanges();
306 print STDOUT $current;
307 }
308}
309
310# read data.xml to figure out the tokens
311# (ideally tokens should also be in in morpho.xml, but they are not)
312sub fetch_plaintext {
313 my ($target_id) = @_;
314 my $docid;
315 my $text_started=0;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100316 my $text_count = 0;
Marc Kupietzd8455832021-02-11 17:30:29 +0100317 my ($current_id, $current_from, $current_to);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100318
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200319 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 +0200320 $log->debug("Already got $target_id");
Marc Kupietzd8455832021-02-11 17:30:29 +0100321 return 1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100322 }
323 while(<PLAINTEXTPIPE>) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200324 if ($extract_metadata) {
325 if (/<textSigle>([^<]+)/) {
326 $docid = $1;
327 $docid =~ s@/@_@;
328 $log->debug("textsigle=$docid");
329 }
330 for (my $i=0; $i < @extract_metadata_regex; $i++) {
331 if ($_ =~ /$extract_metadata_regex[$i]/) {
332 $metadata{$docid}[$i]=$1;
333 }
334 }
335 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100336 if(/<raw_text[^>]+docid="([^"]*)/) {
337 $docid=$1;
Marc Kupietz1db65e52021-07-31 23:38:07 +0200338 $log->debug("Getting plain text for $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100339 $text_started=0;
Marc Kupietzd8455832021-02-11 17:30:29 +0100340 } elsif(/<layer[^>]+docid="([^"]*)/) {
341 $docid=$1;
Marc Kupietzd8455832021-02-11 17:30:29 +0100342 } elsif(m@<span @) {
343 ($current_id) = /id="[^0-9]*([^\"]*)"/;
344 ($current_from) = /from="([^\"]*)"/;
345 ($current_to) = /to="([^\"]*)"/;
346 } elsif(m@<f\s[^>]*>s</f>@) {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200347 if ($s_bounds_from_morpho) {
348 $log->debug("Ignoring sentence end for $docid \@$current_to because of --s-bounds-from-morpho");
349 } else {
350 $log->debug("Found sentence end for $docid \@$current_to");
351 $sentence_ends{$docid}{$current_to} = 1;
352 }
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100353 } elsif($extract_attributes_regex && m@<f\sname="name"[^>]*>([^<]+)</f>@) {
354 my $current_element = $1;
Marc Kupietz1db65e52021-07-31 23:38:07 +0200355 $log->debug("Looking for matching attributes in $docid");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100356 while(<PLAINTEXTPIPE>) {
357 last if(m@</fs>@);
358 if(m@<f\sname="([^"]+)"[^>]*>([^<]+)</f>@) {
359 my $current_node = "$current_element/$1";
360 my $value = $2;
361 if ($current_node =~ /$extract_attributes_regex/) {
Marc Kupietz1db65e52021-07-31 23:38:07 +0200362 $log->debug("Found matching attribute: $docid - $current_node = $value");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100363 $extras{$docid}{$current_from} .= "# $current_node = $value\n";
364 }
365 }
366 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100367 } elsif (m@<text>(.*)</text>@) {
368 $_= decode("utf-8", $1, Encode::FB_DEFAULT);
369 s/&lt;/</go;
370 s/&gt;/>/go;
371 s/&amp;/&/go;
372 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
373 $plain_texts{$docid} = $_;
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200374 last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id} && (!$extract_metadata || $metadata{$target_id})));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100375 } elsif (m@<text>(.*)@) {
376 $_= decode("utf-8", $1, Encode::FB_DEFAULT);
377 s/&lt;/</go;
378 s/&gt;/>/go;
379 s/&amp;/&/go;
380 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
381 $plain_texts{$docid} = "$_ ";
382 $text_started=1;
383 } elsif ($text_started && m@(.*)</text>@) {
384 $_= decode("utf-8", $1, Encode::FB_DEFAULT);
385 s/&lt;/</go;
386 s/&gt;/>/go;
387 s/&amp;/&/go;
388 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
389 $plain_texts{$docid} .= $_;
390 $text_started=0;
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200391 last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id} && (!$extract_metadata || $metadata{$target_id})));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100392 } elsif ($text_started) {
393 chomp;
394 $_ = decode("utf-8", $_, Encode::FB_DEFAULT) . ' ';
395 s/&lt;/</go;
396 s/&gt;/>/go;
397 s/&amp;/&/go;
398 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
399 $plain_texts{$docid} .= $_;
400 }
401 }
Marc Kupietz1db65e52021-07-31 23:38:07 +0200402 $log->debug("Got plain text for $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100403 if(defined($ENV{PLAINTEXTFILTER})) {
404 if ($plain_texts{$docid} !~ $ENV{PLAINTEXTFILTER}) {
405 $plain_texts{$docid} = undef;
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100406 $log->info("Skipping $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100407 return(undef);
408 } else {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100409 $log->debug("Using $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100410 }
411 }
412 return(1);
413}
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100414
415=pod
416
417=encoding utf8
418
419=head1 NAME
420
421korapxml2conllu - Conversion of KorAP-XML zips to CoNLL-U
422
423=head1 SYNOPSIS
424
425 korapxml2conllu zca15.tree_tagger.zip > zca15.conllu
426
427=head1 DESCRIPTION
428
429C<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
430 for reconstruction in comment lines.
431
432=head1 INSTALLATION
433
434 $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git
435
436=head1 OPTIONS
437
438=over 2
439
440=item B<--sigle-pattern|-p>
441
442Convert 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 +0100443
444=item B<--extract-attribute-pattern|-e>
445
446Extract element/attribute regular expressions to comments.
447
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200448=item B<--columns>=I<int> | B<-c> I<int>
449
450Print n columns (default: 10). If n=1, only the token itself is printed.
451
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200452=item B<--s-bounds-from-morpho>
453
Marc Kupietz8eb468e2022-06-29 11:16:42 +0200454Get sentence boundary information from tagger output rather than from s annotation in structure.xml files.
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200455
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200456=item B<--token-separator>=I<string>
457
458Token separator.
459
460=item B<--(no)comments>
461
462Switch comment printing on or off (default: on).
463
464=item B<--(no)offsets>
465
466Switch offsets printing on or off (default: on).
467
468=item B<--word2vec>
469
470Print output in word2vec (tokenized) one sentence per line format.
471
472=item B<--extract-metadata-regex|-m>
473
474Can be used to extract and print strings from the text headers. Currently only works together with --word2vec option.
475For example:
476
477 korapxml2conllu -m '<textSigle>([^<.]+)' -m '<creatDate>([^<]{7})' --word2vec t/data/wdf19.zip
478
479Will print the document sigle, year and month of the creation date and one sentence per line, separated by tabs.
480
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100481=item B<--help|-h>
482
483Print help information.
484
485=item B<--version|-v>
486
487Print version information.
488
489
490=item B<--log|-l>
491
492Loglevel for I<Log::Any>. Defaults to C<warn>.
493
494=back
495
496=head1 EXAMPLES
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100497
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200498=head2 Extract to CoNNL-U with posting and div ids:
499
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100500 korapxml2conllu -e '(posting/id|div/id)' t/data/wdf19.zip
501
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200502=head2 Extract to word2vec input format metadata columns:
503
504 korapxml2conllu --word2vec t/data/wdf19.zip
505
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100506=head1 COPYRIGHT AND LICENSE
507
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200508Copyright (C) 2021-2022, L<IDS Mannheim|https://www.ids-mannheim.de/>
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100509
510Author: Marc Kupietz
511
512Contributors: Nils Diewald
513
514L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
515Corpus Analysis Platform at the
516L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
517member of the
518L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
519
520This program is free software published under the
521L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.