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