blob: 40a06d35c31b515d189c58e12894217431719694 [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 +01009
10my $MAX_SENTENCE_LENGTH=10000;
11my $COMMENT_START="#";
Marc Kupietza2680b92021-10-11 17:24:28 +020012my $COMMENT_END="";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010013
14my $test=0;
15my $text_no=0;
16my %opts;
17my %plain_texts;
Marc Kupietzd8455832021-02-11 17:30:29 +010018my %sentence_ends;
Marc Kupietzd0bf2772022-06-26 19:27:58 +020019my %metadata;
20
21my $offsets = 1;
22my $comments = 1;
23my $extract_metadata = 0;
24my @extract_metadata_regex;
25
26my $lm_training_data = 0;
Marc Kupietzd8455832021-02-11 17:30:29 +010027
Akrone24c9332023-03-22 10:59:33 +010028our $VERSION = '0.6.1';
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010029
Marc Kupietz0ab8a2c2021-03-19 16:21:00 +010030our $VERSION_MSG = "\nkorapxml2conllu - v$VERSION\n";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010031
Marc Kupietz6a79cad2021-03-19 16:26:58 +010032use constant {
33 # Set to 1 for minimal more debug output (no need to be parametrized)
34 DEBUG => $ENV{KORAPXMLCONLLU_DEBUG} // 0
35};
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010036
Marc Kupietz911fe972024-01-13 19:29:31 +010037use open ':std', ':encoding(UTF-8)';
38
Marc Kupietz6a79cad2021-03-19 16:26:58 +010039GetOptions(
40 'sigle-pattern|p=s' => \(my $sigle_pattern = ''),
Marc Kupietzeb7d06a2021-03-19 16:29:16 +010041 'extract-attributes-regex|e=s' => \(my $extract_attributes_regex = ''),
Marc Kupietz15c84fd2021-10-12 12:20:27 +020042 's-bounds-from-morpho' => \(my $s_bounds_from_morpho = 0),
Marc Kupietz6a79cad2021-03-19 16:26:58 +010043 'log|l=s' => \(my $log_level = 'warn'),
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +020044 'columns|c=n' => \(my $columns = 10),
Marc Kupietzd0bf2772022-06-26 19:27:58 +020045 'word2vec|lm-training-data!' => \$lm_training_data,
46 'token-separator|s=s' => \(my $token_separator = "\n"),
47 'offsets!' => \$offsets,
48 'comments!' => \$comments,
49 'extract-metadata-regex|m=s@' => \@extract_metadata_regex,
Marc Kupietz6a79cad2021-03-19 16:26:58 +010050 'help|h' => sub {
51 pod2usage(
52 -verbose => 99,
53 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS|EXAMPLES',
54 -msg => $VERSION_MSG,
55 -output => '-'
56 )
57 },
58 'version|v' => sub {
59 pod2usage(
60 -verbose => 0,
61 -msg => $VERSION_MSG,
62 -output => '-'
63 );
64 }
65);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +010066
Marc Kupietzd0bf2772022-06-26 19:27:58 +020067if (@extract_metadata_regex) {
68 $extract_metadata = 1;
Marc Kupietzd0bf2772022-06-26 19:27:58 +020069}
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) {
Marc Kupietz534df182022-12-16 15:00:30 +0100130 $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 Kupietz534df182022-12-16 15:00:30 +0100132 $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 Kupietz534df182022-12-16 15:00:30 +0100135 $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*${sigle_pattern}*/data.xml' $zipsiglepattern |";
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100136 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100137 } else {
138 $foundry = "base";
Marc Kupietz534df182022-12-16 15:00:30 +0100139 $morphoOrTokenCommand = "$UNZIP -l $morpho_zip '*${sigle_pattern}*/morpho.xml' $zipsiglepattern";
Marc Kupietzf1fdc192021-10-08 13:29:59 +0200140 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 Kupietz534df182022-12-16 15:00:30 +0100147 $plaintextAndStructureCommand = "$UNZIP -c $data_zip " . "'*${sigle_pattern}*/$zip_content_pattern.xml' " . "$zipsiglepattern |";
Marc Kupietzd8455832021-02-11 17:30:29 +0100148 }
149
Marc Kupietz534df182022-12-16 15:00:30 +0100150 $log->debug("command to extract annotation and/or tokenization: $morphoOrTokenCommand");
151 $log->debug("command to extract plain text and structure: $plaintextAndStructureCommand");
152
Marc Kupietzd8455832021-02-11 17:30:29 +0100153 open (MORPHO_OR_TOKENPIPE, $morphoOrTokenCommand) or die "cannot unzip $morpho_zip";
154 open (PLAINTEXTPIPE, $plaintextAndStructureCommand) or die "cannot unzip $data_zip";
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200155 print "$COMMENT_START foundry = $foundry$COMMENT_END\n" if ($comments);
Marc Kupietzd8455832021-02-11 17:30:29 +0100156 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz30c41b12020-09-22 14:32:34 +0200157 if (/^ inflating: (.*)/) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100158 $filename=$1;
Marc Kupietzd8455832021-02-11 17:30:29 +0100159 while($processedFilenames{$filename} && !eof(MORPHO_OR_TOKENPIPE)) {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100160 $log->warn("$filename already processed");
Marc Kupietzd8455832021-02-11 17:30:29 +0100161 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100162 last if(/\s+inflating:\s+(.*)/);
163 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100164 $filename=$1 if(!eof(MORPHO_OR_TOKENPIPE) && /\s+inflating:\s+(.*)/);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100165 }
Marc Kupietz7e4cd6c2022-12-15 18:34:37 +0100166 } elsif(m@(?:^|\s)docid="([^"]+)"@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100167 last if($test && $text_no++ > 3);
168 if(!$first) {
169 closeDoc(0);
170 }
171 $processedFilenames{$filename}=1;
172 $docid=$1;
173 @current_lines=();
174 $known=$unknown=0;
175 $current="";
176 if ($first) {
177 $first = 0;
178 }
179 if(!fetch_plaintext($docid)) { # skip this text
Marc Kupietzd8455832021-02-11 17:30:29 +0100180 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100181 last if(m@</layer>@);
182 }
183 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200184 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 +0100185 $log->debug("Analyzing $docid");
Marc Kupietz30c41b12020-09-22 14:32:34 +0200186 } elsif (m@^\s*<f\s+.*name="([^"]+)">([^<]+)</f>@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100187 if ($1 eq "lemma") {
188 $conll[$LEMMA_idx] = $2;
189 $conll[$LEMMA_idx] =~ s/[\t\n\r]//g; # make sure that lemmas never contain tabs or newlines
190 if($conll[$LEMMA_idx] eq 'UNKNOWN') {
191 $conll[$LEMMA_idx] = "--";
192 $unknown++;
193 } else {
194 $known++;
195 }
196 } elsif ($1 eq 'pos' || $1 eq "ctag") {
197 $unknown++;
198 $conll[$XPOS_idx] = $conll[$UPOS_idx] = $2;
199 } elsif ($1 eq 'msd') {
200 $conll[$FEATS_idx] = $2;
201 } elsif ($1 eq 'certainty') {
202 $conll[$MISC_idx] = $2;
203 }
204 } elsif (/<span /) {
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100205 my $last_from = $current_from // -1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100206 ($current_id) = /id="[^0-9]*([^\"]*)"/;
207 ($current_from) = /from="([^\"]*)"/;
208 ($current_to) = /to="([^\"]*)"/;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100209 if($extract_attributes_regex) {
210 for (my $i = $last_from + 1; $i <= $current_from; $i++) {
211 if ($extras{$docid}{$i}) {
212 $current .= $extras{$docid}{$i};
213 undef $extras{$docid}{$i};
214 }
215 }
216 }
Marc Kupietz1db65e52021-07-31 23:38:07 +0200217# $log->debug("found span: $current_id $current_from $current_to");
Marc Kupietz7e71a822020-06-22 17:14:30 +0200218 $token = substr($plain_texts{$docid}, $current_from, $current_to - $current_from);
Akronf2b0bba2022-12-16 18:00:08 +0100219 if (!defined $token || length($token) == 0) {
Marc Kupietzd599ed32022-07-01 09:38:34 +0200220 $log->warn("$docid: could not retrieve token 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 +0100221 $token = "_";
222 }
223 $token=~s/[\t\n\r]//g; # make sure that tokens never contain tabs or newlines
224 @conll = ("_") x 10;
Marc Kupietz911fe972024-01-13 19:29:31 +0100225 $conll[$FORM_idx] = $token;
Marc Kupietzd8455832021-02-11 17:30:29 +0100226 if($baseOnly) {
227 my @vals = ($current_from, $current_to);
Marc Kupietz1db65e52021-07-31 23:38:07 +0200228# $log->debug("joining : ", join(" ", @vals));
Marc Kupietzd8455832021-02-11 17:30:29 +0100229 push @current_lines, \@vals;
230 $known++;
231 $conll[$ID_idx] = $#current_lines+1;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200232 if ($columns == 1) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200233 $current .= "$conll[1]$token_separator" ;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200234 } else {
235 $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns
236 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100237 if ($sentence_ends{$docid}{$current_to}) {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200238 $log->debug("Using sentence end for $docid \@$current_to");
Marc Kupietzd8455832021-02-11 17:30:29 +0100239 $current .= "\n";
240 printTokenRanges();
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200241 if ($extract_metadata) {
242 for (my $i = 0; $i < @extract_metadata_regex; $i++) {
Marc Kupietzd599ed32022-07-01 09:38:34 +0200243 if(!defined($metadata{$docid}[$i])) {
244 $log->warn("$docid: metadata matching /$extract_metadata_regex[$i]/ was not found, using empty string instead");
245 $metadata{$docid}[$i]="";
246 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200247 print "$metadata{$docid}[$i]\t";
248 }
249 }
250 $current =~ s/ $//;
Marc Kupietzd8455832021-02-11 17:30:29 +0100251 print STDOUT $current;
252 $current = "";
253 $known = 0;
254 $unknown = 0;
255 @current_lines = ();
256 }
257 }
Marc Kupietz30c41b12020-09-22 14:32:34 +0200258 } elsif (m@^\s*</fs>@) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100259 my @vals = ($current_from, $current_to);
Marc Kupietz1db65e52021-07-31 23:38:07 +0200260# $log->debug("joining : ", join(" ", @vals));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100261 push @current_lines, \@vals;
262 # convert gathered information to CONLL
263 $conll[$ID_idx] = $#current_lines+1;
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200264 if ($columns == 1) {
265 $current .= "$conll[1]\n";
266 } else {
267 $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns
268 }
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200269 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) {
270 $log->debug("Using sentence end for $docid \@$current_to");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100271 $current .= "\n";
272 if($known + $unknown > 0) { # only print sentence if it contains some words
273 printTokenRanges();
274 print STDOUT $current;
275 }
276 $current=""; $known=0; $unknown=0;
277 @current_lines = ();
278 }
Marc Kupietzd8455832021-02-11 17:30:29 +0100279 while (<MORPHO_OR_TOKENPIPE>) {
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100280 last if (m@</span>@); # only consider first interpretation
281 }
282 }
283 }
284 $current .= "\n";
285 closeDoc(1);
Marc Kupietzd8455832021-02-11 17:30:29 +0100286 close(MORPHO_OR_TOKENPIPE);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100287 close(PLAINTEXTPIPE);
288}
289exit;
290
291sub printTokenRanges {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200292 return if(!$offsets);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100293 print "$COMMENT_START start_offsets = ", $current_lines[0]->[0];
294 foreach my $t (@current_lines) {
Marc Kupietz911fe972024-01-13 19:29:31 +0100295 print " $t->[0]";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100296 }
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200297 print "$COMMENT_END\n$COMMENT_START end_offsets = ", $current_lines[$#current_lines]->[1] if($comments);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100298 foreach my $t (@current_lines) {
Marc Kupietz911fe972024-01-13 19:29:31 +0100299 print " $t->[1]";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100300 }
Marc Kupietza2680b92021-10-11 17:24:28 +0200301 print "$COMMENT_END\n";
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100302}
303
304sub closeDoc {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100305 $log->debug("closing doc");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100306 if($known + $unknown > 0) { # only parse a sentence if it has some words
307 chomp $current;
308 chomp $current;
309 chomp $current;
310 $current .= "\n\n";
311 printTokenRanges();
Marc Kupietz911fe972024-01-13 19:29:31 +0100312 print $current;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100313 }
314}
315
316# read data.xml to figure out the tokens
317# (ideally tokens should also be in in morpho.xml, but they are not)
318sub fetch_plaintext {
319 my ($target_id) = @_;
320 my $docid;
321 my $text_started=0;
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100322 my $text_count = 0;
Marc Kupietzd8455832021-02-11 17:30:29 +0100323 my ($current_id, $current_from, $current_to);
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100324
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200325 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 +0200326 $log->debug("Already got $target_id");
Marc Kupietzd8455832021-02-11 17:30:29 +0100327 return 1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100328 }
329 while(<PLAINTEXTPIPE>) {
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200330 if ($extract_metadata) {
331 if (/<textSigle>([^<]+)/) {
332 $docid = $1;
333 $docid =~ s@/@_@;
334 $log->debug("textsigle=$docid");
335 }
336 for (my $i=0; $i < @extract_metadata_regex; $i++) {
337 if ($_ =~ /$extract_metadata_regex[$i]/) {
338 $metadata{$docid}[$i]=$1;
339 }
340 }
341 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100342 if(/<raw_text[^>]+docid="([^"]*)/) {
343 $docid=$1;
Marc Kupietz1db65e52021-07-31 23:38:07 +0200344 $log->debug("Getting plain text for $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100345 $text_started=0;
Marc Kupietz7e4cd6c2022-12-15 18:34:37 +0100346 } elsif(/<raw_text\b/) {
347 $text_started=0;
348 } elsif(/(?:^|\s)docid="([^"]*)/) {
Marc Kupietzd8455832021-02-11 17:30:29 +0100349 $docid=$1;
Marc Kupietz7e4cd6c2022-12-15 18:34:37 +0100350 $log->debug("Getting plain text for $docid");
Marc Kupietzd8455832021-02-11 17:30:29 +0100351 } elsif(m@<span @) {
352 ($current_id) = /id="[^0-9]*([^\"]*)"/;
353 ($current_from) = /from="([^\"]*)"/;
354 ($current_to) = /to="([^\"]*)"/;
355 } elsif(m@<f\s[^>]*>s</f>@) {
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200356 if ($s_bounds_from_morpho) {
357 $log->debug("Ignoring sentence end for $docid \@$current_to because of --s-bounds-from-morpho");
358 } else {
359 $log->debug("Found sentence end for $docid \@$current_to");
360 $sentence_ends{$docid}{$current_to} = 1;
361 }
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100362 } elsif($extract_attributes_regex && m@<f\sname="name"[^>]*>([^<]+)</f>@) {
363 my $current_element = $1;
Marc Kupietz1db65e52021-07-31 23:38:07 +0200364 $log->debug("Looking for matching attributes in $docid");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100365 while(<PLAINTEXTPIPE>) {
366 last if(m@</fs>@);
367 if(m@<f\sname="([^"]+)"[^>]*>([^<]+)</f>@) {
368 my $current_node = "$current_element/$1";
369 my $value = $2;
370 if ($current_node =~ /$extract_attributes_regex/) {
Marc Kupietz1db65e52021-07-31 23:38:07 +0200371 $log->debug("Found matching attribute: $docid - $current_node = $value");
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100372 $extras{$docid}{$current_from} .= "# $current_node = $value\n";
373 }
374 }
375 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100376 } elsif (m@<text>(.*)</text>@) {
Marc Kupietz911fe972024-01-13 19:29:31 +0100377 $_= $1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100378 s/&lt;/</go;
379 s/&gt;/>/go;
380 s/&amp;/&/go;
381 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
382 $plain_texts{$docid} = $_;
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200383 last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id} && (!$extract_metadata || $metadata{$target_id})));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100384 } elsif (m@<text>(.*)@) {
Marc Kupietz911fe972024-01-13 19:29:31 +0100385 $_= $1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100386 s/&lt;/</go;
387 s/&gt;/>/go;
388 s/&amp;/&/go;
389 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
390 $plain_texts{$docid} = "$_ ";
391 $text_started=1;
392 } elsif ($text_started && m@(.*)</text>@) {
Marc Kupietz911fe972024-01-13 19:29:31 +0100393 $_= $1;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100394 s/&lt;/</go;
395 s/&gt;/>/go;
396 s/&amp;/&/go;
397 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
398 $plain_texts{$docid} .= $_;
399 $text_started=0;
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200400 last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id} && (!$extract_metadata || $metadata{$target_id})));
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100401 } elsif ($text_started) {
402 chomp;
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100403 s/&lt;/</go;
404 s/&gt;/>/go;
405 s/&amp;/&/go;
406 tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/;
407 $plain_texts{$docid} .= $_;
408 }
409 }
Marc Kupietz1db65e52021-07-31 23:38:07 +0200410 $log->debug("Got plain text for $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100411 if(defined($ENV{PLAINTEXTFILTER})) {
412 if ($plain_texts{$docid} !~ $ENV{PLAINTEXTFILTER}) {
413 $plain_texts{$docid} = undef;
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100414 $log->info("Skipping $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100415 return(undef);
416 } else {
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100417 $log->debug("Using $docid");
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100418 }
Marc Kupietz911fe972024-01-13 19:29:31 +0100419 }
Marc Kupietz5e7f20a2020-02-17 18:17:11 +0100420 return(1);
421}
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100422
423=pod
424
425=encoding utf8
426
427=head1 NAME
428
429korapxml2conllu - Conversion of KorAP-XML zips to CoNLL-U
430
431=head1 SYNOPSIS
432
433 korapxml2conllu zca15.tree_tagger.zip > zca15.conllu
434
435=head1 DESCRIPTION
436
437C<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
438 for reconstruction in comment lines.
439
440=head1 INSTALLATION
441
442 $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git
443
444=head1 OPTIONS
445
446=over 2
447
448=item B<--sigle-pattern|-p>
449
450Convert 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 +0100451
452=item B<--extract-attribute-pattern|-e>
453
454Extract element/attribute regular expressions to comments.
455
Marc Kupietzd7d5d6a2021-10-11 17:52:58 +0200456=item B<--columns>=I<int> | B<-c> I<int>
457
458Print n columns (default: 10). If n=1, only the token itself is printed.
459
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200460=item B<--s-bounds-from-morpho>
461
Marc Kupietz8eb468e2022-06-29 11:16:42 +0200462Get sentence boundary information from tagger output rather than from s annotation in structure.xml files.
Marc Kupietz15c84fd2021-10-12 12:20:27 +0200463
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200464=item B<--token-separator>=I<string>
465
466Token separator.
467
468=item B<--(no)comments>
469
470Switch comment printing on or off (default: on).
471
472=item B<--(no)offsets>
473
474Switch offsets printing on or off (default: on).
475
476=item B<--word2vec>
477
478Print output in word2vec (tokenized) one sentence per line format.
479
480=item B<--extract-metadata-regex|-m>
481
482Can be used to extract and print strings from the text headers. Currently only works together with --word2vec option.
483For example:
484
485 korapxml2conllu -m '<textSigle>([^<.]+)' -m '<creatDate>([^<]{7})' --word2vec t/data/wdf19.zip
486
487Will print the document sigle, year and month of the creation date and one sentence per line, separated by tabs.
488
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100489=item B<--help|-h>
490
491Print help information.
492
493=item B<--version|-v>
494
495Print version information.
496
497
498=item B<--log|-l>
499
500Loglevel for I<Log::Any>. Defaults to C<warn>.
501
502=back
503
504=head1 EXAMPLES
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100505
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200506=head2 Extract to CoNNL-U with posting and div ids:
507
Marc Kupietzeb7d06a2021-03-19 16:29:16 +0100508 korapxml2conllu -e '(posting/id|div/id)' t/data/wdf19.zip
509
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200510=head2 Extract to word2vec input format metadata columns:
511
512 korapxml2conllu --word2vec t/data/wdf19.zip
513
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100514=head1 COPYRIGHT AND LICENSE
515
Marc Kupietzd0bf2772022-06-26 19:27:58 +0200516Copyright (C) 2021-2022, L<IDS Mannheim|https://www.ids-mannheim.de/>
Marc Kupietz6a79cad2021-03-19 16:26:58 +0100517
518Author: Marc Kupietz
519
520Contributors: Nils Diewald
521
522L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
523Corpus Analysis Platform at the
524L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
525member of the
526L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
527
528This program is free software published under the
529L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.