Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use POSIX; |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 5 | use Getopt::Long qw(GetOptions :config no_auto_abbrev); |
| 6 | use Log::Any '$log'; |
| 7 | use Log::Any::Adapter; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 8 | use Encode; |
| 9 | use IO::Compress::Zip qw(zip $ZipError :constants); |
| 10 | use File::Basename; |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 11 | use Pod::Usage; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 12 | |
| 13 | my $_COMPRESSION_METHOD = ZIP_CM_DEFLATE; |
| 14 | my %opts; |
| 15 | my %processedFilenames; |
| 16 | |
Akron | 249fc83 | 2024-06-04 16:36:44 +0200 | [diff] [blame] | 17 | our $VERSION = '0.6.3'; |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 18 | our $VERSION_MSG = "\nconllu2korapxml - v$VERSION\n"; |
Marc Kupietz | 4cc243a | 2021-10-11 17:15:16 +0200 | [diff] [blame] | 19 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 20 | use constant { |
| 21 | # Set to 1 for minimal more debug output (no need to be parametrized) |
| 22 | DEBUG => $ENV{KORAPXMLCONLLU_DEBUG} // 0 |
| 23 | }; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 24 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 25 | GetOptions( |
| 26 | 'force-foundry|f=s' => \(my $foundry_name = ''), |
| 27 | 'log|l=s' => \(my $log_level = 'warn'), |
Marc Kupietz | 187abd7 | 2024-06-25 14:30:01 +0200 | [diff] [blame^] | 28 | 'output|o=s' => \(my $outh = '-'), |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 29 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 30 | 'help|h' => sub { |
| 31 | pod2usage( |
| 32 | -verbose => 99, |
| 33 | -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS|EXAMPLES', |
| 34 | -msg => $VERSION_MSG, |
| 35 | -output => '-' |
| 36 | ) |
| 37 | }, |
| 38 | 'version|v' => sub { |
| 39 | pod2usage( |
| 40 | -verbose => 0, |
| 41 | -msg => $VERSION_MSG, |
| 42 | -output => '-' |
| 43 | ); |
| 44 | } |
| 45 | ); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 46 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 47 | # Establish logger |
| 48 | binmode(STDERR, ':encoding(UTF-8)'); |
| 49 | Log::Any::Adapter->set('Stderr', log_level => $log_level); |
| 50 | $log->notice('Debugging is activated') if DEBUG; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 51 | |
| 52 | my $docid=""; |
| 53 | my $zip = undef; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 54 | my $parser_file; |
| 55 | my $parse; |
| 56 | my $morpho_file; |
| 57 | my $morpho; |
| 58 | my @spansFrom; |
| 59 | my @spansTo; |
| 60 | my $current; |
| 61 | my ($unknown, $known) = (0, 0); |
| 62 | |
| 63 | my ($write_morpho, $write_syntax, $base) = (1, 0, 0); |
| 64 | my $filename; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 65 | my $first=1; |
| 66 | my @conllu_files = @ARGV; |
| 67 | push @conllu_files, "-" if (@conllu_files == 0); |
| 68 | my $fh; |
Marc Kupietz | dd546a8 | 2024-03-22 16:30:09 +0100 | [diff] [blame] | 69 | |
| 70 | my $dependency_foundry_name = $foundry_name; |
| 71 | if ($foundry_name =~ /(.*) dependency:(.*)/) { |
| 72 | $foundry_name = $1; |
| 73 | $dependency_foundry_name = $2; |
| 74 | } |
| 75 | |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 76 | foreach my $conllu_file (@conllu_files) { |
| 77 | if ($conllu_file eq '-') { |
| 78 | $fh = \*STDIN; |
| 79 | } else { |
| 80 | open($fh, "<", $conllu_file) or die "Cannot open $conllu_file"; |
| 81 | } |
| 82 | my $i=0; my $s=0; my $first_in_sentence=0; |
| 83 | my $lastDocSigle=""; |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 84 | MAIN: while (<$fh>) { |
Marc Kupietz | bcb55b8 | 2022-09-15 11:42:26 +0200 | [diff] [blame] | 85 | if(/^\s*(?:#|0\.\d)/) { |
| 86 | if(/^(?:#|0\.1)\s+filename\s*[:=]\s*(.*)/) { |
| 87 | $filename=$1; |
| 88 | if(!$first) { |
| 89 | closeDoc(0); |
| 90 | } else { |
| 91 | $first=0; |
| 92 | } |
| 93 | if($processedFilenames{$filename}) { |
| 94 | $log->warn("WARNING: $filename is already processed"); |
| 95 | } |
| 96 | $processedFilenames{$filename}=1; |
| 97 | $i=0; |
| 98 | } elsif(/^#\s*foundry\s*[:=]\s*(.*)/) { |
| 99 | if(!$foundry_name) { |
Marc Kupietz | dd546a8 | 2024-03-22 16:30:09 +0100 | [diff] [blame] | 100 | $dependency_foundry_name = $foundry_name = $1; |
| 101 | if ($foundry_name =~ /(.*) dependency:(.*)/) { |
| 102 | $foundry_name = $1; |
| 103 | $dependency_foundry_name = $2; |
| 104 | } |
Marc Kupietz | bcb55b8 | 2022-09-15 11:42:26 +0200 | [diff] [blame] | 105 | $log->debug("Foundry: $foundry_name\n"); |
| 106 | } else { |
| 107 | $log->debug("Ignored foundry name: $1\n"); |
| 108 | } |
| 109 | } elsif(/^#\s*generator\s*[=]\s*udpipe/i) { |
| 110 | if(!$foundry_name) { |
Marc Kupietz | dd546a8 | 2024-03-22 16:30:09 +0100 | [diff] [blame] | 111 | $dependency_foundry_name = $foundry_name = "ud"; |
Marc Kupietz | bcb55b8 | 2022-09-15 11:42:26 +0200 | [diff] [blame] | 112 | $log->debug("Foundry: $foundry_name\n"); |
| 113 | } else { |
| 114 | $log->debug("Ignored foundry name: ud\n"); |
| 115 | } |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 116 | } elsif(/^(?:#|0\.2)\s+text_id\s*[:=]\s*(.*)/) { |
Marc Kupietz | bcb55b8 | 2022-09-15 11:42:26 +0200 | [diff] [blame] | 117 | $docid=$1; |
Marc Kupietz | cc39147 | 2024-06-24 10:48:34 +0200 | [diff] [blame] | 118 | $docid =~ s/\s+$//; |
Marc Kupietz | bcb55b8 | 2022-09-15 11:42:26 +0200 | [diff] [blame] | 119 | my $docSigle = $docid; |
| 120 | $docSigle =~ s/\..*//; |
| 121 | if($docSigle ne $lastDocSigle) { |
| 122 | $log->info("Analyzing $docSigle"); |
| 123 | $lastDocSigle = $docSigle; |
| 124 | } |
| 125 | $known=$unknown=0; |
| 126 | $current=""; |
| 127 | $parser_file = dirname($filename); |
| 128 | $parser_file =~ s@(.*)/[^/]+$@$1@; |
| 129 | $morpho_file = $parser_file; |
| 130 | $morpho_file .= "/$foundry_name/morpho.xml"; |
Marc Kupietz | dd546a8 | 2024-03-22 16:30:09 +0100 | [diff] [blame] | 131 | $parser_file .= "/$dependency_foundry_name/dependency.xml"; |
Marc Kupietz | bcb55b8 | 2022-09-15 11:42:26 +0200 | [diff] [blame] | 132 | $parse = $morpho = layer_header($docid); |
| 133 | } elsif (/^(?:#|0\.3)\s+(?:start_offsets|from)\s*[:=]\s*(.*)/) { |
| 134 | @spansFrom = split(/\s+/, $1); |
| 135 | } elsif (/^(?:#|0\.4)\s+(?:end_offsets|to)\s+[:=]\s*(.*)/) { |
| 136 | @spansTo = split(/\s+/, $1); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 137 | } |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 138 | } elsif ( !/^\s*$/ ) { |
| 139 | if ( !$docid || scalar @spansTo == 0 || scalar @spansFrom == 0 ) { |
| 140 | if ( !$docid ) { |
| 141 | $log->warn("WARNING: No valid input document: text_id (e.g. '# text_id = GOE_AGA.00000') missing"); |
| 142 | } |
| 143 | if ( scalar @spansTo == 0 || scalar @spansFrom == 0 ) { |
| 144 | $log->warn("WARNING: No valid input document: token offsets missing"); |
| 145 | } |
| 146 | |
| 147 | # Skip to next potentially valid document |
| 148 | while (<$fh>) { |
| 149 | next MAIN if m!^\s*$!s; |
| 150 | } |
| 151 | }; |
Marc Kupietz | d50de7c | 2024-03-10 15:24:55 +0100 | [diff] [blame] | 152 | my @parsed = map { |
| 153 | my $s = $_; |
| 154 | $s =~ s/&/&/g; |
| 155 | $s =~ s/</</g; |
| 156 | $s =~ s/>/>/g; |
| 157 | $s; |
| 158 | } split('\t'); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 159 | chomp $parsed[9]; |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 160 | if (@parsed != 10) { |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 161 | $log->warn("WARNING: skipping strange parser output line in $docid"); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 162 | $i++; |
| 163 | next; |
| 164 | } |
| 165 | my $t=$parsed[0]; |
| 166 | if($t == 1) { |
| 167 | $s++; |
| 168 | $first_in_sentence = $i; |
| 169 | } |
| 170 | if($parsed[6] =~ /\d+/ && $parsed[7] !~ /_/) { |
| 171 | $write_syntax=1; |
| 172 | my $from=$spansFrom[$parsed[6]]; |
| 173 | my $to=$spansTo[$parsed[6]]; |
| 174 | $parse .= qq@<span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]"> |
| 175 | <rel label="$parsed[7]"> |
| 176 | <span from="$from" to="$to"/> |
| 177 | </rel> |
| 178 | </span> |
| 179 | @; |
Marc Kupietz | a591cdd | 2021-10-12 13:23:48 +0200 | [diff] [blame] | 180 | } |
Marc Kupietz | 5cc4df2 | 2024-03-24 13:46:42 +0100 | [diff] [blame] | 181 | my $pos = $parsed[4]; |
| 182 | my $upos = $parsed[3]; |
Marc Kupietz | a591cdd | 2021-10-12 13:23:48 +0200 | [diff] [blame] | 183 | $pos =~ s/\|.*//; |
| 184 | $morpho .= qq( <span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]"> |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 185 | <fs type="lex" xmlns="http://www.tei-c.org/ns/1.0"> |
| 186 | <f name="lex"> |
| 187 | <fs> |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 188 | ); |
Marc Kupietz | 5cc4df2 | 2024-03-24 13:46:42 +0100 | [diff] [blame] | 189 | if($pos ne "_") { |
| 190 | $morpho .= qq( <f name="pos">$pos</f>\n); |
| 191 | } |
| 192 | if($upos ne "_") { |
| 193 | $morpho .= qq( <f name="upos">$upos</f>\n); |
| 194 | } |
Marc Kupietz | 97ba2ba | 2021-10-11 17:55:47 +0200 | [diff] [blame] | 195 | $morpho .= qq( <f name="lemma">$parsed[2]</f>\n) if($parsed[2] ne "_" || $parsed[1] eq '_'); |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 196 | $morpho .= qq( <f name="msd">$parsed[5]</f>\n) if($parsed[5] ne "_"); |
| 197 | if($parsed[9] ne "_") { |
| 198 | if ($parsed[9] =~ /[0-9.e]+/) { |
| 199 | $morpho .= qq( <f name="certainty">$parsed[9]</f>\n) |
| 200 | } |
| 201 | else { |
| 202 | $morpho .= qq( <f name="misc">$parsed[9]</f>\n) |
| 203 | } |
| 204 | } |
| 205 | $morpho .= qq( </fs> |
| 206 | </f> |
| 207 | </fs> |
| 208 | </span> |
| 209 | ); |
| 210 | $i++; |
| 211 | } |
| 212 | } |
| 213 | $current .= "\n"; |
| 214 | closeDoc(1); |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 215 | $zip->close() if $zip; |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 216 | close($fh); |
| 217 | } |
| 218 | exit; |
| 219 | |
| 220 | sub newZipStream { |
| 221 | my ($fname) = @_; |
| 222 | if (defined $zip) { |
| 223 | $zip->newStream(Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, |
Marc Kupietz | 447f475 | 2024-03-22 17:35:57 +0100 | [diff] [blame] | 224 | Append => 1, Name => $fname, ExtAttr => 0100666 << 16) |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 225 | or die "ERROR ('$fname'): zip failed: $ZipError\n"; |
| 226 | } else { |
| 227 | $zip = new IO::Compress::Zip $outh, Zip64 => 1, TextFlag => 1, |
Marc Kupietz | 447f475 | 2024-03-22 17:35:57 +0100 | [diff] [blame] | 228 | Method => $_COMPRESSION_METHOD, Append => 0, Name => "$fname", ExtAttr => 0100666 << 16 |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 229 | or die "ERROR ('$fname'): zip failed: $ZipError\n"; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | sub closeDoc { |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 234 | if ($write_morpho && $morpho_file) { |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 235 | newZipStream($morpho_file); |
| 236 | $zip->print($morpho, qq( </spanList>\n</layer>\n)); |
| 237 | } |
Akron | 49f333b | 2022-09-27 17:03:49 +0200 | [diff] [blame] | 238 | if ($write_syntax && $parser_file) { |
Marc Kupietz | 79ba1e5 | 2021-02-12 17:26:54 +0100 | [diff] [blame] | 239 | $write_syntax = 0; |
| 240 | newZipStream($parser_file); |
| 241 | $zip->print($parse, qq(</spanList>\n</layer>\n)); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | sub layer_header { |
| 246 | my ($docid) = @_; |
| 247 | return(qq(<?xml version="1.0" encoding="UTF-8"?> |
| 248 | <?xml-model href="span.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?> |
| 249 | <layer docid="$docid" xmlns="http://ids-mannheim.de/ns/KorAP" version="KorAP-0.4"> |
| 250 | <spanList> |
| 251 | )); |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | =pod |
| 255 | |
| 256 | =encoding utf8 |
| 257 | |
| 258 | =head1 NAME |
| 259 | |
| 260 | conllu2korapxml - Conversion of KorAP-XML CoNLL-U to KorAP-XML zips |
| 261 | |
| 262 | =head1 SYNOPSIS |
| 263 | |
| 264 | conllu2korapxml < zca15.tree_tagger.conllu > zca15.tree_tagger.zip |
| 265 | |
| 266 | =head1 DESCRIPTION |
| 267 | |
| 268 | C<conllu2korapxml> converts CoNLL-U files that follow KorAP-specific comment conventions |
| 269 | and contain morphosyntactic and/or dependency annotations to |
| 270 | corresponding KorAP-XML zip files. |
| 271 | |
| 272 | =head1 INSTALLATION |
| 273 | |
| 274 | $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git |
| 275 | |
| 276 | =head1 OPTIONS |
| 277 | |
| 278 | =over 2 |
| 279 | |
| 280 | =item B<--force-foundry|-f> |
| 281 | |
| 282 | Set foundry name and ignore foundry names in the input. |
| 283 | |
Marc Kupietz | dd546a8 | 2024-03-22 16:30:09 +0100 | [diff] [blame] | 284 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 285 | =item B<--help|-h> |
| 286 | |
| 287 | Print help information. |
| 288 | |
| 289 | =item B<--version|-v> |
| 290 | |
| 291 | Print version information. |
| 292 | |
| 293 | |
| 294 | =item B<--log|-l> |
| 295 | |
| 296 | Loglevel for I<Log::Any>. Defaults to C<warn>. |
| 297 | |
Marc Kupietz | 187abd7 | 2024-06-25 14:30:01 +0200 | [diff] [blame^] | 298 | =item B<--output|-o> |
| 299 | |
| 300 | Output file. Defaults to C<-> (stdout). |
| 301 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 302 | =back |
| 303 | |
| 304 | =head1 EXAMPLES |
| 305 | |
| 306 | conllu2korapxml -f tree_tagger < t/data/wdf19.morpho.conllu > wdf19.tree_tagger.zip |
| 307 | |
Marc Kupietz | dd546a8 | 2024-03-22 16:30:09 +0100 | [diff] [blame] | 308 | conllu2korapxml -f "tree_tagger dependency:malt" < t/data/wdf19.tt-malt.conllu > wdf19.tree_tagger.zip |
| 309 | |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 310 | =head1 COPYRIGHT AND LICENSE |
| 311 | |
Akron | 249fc83 | 2024-06-04 16:36:44 +0200 | [diff] [blame] | 312 | Copyright (C) 2021-2024, L<IDS Mannheim|https://www.ids-mannheim.de/> |
Marc Kupietz | aeb84a0 | 2021-10-11 17:57:29 +0200 | [diff] [blame] | 313 | |
| 314 | Author: Marc Kupietz |
| 315 | |
| 316 | Contributors: Nils Diewald |
| 317 | |
| 318 | L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/> |
| 319 | Corpus Analysis Platform at the |
| 320 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 321 | member of the |
| 322 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. |
| 323 | |
| 324 | This program is free software published under the |
| 325 | L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>. |