File size: 9,684 Bytes
22e4ecd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
#!/usr/bin/env perl
# Reads a plain text and a CoNLL-U file. Splits the CoNLL-U file into two so that
# the first part matches the plain text and the second part contains the rest.
# Copyright © 2020 Dan Zeman <zeman@ufal.mff.cuni.cz>
# License: GNU GPL
use utf8;
use open ':utf8';
binmode(STDIN, ':utf8');
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
use Carp; # confess() instead of die()
if(scalar(@ARGV) != 4)
{
die("4 arguments expected: 1. input text; 2. input conllu; 3. output first part; 4. output second part");
}
my $intext = shift(@ARGV);
my $inconllu = shift(@ARGV);
my $outpart1 = shift(@ARGV);
my $outpart2 = shift(@ARGV);
# To make my life easier, I am assuming that the plain text fits in the memory
# and I am going to read the entire file first.
open(TEXT, $intext) or die("Cannot read '$intext': $!");
my $text = '';
while(<TEXT>)
{
$text .= $_;
}
close(TEXT);
# We will ignore whitespace characters and only synchronize on the non-whitespace ones.
$text =~ s/\s//sg;
# Read the input CoNLL-U file and check it against the input text.
open(IN, $inconllu) or die("Cannot read '$inconllu': $!");
open(OUT, ">$outpart1") or die("Cannot write '$outpart1': $!");
my $writingpart = 1;
my $incomplete_sentence = 0;
my @sentence = ();
my $mwtuntil;
while(<IN>)
{
push(@sentence, $_);
if(m/^\d+-(\d+)\t/)
{
$mwtuntil = $1;
my @f = split(/\t/, $_);
check_word_form($f[1]) if($writingpart==1);
}
elsif(m/^(\d+)\t/)
{
my $id = $1;
if(defined($mwtuntil) && $id > $mwtuntil)
{
$mwtuntil = undef;
}
unless(defined($mwtuntil))
{
my @f = split(/\t/, $_);
check_word_form($f[1]) if($writingpart==1);
}
}
elsif(m/^\s*$/) # end of sentence
{
if($incomplete_sentence)
{
@sentence = fix_sentence(@sentence);
$incomplete_sentence = 0;
}
my $sentence = join('', @sentence);
print OUT ($sentence);
if($writingpart==1 && length($text)==0)
{
close(OUT);
open(OUT, ">$outpart2") or die("Cannot write '$outpart2': $!");
$writingpart = 2;
}
@sentence = ();
$mwtuntil = undef;
}
}
close(IN);
close(OUT);
#------------------------------------------------------------------------------
# Checks the next word form against the input text, and removes it from the
# input text. The form is a parameter, the input text is accessed directly as
# a global variable.
#------------------------------------------------------------------------------
sub check_word_form
{
my $form = shift;
# This function should be used only if the global variable $writingpart == 1.
if($writingpart != 1)
{
confess("Expected \$writingpart == 1 but it is '$writingpart'");
}
# If there are words with spaces, we ignore the spaces.
$form =~ s/\s//g;
my $nt = length($text);
my $nf = length($form);
if($nt == 0)
{
###!!! We access directly the global variable with the sentence.
###!!! This should perhaps be done better in the future.
my @sentence1 = @sentence;
# The current line contains a form that is not in the input text. Discard it.
# Replace it by the empty line that should terminate every sentence.
my $current_line = pop(@sentence1);
push(@sentence1, "\n");
@sentence1 = fix_sentence(@sentence1);
my $sentence1 = join('', @sentence1);
print OUT ($sentence1);
close(OUT);
open(OUT, ">$outpart2") or die("Cannot write '$outpart2': $!");
$writingpart = 2;
# Modify the current sentence. Keep comments but remove tokens except
# the current one.
my @sentence2 = ();
foreach my $line (@sentence)
{
if($line =~ m/^\#/)
{
push(@sentence2, $line);
}
else
{
last;
}
}
push(@sentence2, $current_line);
@sentence = @sentence2;
# Set the global flag that the current sentence is incomplete.
$incomplete_sentence = 1;
return 0;
}
elsif($nf > $nt)
{
###!!!
# We currently do not support breaking in the middle of a token.
confess("Input text ends in the middle of a token, which is not supported at present");
}
my $prefix = substr($text, 0, $nf);
$text = substr($text, $nf);
if($prefix ne $form)
{
confess("Word form in CoNLL-U '$form' does not match the input text '$prefix'");
}
return 1;
}
#------------------------------------------------------------------------------
# Modifies an incomplete sentence so that it can be printed as a valid CoNLL-U.
#------------------------------------------------------------------------------
sub fix_sentence
{
my @sentence = @_;
# Collect node ids that exist in the sentence.
my @ids;
my %ids;
foreach my $line (@sentence)
{
if($line =~ m/^(\d+(\.\d+)?)\t/)
{
my $id = $1;
push(@ids, $id);
$ids{$id}++;
}
}
# Change references to non-existing head nodes to 0.
foreach my $line (@sentence)
{
if($line =~ m/^\d/)
{
my @f = split(/\t/, $line);
# The HEAD column.
if($f[6] =~ m/^\d+$/ && !exists($ids{$f[6]}))
{
$f[6] = 0;
}
# The DEPS column.
if($f[8] ne '_')
{
my @deps = split(/\|/, $f[8]);
foreach my $dep (@deps)
{
if($dep =~ m/^(\d+(?:\.\d+)?):(.+)$/)
{
my $h = $1;
my $d = $2;
if(!exists($ids{$h}))
{
$h = 0;
$dep = "$h:$d";
}
}
}
$f[8] = join('|', @deps);
}
$line = join("\t", @f);
}
}
# If the ids do not start with 1 (or 0.something), shift them.
# First determine what the mapping should be.
# Assume that the original sentence had a valid sequence of ids, i.e.,
# if the first id is 0.* or 1, no action is needed, and otherwise only
# constant subtraction is needed.
if($ids[0] != m/^(1|0\.\d+)$/)
{
my $offset = 0;
if($ids[0] =~ m/^(\d+)\.\d+$/)
{
$offset = $1;
}
else
{
$offset = $ids[0]-1;
}
foreach my $line (@sentence)
{
if($line =~ m/^\d/)
{
my @f = split(/\t/, $line);
# The ID column.
if($f[0] =~ m/^(\d+)-(\d+)$/)
{
my $from = $1;
my $to = $2;
$from -= $offset;
$to -= $offset;
$f[0] = "$from-$to";
}
else
{
$f[0] -= $offset;
}
# The HEAD column.
if($f[6] =~ m/^\d+$/ && $f[6] > $offset)
{
$f[6] -= $offset;
}
# The DEPS column.
if($f[8] ne '_')
{
my @deps = split(/\|/, $f[8]);
foreach my $dep (@deps)
{
if($dep =~ m/^(\d+(?:\.\d+)?):(.+)$/)
{
my $h = $1;
my $d = $2;
if($h > $offset)
{
$h -= $offset;
$dep = "$h:$d";
}
}
}
$f[8] = join('|', @deps);
}
$line = join("\t", @f);
}
}
}
# Fix the sentence text attribute.
my $sentence_text = '';
my $mwtuntil;
foreach my $line (@sentence)
{
if($line =~ m/^\d+-(\d+)\t/)
{
$mwtuntil = $1;
my @f = split(/\t/, $line);
$sentence_text .= $f[1];
$sentence_text .= ' ' unless(is_no_space_after($f[9]));
}
elsif($line =~ m/^(\d+)\t/)
{
my $id = $1;
if(defined($mwtuntil) && $id > $mwtuntil)
{
$mwtuntil = undef;
}
unless(defined($mwtuntil))
{
my @f = split(/\t/, $line);
$sentence_text .= $f[1];
$sentence_text .= ' ' unless(is_no_space_after($f[9]));
}
}
}
$sentence_text =~ s/\s+$//;
foreach my $line (@sentence)
{
if($line =~ m/^\#\s*text\s*=\s*.+/)
{
$line = "\# text = $sentence_text\n";
}
}
return @sentence;
}
#------------------------------------------------------------------------------
# Checks whether the MISC column contains SpaceAfter=No.
#------------------------------------------------------------------------------
sub is_no_space_after
{
my $misc = shift;
# MISC is the last column and we do not know whether the end-of-line character has been removed.
$misc =~ s/\r?\n$//;
my @nospaceafter = grep {$_ eq 'SpaceAfter=No'} (split(/\|/, $misc));
return scalar(@nospaceafter) > 0;
}
|