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