blob: 910ced3ce98b3f6d7d65d49f7b891b28efa9ca4a [file] [log] [blame]
Marc Kupietz79ba1e52021-02-12 17:26:54 +01001#!/usr/bin/env perl
2use strict;
3use warnings;
4use POSIX;
Marc Kupietzaeb84a02021-10-11 17:57:29 +02005use Getopt::Long qw(GetOptions :config no_auto_abbrev);
6use Log::Any '$log';
7use Log::Any::Adapter;
Marc Kupietz79ba1e52021-02-12 17:26:54 +01008use Encode;
9use IO::Compress::Zip qw(zip $ZipError :constants);
10use File::Basename;
Marc Kupietzaeb84a02021-10-11 17:57:29 +020011use Pod::Usage;
Marc Kupietz79ba1e52021-02-12 17:26:54 +010012
13my $_COMPRESSION_METHOD = ZIP_CM_DEFLATE;
14my %opts;
15my %processedFilenames;
16
Marc Kupietz6dc97b72024-01-24 13:01:33 +010017our $VERSION = '0.6.2';
Marc Kupietzaeb84a02021-10-11 17:57:29 +020018our $VERSION_MSG = "\nconllu2korapxml - v$VERSION\n";
Marc Kupietz4cc243a2021-10-11 17:15:16 +020019
Marc Kupietzaeb84a02021-10-11 17:57:29 +020020use constant {
21 # Set to 1 for minimal more debug output (no need to be parametrized)
22 DEBUG => $ENV{KORAPXMLCONLLU_DEBUG} // 0
23};
Marc Kupietz79ba1e52021-02-12 17:26:54 +010024
Marc Kupietzaeb84a02021-10-11 17:57:29 +020025GetOptions(
26 'force-foundry|f=s' => \(my $foundry_name = ''),
27 'log|l=s' => \(my $log_level = 'warn'),
Marc Kupietz79ba1e52021-02-12 17:26:54 +010028
Marc Kupietzaeb84a02021-10-11 17:57:29 +020029 '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 Kupietz79ba1e52021-02-12 17:26:54 +010045
Marc Kupietzaeb84a02021-10-11 17:57:29 +020046# Establish logger
47binmode(STDERR, ':encoding(UTF-8)');
48Log::Any::Adapter->set('Stderr', log_level => $log_level);
49$log->notice('Debugging is activated') if DEBUG;
Marc Kupietz79ba1e52021-02-12 17:26:54 +010050
51my $docid="";
52my $zip = undef;
Akron1ce1bb52023-03-22 08:18:08 +010053my $outh = '-';
Marc Kupietz79ba1e52021-02-12 17:26:54 +010054my $parser_file;
55my $parse;
56my $morpho_file;
57my $morpho;
58my @spansFrom;
59my @spansTo;
60my $current;
61my ($unknown, $known) = (0, 0);
62
63my ($write_morpho, $write_syntax, $base) = (1, 0, 0);
64my $filename;
Marc Kupietz79ba1e52021-02-12 17:26:54 +010065my $first=1;
66my @conllu_files = @ARGV;
67push @conllu_files, "-" if (@conllu_files == 0);
68my $fh;
69foreach 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="";
Akron49f333b2022-09-27 17:03:49 +020077 MAIN: while (<$fh>) {
Marc Kupietzbcb55b82022-09-15 11:42:26 +020078 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 }
Akron49f333b2022-09-27 17:03:49 +0200105 } elsif(/^(?:#|0\.2)\s+text_id\s*[:=]\s*(.*)/) {
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200106 $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 Kupietz79ba1e52021-02-12 17:26:54 +0100125 }
Akron49f333b2022-09-27 17:03:49 +0200126 } 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 Kupietzd50de7c2024-03-10 15:24:55 +0100140 my @parsed = map {
141 my $s = $_;
142 $s =~ s/&/&amp;/g;
143 $s =~ s/</&lt;/g;
144 $s =~ s/>/&gt;/g;
145 $s;
146 } split('\t');
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100147 chomp $parsed[9];
Akron49f333b2022-09-27 17:03:49 +0200148 if (@parsed != 10) {
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200149 $log->warn("WARNING: skipping strange parser output line in $docid");
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100150 $i++;
151 next;
152 }
153 my $t=$parsed[0];
154 if($t == 1) {
155 $s++;
156 $first_in_sentence = $i;
157 }
158 if($parsed[6] =~ /\d+/ && $parsed[7] !~ /_/) {
159 $write_syntax=1;
160 my $from=$spansFrom[$parsed[6]];
161 my $to=$spansTo[$parsed[6]];
162 $parse .= qq@<span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]">
163<rel label="$parsed[7]">
164<span from="$from" to="$to"/>
165</rel>
166</span>
167@;
Marc Kupietza591cdd2021-10-12 13:23:48 +0200168 }
169 my $pos = $parsed[3];
170 $pos =~ s/\|.*//;
171 $morpho .= qq( <span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]">
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100172 <fs type="lex" xmlns="http://www.tei-c.org/ns/1.0">
173 <f name="lex">
174 <fs>
Marc Kupietza591cdd2021-10-12 13:23:48 +0200175 <f name="pos">$pos</f>
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100176);
Marc Kupietz97ba2ba2021-10-11 17:55:47 +0200177 $morpho .= qq( <f name="lemma">$parsed[2]</f>\n) if($parsed[2] ne "_" || $parsed[1] eq '_');
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100178 $morpho .= qq( <f name="msd">$parsed[5]</f>\n) if($parsed[5] ne "_");
179 if($parsed[9] ne "_") {
180 if ($parsed[9] =~ /[0-9.e]+/) {
181 $morpho .= qq( <f name="certainty">$parsed[9]</f>\n)
182 }
183 else {
184 $morpho .= qq( <f name="misc">$parsed[9]</f>\n)
185 }
186 }
187 $morpho .= qq( </fs>
188 </f>
189 </fs>
190 </span>
191);
192 $i++;
193 }
194 }
195 $current .= "\n";
196 closeDoc(1);
Akron49f333b2022-09-27 17:03:49 +0200197 $zip->close() if $zip;
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100198 close($fh);
199}
200exit;
201
202sub newZipStream {
203 my ($fname) = @_;
204 if (defined $zip) {
205 $zip->newStream(Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD,
Marc Kupietz447f4752024-03-22 17:35:57 +0100206 Append => 1, Name => $fname, ExtAttr => 0100666 << 16)
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100207 or die "ERROR ('$fname'): zip failed: $ZipError\n";
208 } else {
209 $zip = new IO::Compress::Zip $outh, Zip64 => 1, TextFlag => 1,
Marc Kupietz447f4752024-03-22 17:35:57 +0100210 Method => $_COMPRESSION_METHOD, Append => 0, Name => "$fname", ExtAttr => 0100666 << 16
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100211 or die "ERROR ('$fname'): zip failed: $ZipError\n";
212 }
213}
214
215sub closeDoc {
Akron49f333b2022-09-27 17:03:49 +0200216 if ($write_morpho && $morpho_file) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100217 newZipStream($morpho_file);
218 $zip->print($morpho, qq( </spanList>\n</layer>\n));
219 }
Akron49f333b2022-09-27 17:03:49 +0200220 if ($write_syntax && $parser_file) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100221 $write_syntax = 0;
222 newZipStream($parser_file);
223 $zip->print($parse, qq(</spanList>\n</layer>\n));
224 }
225}
226
227sub layer_header {
228 my ($docid) = @_;
229 return(qq(<?xml version="1.0" encoding="UTF-8"?>
230<?xml-model href="span.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
231<layer docid="$docid" xmlns="http://ids-mannheim.de/ns/KorAP" version="KorAP-0.4">
232<spanList>
233));
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200234}
235
236=pod
237
238=encoding utf8
239
240=head1 NAME
241
242conllu2korapxml - Conversion of KorAP-XML CoNLL-U to KorAP-XML zips
243
244=head1 SYNOPSIS
245
246 conllu2korapxml < zca15.tree_tagger.conllu > zca15.tree_tagger.zip
247
248=head1 DESCRIPTION
249
250C<conllu2korapxml> converts CoNLL-U files that follow KorAP-specific comment conventions
251 and contain morphosyntactic and/or dependency annotations to
252 corresponding KorAP-XML zip files.
253
254=head1 INSTALLATION
255
256 $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git
257
258=head1 OPTIONS
259
260=over 2
261
262=item B<--force-foundry|-f>
263
264Set foundry name and ignore foundry names in the input.
265
266=item B<--help|-h>
267
268Print help information.
269
270=item B<--version|-v>
271
272Print version information.
273
274
275=item B<--log|-l>
276
277Loglevel for I<Log::Any>. Defaults to C<warn>.
278
279=back
280
281=head1 EXAMPLES
282
283 conllu2korapxml -f tree_tagger < t/data/wdf19.morpho.conllu > wdf19.tree_tagger.zip
284
285=head1 COPYRIGHT AND LICENSE
286
287Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
288
289Author: Marc Kupietz
290
291Contributors: Nils Diewald
292
293L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
294Corpus Analysis Platform at the
295L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
296member of the
297L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
298
299This program is free software published under the
300L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.