summaryrefslogtreecommitdiff
path: root/corpus/cut-corpus.pl
blob: 0af3b23ca4eb38e67d51ce51d0f685af7ba1506a (plain)
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
#!/usr/bin/perl -w
use strict;
die "Usage: $0 N\nSplits a corpus separated by ||| symbols and returns the Nth field\n" unless scalar @ARGV > 0;

my $x = shift @ARGV;
my @ind = split /,/, $x;
my @o = ();
for my $ff (@ind) {
  if ($ff =~ /^\d+$/) {
    push @o, $ff - 1;
  } elsif ($ff =~ /^(\d+)-(\d+)$/) {
    my $a = $1;
    my $b = $2;
    die "$a-$b is a bad range in input: $x\n" unless $b > $a;
    for (my $i=$a; $i <= $b; $i++) {
      push @o, $i - 1;
    }
  } else {
    die "Bad input: $x\n";
  }
}

while(<>) {
  chomp;
  my @fields = split /\s*\|\|\|\s*/;
  my @sf;
  for my $i (@o) {
    my $y = $fields[$i];
    if (!defined $y) { $y= ''; }
    push @sf, $y;
  }
  print join(' ||| ', @sf) . "\n";
}