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