-
Notifications
You must be signed in to change notification settings - Fork 46
/
conllu_copy_basic_to_enhanced.pl
executable file
·46 lines (44 loc) · 1.27 KB
/
conllu_copy_basic_to_enhanced.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
#!/usr/bin/env perl
# Copies basic dependencies to enhanced dependencies. If there are already
# enhanced dependencies, the basic ones are added to them, unless it is
# explicitly required that the previous enhanced graph is removed first.
# Copyright © 2018, 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 Getopt::Long;
my $replace = 0;
GetOptions
(
'replace' => \$replace
);
while(<>)
{
if(m/^\d+\t/)
{
my @f = split(/\t/, $_);
my $head = $f[6];
my $deprel = $f[7];
my $deps = $f[8];
my @deps;
if($deps ne '_' && !$replace)
{
@deps = split(/\|/, $deps);
}
my $newdep = "$head:$deprel";
unless(grep {$_ eq $newdep} (@deps))
{
push(@deps, $newdep);
my @deps2 = map {my ($h, $d); if(m/^(\d+):(.+)$/) {$h=$1; $d=$2} else {$h=$_; $d=''} [$h, $d]} (@deps);
@deps2 = sort {my $r = $a->[0] <=> $b->[0]; unless($r) {$r = $a->[1] cmp $b->[1]} $r} (@deps2);
@deps = map {join(':', @{$_})} (@deps2);
$deps = join('|', @deps);
$f[8] = $deps;
$_ = join("\t", @f);
}
}
print;
}