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