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