blob: 29df253a047d4605983a591d4a119bc2449fbff9 [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;
Marc Kupietzdd546a82024-03-22 16:30:09 +010069
70my $dependency_foundry_name = $foundry_name;
71if ($foundry_name =~ /(.*) dependency:(.*)/) {
72 $foundry_name = $1;
73 $dependency_foundry_name = $2;
74}
75
Marc Kupietz79ba1e52021-02-12 17:26:54 +010076foreach 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="";
Akron49f333b2022-09-27 17:03:49 +020084 MAIN: while (<$fh>) {
Marc Kupietzbcb55b82022-09-15 11:42:26 +020085 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 Kupietzdd546a82024-03-22 16:30:09 +0100100 $dependency_foundry_name = $foundry_name = $1;
101 if ($foundry_name =~ /(.*) dependency:(.*)/) {
102 $foundry_name = $1;
103 $dependency_foundry_name = $2;
104 }
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200105 $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 Kupietzdd546a82024-03-22 16:30:09 +0100111 $dependency_foundry_name = $foundry_name = "ud";
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200112 $log->debug("Foundry: $foundry_name\n");
113 } else {
114 $log->debug("Ignored foundry name: ud\n");
115 }
Akron49f333b2022-09-27 17:03:49 +0200116 } elsif(/^(?:#|0\.2)\s+text_id\s*[:=]\s*(.*)/) {
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200117 $docid=$1;
118 my $docSigle = $docid;
119 $docSigle =~ s/\..*//;
120 if($docSigle ne $lastDocSigle) {
121 $log->info("Analyzing $docSigle");
122 $lastDocSigle = $docSigle;
123 }
124 $known=$unknown=0;
125 $current="";
126 $parser_file = dirname($filename);
127 $parser_file =~ s@(.*)/[^/]+$@$1@;
128 $morpho_file = $parser_file;
129 $morpho_file .= "/$foundry_name/morpho.xml";
Marc Kupietzdd546a82024-03-22 16:30:09 +0100130 $parser_file .= "/$dependency_foundry_name/dependency.xml";
Marc Kupietzbcb55b82022-09-15 11:42:26 +0200131 $parse = $morpho = layer_header($docid);
132 } elsif (/^(?:#|0\.3)\s+(?:start_offsets|from)\s*[:=]\s*(.*)/) {
133 @spansFrom = split(/\s+/, $1);
134 } elsif (/^(?:#|0\.4)\s+(?:end_offsets|to)\s+[:=]\s*(.*)/) {
135 @spansTo = split(/\s+/, $1);
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100136 }
Akron49f333b2022-09-27 17:03:49 +0200137 } elsif ( !/^\s*$/ ) {
138 if ( !$docid || scalar @spansTo == 0 || scalar @spansFrom == 0 ) {
139 if ( !$docid ) {
140 $log->warn("WARNING: No valid input document: text_id (e.g. '# text_id = GOE_AGA.00000') missing");
141 }
142 if ( scalar @spansTo == 0 || scalar @spansFrom == 0 ) {
143 $log->warn("WARNING: No valid input document: token offsets missing");
144 }
145
146 # Skip to next potentially valid document
147 while (<$fh>) {
148 next MAIN if m!^\s*$!s;
149 }
150 };
Marc Kupietzd50de7c2024-03-10 15:24:55 +0100151 my @parsed = map {
152 my $s = $_;
153 $s =~ s/&/&amp;/g;
154 $s =~ s/</&lt;/g;
155 $s =~ s/>/&gt;/g;
156 $s;
157 } split('\t');
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100158 chomp $parsed[9];
Akron49f333b2022-09-27 17:03:49 +0200159 if (@parsed != 10) {
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200160 $log->warn("WARNING: skipping strange parser output line in $docid");
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100161 $i++;
162 next;
163 }
164 my $t=$parsed[0];
165 if($t == 1) {
166 $s++;
167 $first_in_sentence = $i;
168 }
169 if($parsed[6] =~ /\d+/ && $parsed[7] !~ /_/) {
170 $write_syntax=1;
171 my $from=$spansFrom[$parsed[6]];
172 my $to=$spansTo[$parsed[6]];
173 $parse .= qq@<span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]">
174<rel label="$parsed[7]">
175<span from="$from" to="$to"/>
176</rel>
177</span>
178@;
Marc Kupietza591cdd2021-10-12 13:23:48 +0200179 }
180 my $pos = $parsed[3];
181 $pos =~ s/\|.*//;
182 $morpho .= qq( <span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]">
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100183 <fs type="lex" xmlns="http://www.tei-c.org/ns/1.0">
184 <f name="lex">
185 <fs>
Marc Kupietza591cdd2021-10-12 13:23:48 +0200186 <f name="pos">$pos</f>
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100187);
Marc Kupietz97ba2ba2021-10-11 17:55:47 +0200188 $morpho .= qq( <f name="lemma">$parsed[2]</f>\n) if($parsed[2] ne "_" || $parsed[1] eq '_');
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100189 $morpho .= qq( <f name="msd">$parsed[5]</f>\n) if($parsed[5] ne "_");
190 if($parsed[9] ne "_") {
191 if ($parsed[9] =~ /[0-9.e]+/) {
192 $morpho .= qq( <f name="certainty">$parsed[9]</f>\n)
193 }
194 else {
195 $morpho .= qq( <f name="misc">$parsed[9]</f>\n)
196 }
197 }
198 $morpho .= qq( </fs>
199 </f>
200 </fs>
201 </span>
202);
203 $i++;
204 }
205 }
206 $current .= "\n";
207 closeDoc(1);
Akron49f333b2022-09-27 17:03:49 +0200208 $zip->close() if $zip;
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100209 close($fh);
210}
211exit;
212
213sub newZipStream {
214 my ($fname) = @_;
215 if (defined $zip) {
216 $zip->newStream(Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD,
Marc Kupietz447f4752024-03-22 17:35:57 +0100217 Append => 1, Name => $fname, ExtAttr => 0100666 << 16)
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100218 or die "ERROR ('$fname'): zip failed: $ZipError\n";
219 } else {
220 $zip = new IO::Compress::Zip $outh, Zip64 => 1, TextFlag => 1,
Marc Kupietz447f4752024-03-22 17:35:57 +0100221 Method => $_COMPRESSION_METHOD, Append => 0, Name => "$fname", ExtAttr => 0100666 << 16
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100222 or die "ERROR ('$fname'): zip failed: $ZipError\n";
223 }
224}
225
226sub closeDoc {
Akron49f333b2022-09-27 17:03:49 +0200227 if ($write_morpho && $morpho_file) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100228 newZipStream($morpho_file);
229 $zip->print($morpho, qq( </spanList>\n</layer>\n));
230 }
Akron49f333b2022-09-27 17:03:49 +0200231 if ($write_syntax && $parser_file) {
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100232 $write_syntax = 0;
233 newZipStream($parser_file);
234 $zip->print($parse, qq(</spanList>\n</layer>\n));
235 }
236}
237
238sub layer_header {
239 my ($docid) = @_;
240 return(qq(<?xml version="1.0" encoding="UTF-8"?>
241<?xml-model href="span.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
242<layer docid="$docid" xmlns="http://ids-mannheim.de/ns/KorAP" version="KorAP-0.4">
243<spanList>
244));
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200245}
246
247=pod
248
249=encoding utf8
250
251=head1 NAME
252
253conllu2korapxml - Conversion of KorAP-XML CoNLL-U to KorAP-XML zips
254
255=head1 SYNOPSIS
256
257 conllu2korapxml < zca15.tree_tagger.conllu > zca15.tree_tagger.zip
258
259=head1 DESCRIPTION
260
261C<conllu2korapxml> converts CoNLL-U files that follow KorAP-specific comment conventions
262 and contain morphosyntactic and/or dependency annotations to
263 corresponding KorAP-XML zip files.
264
265=head1 INSTALLATION
266
267 $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git
268
269=head1 OPTIONS
270
271=over 2
272
273=item B<--force-foundry|-f>
274
275Set foundry name and ignore foundry names in the input.
276
Marc Kupietzdd546a82024-03-22 16:30:09 +0100277
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200278=item B<--help|-h>
279
280Print help information.
281
282=item B<--version|-v>
283
284Print version information.
285
286
287=item B<--log|-l>
288
289Loglevel for I<Log::Any>. Defaults to C<warn>.
290
291=back
292
293=head1 EXAMPLES
294
295 conllu2korapxml -f tree_tagger < t/data/wdf19.morpho.conllu > wdf19.tree_tagger.zip
296
Marc Kupietzdd546a82024-03-22 16:30:09 +0100297 conllu2korapxml -f "tree_tagger dependency:malt" < t/data/wdf19.tt-malt.conllu > wdf19.tree_tagger.zip
298
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200299=head1 COPYRIGHT AND LICENSE
300
301Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
302
303Author: Marc Kupietz
304
305Contributors: Nils Diewald
306
307L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
308Corpus Analysis Platform at the
309L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
310member of the
311L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
312
313This program is free software published under the
314L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.