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