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