Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use POSIX; |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 5 | use Log::Any '$log'; |
| 6 | use Log::Any::Adapter; |
| 7 | use Pod::Usage; |
| 8 | use Getopt::Long qw(GetOptions :config no_auto_abbrev); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 9 | use Encode; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 10 | |
| 11 | my $MAX_SENTENCE_LENGTH=10000; |
| 12 | my $COMMENT_START="#"; |
Marc Kupietz | a2680b9 | 2021-10-11 17:24:28 +0200 | [diff] [blame] | 13 | my $COMMENT_END=""; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 14 | |
| 15 | my $test=0; |
| 16 | my $text_no=0; |
| 17 | my %opts; |
| 18 | my %plain_texts; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 19 | my %sentence_ends; |
| 20 | |
Marc Kupietz | 4cc243a | 2021-10-11 17:15:16 +0200 | [diff] [blame] | 21 | our $VERSION = '0.4.1.9000'; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 22 | |
Marc Kupietz | 0ab8a2c | 2021-03-19 16:21:00 +0100 | [diff] [blame] | 23 | our $VERSION_MSG = "\nkorapxml2conllu - v$VERSION\n"; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 24 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 25 | use constant { |
| 26 | # Set to 1 for minimal more debug output (no need to be parametrized) |
| 27 | DEBUG => $ENV{KORAPXMLCONLLU_DEBUG} // 0 |
| 28 | }; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 29 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 30 | GetOptions( |
| 31 | 'sigle-pattern|p=s' => \(my $sigle_pattern = ''), |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 32 | 'extract-attributes-regex|e=s' => \(my $extract_attributes_regex = ''), |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 33 | 's-bounds-from-morpho' => \(my $s_bounds_from_morpho = 0), |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 34 | 'log|l=s' => \(my $log_level = 'warn'), |
Marc Kupietz | d7d5d6a | 2021-10-11 17:52:58 +0200 | [diff] [blame] | 35 | 'columns|c=n' => \(my $columns = 10), |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 36 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 37 | 'help|h' => sub { |
| 38 | pod2usage( |
| 39 | -verbose => 99, |
| 40 | -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS|EXAMPLES', |
| 41 | -msg => $VERSION_MSG, |
| 42 | -output => '-' |
| 43 | ) |
| 44 | }, |
| 45 | 'version|v' => sub { |
| 46 | pod2usage( |
| 47 | -verbose => 0, |
| 48 | -msg => $VERSION_MSG, |
| 49 | -output => '-' |
| 50 | ); |
| 51 | } |
| 52 | ); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 53 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 54 | # Establish logger |
| 55 | binmode(STDERR, ':encoding(UTF-8)'); |
| 56 | Log::Any::Adapter->set('Stderr', log_level => $log_level); |
| 57 | $log->notice('Debugging is activated') if DEBUG; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 58 | |
| 59 | my $docid=""; |
| 60 | my ($current_id, $current_from, $current_to, $token); |
| 61 | my $current; |
| 62 | my ($unknown, $known) = (0, 0); |
| 63 | my @current_lines; |
| 64 | my %processedFilenames; |
| 65 | my $zipsiglepattern = (defined($ENV{ZIPSIGLEPATTERN})? $ENV{ZIPSIGLEPATTERN} : ""); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 66 | my $baseOnly; |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 67 | my %extras; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 68 | |
| 69 | my ($ID_idx, $FORM_idx, $LEMMA_idx, $UPOS_idx, $XPOS_idx, $FEATS_idx, $HEAD_idx, $DEPREC_idx, $DEPS_idx, $MISC_idx) = (0..9); |
| 70 | |
Marc Kupietz | c7d1b93 | 2020-09-23 13:17:17 +0200 | [diff] [blame] | 71 | my $UNZIP = `sh -c 'command -v unzip'`; |
| 72 | chomp $UNZIP; |
| 73 | |
| 74 | |
| 75 | if ($UNZIP eq '') { |
| 76 | warn('No unzip executable found in PATH.'); |
| 77 | return 0; |
| 78 | }; |
| 79 | |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 80 | foreach my $morpho_zip (@ARGV) { |
| 81 | die "cannot open $morpho_zip" if(! -r $morpho_zip); |
| 82 | my $data_zip = $morpho_zip; |
| 83 | if ($data_zip !~ /\.zip/ && $data_zip =~ /\.conllu?/i) { |
| 84 | open(CONLL, "<$data_zip") or die "cannot open $data_zip"; |
| 85 | while(<CONLL>) { |
| 86 | print; |
| 87 | } |
| 88 | close(CONLL); |
| 89 | next; |
| 90 | } |
Marc Kupietz | fdd2d12 | 2021-10-12 10:42:34 +0200 | [diff] [blame] | 91 | $data_zip =~ s/\.([^\/.]+)\.zip$/.zip/; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 92 | my $foundry = $1; |
| 93 | die "cannot open data file $data_zip corresponding to $morpho_zip" if(! -r $data_zip); |
| 94 | |
| 95 | my $first=1; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 96 | my @conll = ("_") x 10; |
| 97 | my $filename; |
| 98 | |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 99 | $baseOnly = $morpho_zip eq $data_zip; |
| 100 | my ($morphoOrTokenCommand, $plaintextAndStructureCommand); |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 101 | if (!$baseOnly) { |
| 102 | $morphoOrTokenCommand = "$UNZIP -c $morpho_zip '*/${sigle_pattern}*/*/*/morpho.xml' $zipsiglepattern |"; |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 103 | if ($extract_attributes_regex || !$s_bounds_from_morpho) { |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 104 | $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/[sd][ta]*.xml' $zipsiglepattern |"; |
| 105 | } else { |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 106 | $log->debug("Not reading structure information."); |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 107 | $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/data.xml' $zipsiglepattern |"; |
| 108 | } |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 109 | } else { |
| 110 | $foundry = "base"; |
Marc Kupietz | f1fdc19 | 2021-10-08 13:29:59 +0200 | [diff] [blame] | 111 | $morphoOrTokenCommand = "$UNZIP -l $morpho_zip '*/${sigle_pattern}*/*/*/morpho.xml' $zipsiglepattern"; |
| 112 | if (`$morphoOrTokenCommand` !~ /morpho\.xml/) { |
| 113 | $morphoOrTokenCommand =~ s/morpho\.xml/tokens.xml/; |
| 114 | } else { |
| 115 | $baseOnly = 0; |
| 116 | } |
| 117 | $morphoOrTokenCommand =~ s/-l/-c/; |
| 118 | $morphoOrTokenCommand .= ' |'; |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 119 | $plaintextAndStructureCommand = "$UNZIP -c $data_zip '*/${sigle_pattern}*/*/[sd][ta]*.xml' $zipsiglepattern |"; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | open (MORPHO_OR_TOKENPIPE, $morphoOrTokenCommand) or die "cannot unzip $morpho_zip"; |
| 123 | open (PLAINTEXTPIPE, $plaintextAndStructureCommand) or die "cannot unzip $data_zip"; |
Marc Kupietz | a2680b9 | 2021-10-11 17:24:28 +0200 | [diff] [blame] | 124 | print "$COMMENT_START foundry = $foundry$COMMENT_END\n"; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 125 | while (<MORPHO_OR_TOKENPIPE>) { |
Marc Kupietz | 30c41b1 | 2020-09-22 14:32:34 +0200 | [diff] [blame] | 126 | if (/^ inflating: (.*)/) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 127 | $filename=$1; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 128 | while($processedFilenames{$filename} && !eof(MORPHO_OR_TOKENPIPE)) { |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 129 | $log->warn("$filename already processed"); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 130 | while (<MORPHO_OR_TOKENPIPE>) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 131 | last if(/\s+inflating:\s+(.*)/); |
| 132 | } |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 133 | $filename=$1 if(!eof(MORPHO_OR_TOKENPIPE) && /\s+inflating:\s+(.*)/); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 134 | } |
Marc Kupietz | 30c41b1 | 2020-09-22 14:32:34 +0200 | [diff] [blame] | 135 | } elsif(m@^\s*<layer\s+.*docid="([^"]+)"@) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 136 | last if($test && $text_no++ > 3); |
| 137 | if(!$first) { |
| 138 | closeDoc(0); |
| 139 | } |
| 140 | $processedFilenames{$filename}=1; |
| 141 | $docid=$1; |
| 142 | @current_lines=(); |
| 143 | $known=$unknown=0; |
| 144 | $current=""; |
| 145 | if ($first) { |
| 146 | $first = 0; |
| 147 | } |
| 148 | if(!fetch_plaintext($docid)) { # skip this text |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 149 | while (<MORPHO_OR_TOKENPIPE>) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 150 | last if(m@</layer>@); |
| 151 | } |
| 152 | } |
Marc Kupietz | a2680b9 | 2021-10-11 17:24:28 +0200 | [diff] [blame] | 153 | print STDOUT "$COMMENT_START filename = $filename$COMMENT_END\n$COMMENT_START text_id = $docid$COMMENT_END\n"; |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 154 | $log->debug("Analyzing $docid"); |
Marc Kupietz | 30c41b1 | 2020-09-22 14:32:34 +0200 | [diff] [blame] | 155 | } elsif (m@^\s*<f\s+.*name="([^"]+)">([^<]+)</f>@) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 156 | if ($1 eq "lemma") { |
| 157 | $conll[$LEMMA_idx] = $2; |
| 158 | $conll[$LEMMA_idx] =~ s/[\t\n\r]//g; # make sure that lemmas never contain tabs or newlines |
| 159 | if($conll[$LEMMA_idx] eq 'UNKNOWN') { |
| 160 | $conll[$LEMMA_idx] = "--"; |
| 161 | $unknown++; |
| 162 | } else { |
| 163 | $known++; |
| 164 | } |
| 165 | } elsif ($1 eq 'pos' || $1 eq "ctag") { |
| 166 | $unknown++; |
| 167 | $conll[$XPOS_idx] = $conll[$UPOS_idx] = $2; |
| 168 | } elsif ($1 eq 'msd') { |
| 169 | $conll[$FEATS_idx] = $2; |
| 170 | } elsif ($1 eq 'certainty') { |
| 171 | $conll[$MISC_idx] = $2; |
| 172 | } |
| 173 | } elsif (/<span /) { |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 174 | my $last_from = $current_from // -1; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 175 | ($current_id) = /id="[^0-9]*([^\"]*)"/; |
| 176 | ($current_from) = /from="([^\"]*)"/; |
| 177 | ($current_to) = /to="([^\"]*)"/; |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 178 | if($extract_attributes_regex) { |
| 179 | for (my $i = $last_from + 1; $i <= $current_from; $i++) { |
| 180 | if ($extras{$docid}{$i}) { |
| 181 | $current .= $extras{$docid}{$i}; |
| 182 | undef $extras{$docid}{$i}; |
| 183 | } |
| 184 | } |
| 185 | } |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 186 | # $log->debug("found span: $current_id $current_from $current_to"); |
Marc Kupietz | 7e71a82 | 2020-06-22 17:14:30 +0200 | [diff] [blame] | 187 | $token = substr($plain_texts{$docid}, $current_from, $current_to - $current_from); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 188 | if (!defined $token) { |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 189 | $log->warn("could not retrieve token for $docid at $current_from-$current_to/", length($plain_texts{$docid}), " - ending with: ", substr($plain_texts{$docid},length($plain_texts{$docid})-10)); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 190 | $token = "_"; |
| 191 | } |
| 192 | $token=~s/[\t\n\r]//g; # make sure that tokens never contain tabs or newlines |
| 193 | @conll = ("_") x 10; |
| 194 | $conll[$FORM_idx] = encode("utf-8", $token); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 195 | if($baseOnly) { |
| 196 | my @vals = ($current_from, $current_to); |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 197 | # $log->debug("joining : ", join(" ", @vals)); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 198 | push @current_lines, \@vals; |
| 199 | $known++; |
| 200 | $conll[$ID_idx] = $#current_lines+1; |
Marc Kupietz | d7d5d6a | 2021-10-11 17:52:58 +0200 | [diff] [blame] | 201 | if ($columns == 1) { |
| 202 | $current .= "$conll[1]\n"; |
| 203 | } else { |
| 204 | $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns |
| 205 | } |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 206 | fetch_plaintext($docid); |
| 207 | if ($sentence_ends{$docid}{$current_to}) { |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 208 | $log->debug("Using sentence end for $docid \@$current_to"); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 209 | $current .= "\n"; |
| 210 | printTokenRanges(); |
| 211 | print STDOUT $current; |
| 212 | $current = ""; |
| 213 | $known = 0; |
| 214 | $unknown = 0; |
| 215 | @current_lines = (); |
| 216 | } |
| 217 | } |
Marc Kupietz | 30c41b1 | 2020-09-22 14:32:34 +0200 | [diff] [blame] | 218 | } elsif (m@^\s*</fs>@) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 219 | my @vals = ($current_from, $current_to); |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 220 | # $log->debug("joining : ", join(" ", @vals)); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 221 | push @current_lines, \@vals; |
| 222 | # convert gathered information to CONLL |
| 223 | $conll[$ID_idx] = $#current_lines+1; |
Marc Kupietz | d7d5d6a | 2021-10-11 17:52:58 +0200 | [diff] [blame] | 224 | if ($columns == 1) { |
| 225 | $current .= "$conll[1]\n"; |
| 226 | } else { |
| 227 | $current .= join("\t", @conll[0..$columns-1]) . "\n"; # conll columns |
| 228 | } |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 229 | if($sentence_ends{$docid}{$current_to} || ($s_bounds_from_morpho && $conll[$XPOS_idx] eq '$.' || ($conll[$XPOS_idx] eq 'SENT' && $token eq '.')) || $known + $unknown >= $MAX_SENTENCE_LENGTH) { |
| 230 | $log->debug("Using sentence end for $docid \@$current_to"); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 231 | $current .= "\n"; |
| 232 | if($known + $unknown > 0) { # only print sentence if it contains some words |
| 233 | printTokenRanges(); |
| 234 | print STDOUT $current; |
| 235 | } |
| 236 | $current=""; $known=0; $unknown=0; |
| 237 | @current_lines = (); |
| 238 | } |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 239 | while (<MORPHO_OR_TOKENPIPE>) { |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 240 | last if (m@</span>@); # only consider first interpretation |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | $current .= "\n"; |
| 245 | closeDoc(1); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 246 | close(MORPHO_OR_TOKENPIPE); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 247 | close(PLAINTEXTPIPE); |
| 248 | } |
| 249 | exit; |
| 250 | |
| 251 | sub printTokenRanges { |
| 252 | print "$COMMENT_START start_offsets = ", $current_lines[0]->[0]; |
| 253 | foreach my $t (@current_lines) { |
| 254 | print STDOUT " $t->[0]"; |
| 255 | } |
Marc Kupietz | a2680b9 | 2021-10-11 17:24:28 +0200 | [diff] [blame] | 256 | print "$COMMENT_END\n$COMMENT_START end_offsets = ", $current_lines[$#current_lines]->[1]; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 257 | foreach my $t (@current_lines) { |
| 258 | print STDOUT " $t->[1]"; |
| 259 | } |
Marc Kupietz | a2680b9 | 2021-10-11 17:24:28 +0200 | [diff] [blame] | 260 | print "$COMMENT_END\n"; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | sub closeDoc { |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 264 | $log->debug("closing doc"); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 265 | if($known + $unknown > 0) { # only parse a sentence if it has some words |
| 266 | chomp $current; |
| 267 | chomp $current; |
| 268 | chomp $current; |
| 269 | $current .= "\n\n"; |
| 270 | printTokenRanges(); |
| 271 | print STDOUT $current; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | # read data.xml to figure out the tokens |
| 276 | # (ideally tokens should also be in in morpho.xml, but they are not) |
| 277 | sub fetch_plaintext { |
| 278 | my ($target_id) = @_; |
| 279 | my $docid; |
| 280 | my $text_started=0; |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 281 | my $text_count = 0; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 282 | my ($current_id, $current_from, $current_to); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 283 | |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 284 | if($plain_texts{$target_id} && ($s_bounds_from_morpho || $sentence_ends{$target_id})) { |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 285 | $log->debug("Already got $target_id"); |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 286 | return 1; |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 287 | } |
| 288 | while(<PLAINTEXTPIPE>) { |
| 289 | if(/<raw_text[^>]+docid="([^"]*)/) { |
| 290 | $docid=$1; |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 291 | $log->debug("Getting plain text for $docid"); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 292 | $text_started=0; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 293 | } elsif(/<layer[^>]+docid="([^"]*)/) { |
| 294 | $docid=$1; |
Marc Kupietz | d845583 | 2021-02-11 17:30:29 +0100 | [diff] [blame] | 295 | } elsif(m@<span @) { |
| 296 | ($current_id) = /id="[^0-9]*([^\"]*)"/; |
| 297 | ($current_from) = /from="([^\"]*)"/; |
| 298 | ($current_to) = /to="([^\"]*)"/; |
| 299 | } elsif(m@<f\s[^>]*>s</f>@) { |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 300 | if ($s_bounds_from_morpho) { |
| 301 | $log->debug("Ignoring sentence end for $docid \@$current_to because of --s-bounds-from-morpho"); |
| 302 | } else { |
| 303 | $log->debug("Found sentence end for $docid \@$current_to"); |
| 304 | $sentence_ends{$docid}{$current_to} = 1; |
| 305 | } |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 306 | } elsif($extract_attributes_regex && m@<f\sname="name"[^>]*>([^<]+)</f>@) { |
| 307 | my $current_element = $1; |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 308 | $log->debug("Looking for matching attributes in $docid"); |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 309 | while(<PLAINTEXTPIPE>) { |
| 310 | last if(m@</fs>@); |
| 311 | if(m@<f\sname="([^"]+)"[^>]*>([^<]+)</f>@) { |
| 312 | my $current_node = "$current_element/$1"; |
| 313 | my $value = $2; |
| 314 | if ($current_node =~ /$extract_attributes_regex/) { |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 315 | $log->debug("Found matching attribute: $docid - $current_node = $value"); |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 316 | $extras{$docid}{$current_from} .= "# $current_node = $value\n"; |
| 317 | } |
| 318 | } |
| 319 | } |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 320 | } elsif (m@<text>(.*)</text>@) { |
| 321 | $_= decode("utf-8", $1, Encode::FB_DEFAULT); |
| 322 | s/</</go; |
| 323 | s/>/>/go; |
| 324 | s/&/&/go; |
| 325 | tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/; |
| 326 | $plain_texts{$docid} = $_; |
Marc Kupietz | 093b21c | 2021-07-31 23:39:51 +0200 | [diff] [blame] | 327 | last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id})); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 328 | } elsif (m@<text>(.*)@) { |
| 329 | $_= decode("utf-8", $1, Encode::FB_DEFAULT); |
| 330 | s/</</go; |
| 331 | s/>/>/go; |
| 332 | s/&/&/go; |
| 333 | tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/; |
| 334 | $plain_texts{$docid} = "$_ "; |
| 335 | $text_started=1; |
| 336 | } elsif ($text_started && m@(.*)</text>@) { |
| 337 | $_= decode("utf-8", $1, Encode::FB_DEFAULT); |
| 338 | s/</</go; |
| 339 | s/>/>/go; |
| 340 | s/&/&/go; |
| 341 | tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/; |
| 342 | $plain_texts{$docid} .= $_; |
| 343 | $text_started=0; |
Marc Kupietz | 093b21c | 2021-07-31 23:39:51 +0200 | [diff] [blame] | 344 | last if(!$extract_attributes_regex && ($text_count++ > 1 && $plain_texts{$target_id})); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 345 | } elsif ($text_started) { |
| 346 | chomp; |
| 347 | $_ = decode("utf-8", $_, Encode::FB_DEFAULT) . ' '; |
| 348 | s/</</go; |
| 349 | s/>/>/go; |
| 350 | s/&/&/go; |
| 351 | tr/…•⋅»«ˮ“”„›‹ʼ‘’‚′‐‑‒–—―⁓⁻₋−﹣-/...""""""'''''''-/; |
| 352 | $plain_texts{$docid} .= $_; |
| 353 | } |
| 354 | } |
Marc Kupietz | 1db65e5 | 2021-07-31 23:38:07 +0200 | [diff] [blame] | 355 | $log->debug("Got plain text for $docid"); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 356 | if(defined($ENV{PLAINTEXTFILTER})) { |
| 357 | if ($plain_texts{$docid} !~ $ENV{PLAINTEXTFILTER}) { |
| 358 | $plain_texts{$docid} = undef; |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 359 | $log->info("Skipping $docid"); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 360 | return(undef); |
| 361 | } else { |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 362 | $log->debug("Using $docid"); |
Marc Kupietz | 5e7f20a | 2020-02-17 18:17:11 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | return(1); |
| 366 | } |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 367 | |
| 368 | =pod |
| 369 | |
| 370 | =encoding utf8 |
| 371 | |
| 372 | =head1 NAME |
| 373 | |
| 374 | korapxml2conllu - Conversion of KorAP-XML zips to CoNLL-U |
| 375 | |
| 376 | =head1 SYNOPSIS |
| 377 | |
| 378 | korapxml2conllu zca15.tree_tagger.zip > zca15.conllu |
| 379 | |
| 380 | =head1 DESCRIPTION |
| 381 | |
| 382 | C<korapxml2conllu> is a script to Convert L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml> base or morpho zips to CoNLL(-U) format with all information necessary |
| 383 | for reconstruction in comment lines. |
| 384 | |
| 385 | =head1 INSTALLATION |
| 386 | |
| 387 | $ cpanm https://github.com/KorAP/KorAP-XML-CoNLL-U.git |
| 388 | |
| 389 | =head1 OPTIONS |
| 390 | |
| 391 | =over 2 |
| 392 | |
| 393 | =item B<--sigle-pattern|-p> |
| 394 | |
| 395 | Convert only texts from the KorAP XML zip files with folder names (i.e. sigles) matching the glob pattern. |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 396 | |
| 397 | =item B<--extract-attribute-pattern|-e> |
| 398 | |
| 399 | Extract element/attribute regular expressions to comments. |
| 400 | |
Marc Kupietz | d7d5d6a | 2021-10-11 17:52:58 +0200 | [diff] [blame] | 401 | =item B<--columns>=I<int> | B<-c> I<int> |
| 402 | |
| 403 | Print n columns (default: 10). If n=1, only the token itself is printed. |
| 404 | |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 405 | =item B<--s-bounds-from-morpho> |
| 406 | |
Marc Kupietz | 8eb468e | 2022-06-29 11:16:42 +0200 | [diff] [blame^] | 407 | Get sentence boundary information from tagger output rather than from s annotation in structure.xml files. |
Marc Kupietz | 15c84fd | 2021-10-12 12:20:27 +0200 | [diff] [blame] | 408 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 409 | =item B<--help|-h> |
| 410 | |
| 411 | Print help information. |
| 412 | |
| 413 | =item B<--version|-v> |
| 414 | |
| 415 | Print version information. |
| 416 | |
| 417 | |
| 418 | =item B<--log|-l> |
| 419 | |
| 420 | Loglevel for I<Log::Any>. Defaults to C<warn>. |
| 421 | |
| 422 | =back |
| 423 | |
| 424 | =head1 EXAMPLES |
Marc Kupietz | eb7d06a | 2021-03-19 16:29:16 +0100 | [diff] [blame] | 425 | |
| 426 | korapxml2conllu -e '(posting/id|div/id)' t/data/wdf19.zip |
| 427 | |
Marc Kupietz | 6a79cad | 2021-03-19 16:26:58 +0100 | [diff] [blame] | 428 | =head1 COPYRIGHT AND LICENSE |
| 429 | |
| 430 | Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/> |
| 431 | |
| 432 | Author: Marc Kupietz |
| 433 | |
| 434 | Contributors: Nils Diewald |
| 435 | |
| 436 | L<KorAP::XML::CoNNL-U> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/> |
| 437 | Corpus Analysis Platform at the |
| 438 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 439 | member of the |
| 440 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. |
| 441 | |
| 442 | This program is free software published under the |
| 443 | L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>. |