blob: a9bb0306ad4a3f0f8a168ad62d30a5c673a47306 [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 Kupietz4cc243a2021-10-11 17:15:16 +020017our $VERSION = '0.4.1.9000';
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;
53my $outh = \*STDOUT;
54my $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="";
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 Kupietzaeb84a02021-10-11 17:57:29 +020086 $log->warn("WARNING: $filename is already processed");
Marc Kupietz79ba1e52021-02-12 17:26:54 +010087 }
88 $processedFilenames{$filename}=1;
89 $i=0;
90 } elsif(/^#\s*foundry\s*[:=]\s*(.*)/) {
Marc Kupietzaeb84a02021-10-11 17:57:29 +020091 if(!$foundry_name) {
92 $foundry_name = $1;
93 $log->debug("Foundry: $foundry_name\n");
94 }
Marc Kupietz79ba1e52021-02-12 17:26:54 +010095 } elsif(/^(?:#|0\.2)\s+.*id\s*[:=]\s*(.*)/) {
96 $docid=$1;
97 my $docSigle = $docid;
98 $docSigle =~ s/\..*//;
99 if($docSigle ne $lastDocSigle) {
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200100 $log->info("Analyzing $docSigle");
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100101 $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 Kupietzaeb84a02021-10-11 17:57:29 +0200119 $log->warn("WARNING: skipping strange parser output line in $docid");
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100120 $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@;
Marc Kupietza591cdd2021-10-12 13:23:48 +0200138 }
139 my $pos = $parsed[3];
140 $pos =~ s/\|.*//;
141 $morpho .= qq( <span id="s${s}_n$t" from="$spansFrom[$t]" to="$spansTo[$t]">
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100142 <fs type="lex" xmlns="http://www.tei-c.org/ns/1.0">
143 <f name="lex">
144 <fs>
Marc Kupietza591cdd2021-10-12 13:23:48 +0200145 <f name="pos">$pos</f>
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100146);
Marc Kupietz97ba2ba2021-10-11 17:55:47 +0200147 $morpho .= qq( <f name="lemma">$parsed[2]</f>\n) if($parsed[2] ne "_" || $parsed[1] eq '_');
Marc Kupietz79ba1e52021-02-12 17:26:54 +0100148 $morpho .= qq( <f name="msd">$parsed[5]</f>\n) if($parsed[5] ne "_");
149 if($parsed[9] ne "_") {
150 if ($parsed[9] =~ /[0-9.e]+/) {
151 $morpho .= qq( <f name="certainty">$parsed[9]</f>\n)
152 }
153 else {
154 $morpho .= qq( <f name="misc">$parsed[9]</f>\n)
155 }
156 }
157 $morpho .= qq( </fs>
158 </f>
159 </fs>
160 </span>
161);
162 $i++;
163 }
164 }
165 $current .= "\n";
166 closeDoc(1);
167 $zip->close();
168 close($fh);
169}
170exit;
171
172sub newZipStream {
173 my ($fname) = @_;
174 if (defined $zip) {
175 $zip->newStream(Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD,
176 Append => 1, Name => $fname)
177 or die "ERROR ('$fname'): zip failed: $ZipError\n";
178 } else {
179 $zip = new IO::Compress::Zip $outh, Zip64 => 1, TextFlag => 1,
180 Method => $_COMPRESSION_METHOD, Append => 1, Name => "$fname"
181 or die "ERROR ('$fname'): zip failed: $ZipError\n";
182 }
183}
184
185sub closeDoc {
186 if ($write_morpho) {
187 newZipStream($morpho_file);
188 $zip->print($morpho, qq( </spanList>\n</layer>\n));
189 }
190 if ($write_syntax) {
191 $write_syntax = 0;
192 newZipStream($parser_file);
193 $zip->print($parse, qq(</spanList>\n</layer>\n));
194 }
195}
196
197sub layer_header {
198 my ($docid) = @_;
199 return(qq(<?xml version="1.0" encoding="UTF-8"?>
200<?xml-model href="span.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
201<layer docid="$docid" xmlns="http://ids-mannheim.de/ns/KorAP" version="KorAP-0.4">
202<spanList>
203));
Marc Kupietzaeb84a02021-10-11 17:57:29 +0200204}
205
206=pod
207
208=encoding utf8
209
210=head1 NAME
211
212conllu2korapxml - Conversion of KorAP-XML CoNLL-U to KorAP-XML zips
213
214=head1 SYNOPSIS
215
216 conllu2korapxml < zca15.tree_tagger.conllu > zca15.tree_tagger.zip
217
218=head1 DESCRIPTION
219
220C<conllu2korapxml> converts CoNLL-U files that follow KorAP-specific comment conventions
221 and contain morphosyntactic and/or dependency annotations to
222 corresponding KorAP-XML zip files.
223
224=head1 INSTALLATION
225
226 $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git
227
228=head1 OPTIONS
229
230=over 2
231
232=item B<--force-foundry|-f>
233
234Set foundry name and ignore foundry names in the input.
235
236=item B<--help|-h>
237
238Print help information.
239
240=item B<--version|-v>
241
242Print version information.
243
244
245=item B<--log|-l>
246
247Loglevel for I<Log::Any>. Defaults to C<warn>.
248
249=back
250
251=head1 EXAMPLES
252
253 conllu2korapxml -f tree_tagger < t/data/wdf19.morpho.conllu > wdf19.tree_tagger.zip
254
255=head1 COPYRIGHT AND LICENSE
256
257Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
258
259Author: Marc Kupietz
260
261Contributors: Nils Diewald
262
263L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
264Corpus Analysis Platform at the
265L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
266member of the
267L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
268
269This program is free software published under the
270L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.