File size: 2,194 Bytes
b0dd4bf |
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 |
#!/usr/bin/env perl
# Merges multiple UD test files (blind text or CoNLL-U) into one.
# Makes sure that there is a document break in the resulting file at every position where one original file ended and another started.
# This will make it easier to later isolate parser outputs for individual input parts.
# 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');
if(scalar(@ARGV) < 2)
{
print STDERR ("Usage: perl mergetest.pl nl.txt UD_Dutch-Alpino/nl_alpino-ud-test.txt UD_Dutch-LassySmall/nl_lassysmall-ud-test.txt\n");
die("Expected at least two arguments: the target file and at least one source file");
}
my $outfile = shift(@ARGV);
# Trigger mode CoNLL-U if the target file name ends in ".conllu"; assume text mode otherwise.
my $conllu = $outfile =~ m/\.conllu$/;
open(OUT, ">$outfile") or die("Cannot write '$outfile': $!");
my $n = scalar(@ARGV);
my $i = 0;
foreach my $infile (@ARGV)
{
if($conllu)
{
# We do not know whether the input file does or does not use the "newdoc" flags; they are optional.
# If there is a "newdoc" before the first sentence, we do not want to add our own.
# If it is not there, we want to add it.
# Hence we must first read the first few lines to see the situation, then act accordingly.
my $hasnewdoc = 0;
open(IN, $infile) or die("Cannot read '$infile': $!");
while(<IN>)
{
if(m/^\#\s*newdoc( |$)/)
{
$hasnewdoc = 1;
last;
}
elsif(m/^\d/)
{
# First token line and still no newdoc found? Stop it.
last;
}
}
close(IN);
unless($hasnewdoc)
{
print OUT ("\# newdoc\n");
}
}
open(IN, $infile) or die("Cannot read '$infile': $!");
while(<IN>)
{
print OUT;
}
close(IN);
# In text mode, print an empty line after each file except the last one.
$i++;
if(!$conllu && $i < $n)
{
print OUT ("\n");
}
}
close(OUT);
|