-
Notifications
You must be signed in to change notification settings - Fork 46
/
conllu_quick_fix_id_sequence.pl
executable file
·73 lines (68 loc) · 2.2 KB
/
conllu_quick_fix_id_sequence.pl
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
#!/usr/bin/env perl
# Reads a CoNLL-U file and tries to fix errors in the node ID sequences; writes the fixed file to STDOUT.
# Usage: perl conllu-quick-fix-id-sequence.pl < input.conllu > fixed.conllu
# Copyright © 2020 Dan Zeman <[email protected]>
# License: GNU GPL
use utf8;
use open ':utf8';
binmode(STDIN, ':utf8');
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
my @sentence = ();
my $isent = 0;
while(<>)
{
push(@sentence, $_);
if(m/^\s*$/)
{
process_sentence(@sentence);
@sentence = ();
}
}
#------------------------------------------------------------------------------
# Once a sentence has been read, processes it and prints it.
#------------------------------------------------------------------------------
sub process_sentence
{
$isent++; # global counter
my @sentence = @_;
my $iword = 0;
my $ok = 1;
my %idmap; # old ID => new ID
###!!! We currently ignore the ranges of multi-word tokens, empty nodes, and the contents of the DEPS column!
foreach my $line (@sentence)
{
# Lines that start with a number correspond to nodes (words).
# The numbers must form a sequence with the increment of 1.
if($line =~ m/^\d+\t/)
{
$iword++;
my @f = split(/\t/, $line);
$idmap{$f[0]} = $iword unless(exists($idmap{$f[0]}));
if($f[0] != $iword)
{
$ok = 0;
}
}
}
# We need to act only if we encountered a wrong ID (out of place).
unless($ok)
{
$iword = 0;
foreach my $line (@sentence)
{
if($line =~ m/^\d+\t/)
{
my @f = split(/\t/, $line);
# Fix the node ID. We cannot use the hash map here because the original ids might be duplicated.
$iword++;
$f[0] = $iword;
# Fix the ID reference to the HEAD. We will use the hash map here. If there were duplicated IDs, the first of them will be the head.
$f[6] = $idmap{$f[6]} if(exists($idmap{$f[6]}));
$line = join("\t", @f);
}
}
}
# Print the fixed sentence.
print(join('', @sentence));
}