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