~jtv/corpusfiltergraph/cross-python

« back to all changes in this revision

Viewing changes to trunk/lib/corpusfg/graphs/sa-champollion/bin/merge_alignment.pl

  • Committer: tahoar
  • Date: 2012-05-02 15:46:23 UTC
  • Revision ID: svn-v4:bc069b21-dff4-4e29-a776-06a4e04bad4e::266
new layout. need to update code to use the new layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
#
 
3
# Author: Xiaoyi Ma at the LDC, 06/06/2003
 
4
# Purpose: given sentences of both sides and an alignment file
 
5
#          merge_alignment.pl merge two sides together and 
 
6
#          print an easy-to-read output
 
7
# Usage: merge_alignment.pl [hod] X_sentence_file Y_sentence_file aligment_result
 
8
#        X_sentence_file: files contains all X sentences, indicated by 
 
9
#           <seg id=###> </seg>
 
10
#        the seg ids should be sequential numbers, starting from one.
 
11
#        Y_sentence_file: files contains all Y sentences, indicated by
 
12
#           <seg id=###> </seg>
 
13
#        the seg ids should be sequential numbers, starting from one.
 
14
#        alignment_result: alignment file, one alignment per line
 
15
$| = 1; # disable Perl output buffering
 
16
 
 
17
use Getopt::Std;
 
18
 
 
19
getopts('hod', \%opts) || usage();
 
20
usage() if $opts{h};
 
21
 
 
22
$printomission = $opts{o};
 
23
$debug = $opts{d};
 
24
usage() if @ARGV != 3;
 
25
 
 
26
($efn, $cfn, $align) = @ARGV;
 
27
 
 
28
 
 
29
open E, "<$efn" or die "$0: can not open $efn\n";
 
30
open C, "<$cfn" or die "$0: can not open $cfn\n";
 
31
open A, "<$align" or die "$0: can not open $align\n";
 
32
 
 
33
$docid = `basename $cfn`;
 
34
$docid =~ s/\..+//;
 
35
chomp $docid;
 
36
 
 
37
while(<E>) {
 
38
    chomp;
 
39
    if (/<seg id=(\d+)>(.*)<\/seg>/) {
 
40
        $ln = $1;
 
41
        $es = $2;
 
42
        $eline{$ln} = $es;
 
43
    }
 
44
}
 
45
 
 
46
while(<C>) {
 
47
    chomp;
 
48
    if (/<seg id=(\d+)>(.*)<\/seg>/) {
 
49
        $ln = $1;
 
50
        $cs = $2;
 
51
        $cline{$ln} = $cs;
 
52
    }
 
53
}
 
54
 
 
55
 
 
56
print "<DOC docid=$docid>\n";
 
57
 
 
58
while(<A>){
 
59
    chomp;
 
60
    next unless / <=> /;
 
61
    unless ($printomission) {
 
62
        next if /omitted/;
 
63
    }
 
64
    
 
65
    /(.+) <=> (.+)/;
 
66
    $esent = $1; $csent = $2;
 
67
 
 
68
    if ($debug) {
 
69
        print "\n$_\n";
 
70
    }
 
71
 
 
72
    if ($esent =~ /omitted/) {
 
73
        $etype = "0";
 
74
        undef @esent;
 
75
    } else {
 
76
        @esent = split /,/, $esent;
 
77
        $etype = @esent;
 
78
    }
 
79
 
 
80
    if ($csent =~ /omitted/) {
 
81
        $ctype = "0";
 
82
        undef @csent;
 
83
    } else {
 
84
        @csent = split /,/, $csent;
 
85
        $ctype = @csent;
 
86
    }
 
87
 
 
88
    print "<SENT type=$etype-$ctype>\n";
 
89
 
 
90
    foreach (@esent) {
 
91
        print $eline{$_}," ";
 
92
    }
 
93
    print "\n";
 
94
 
 
95
    foreach (@csent) {
 
96
        print $cline{$_};
 
97
    }
 
98
    print "\n</SENT>\n";
 
99
 
 
100
}
 
101
 
 
102
print "</DOC>\n";
 
103
exit;
 
104
 
 
105
sub usage() {
 
106
    print STDERR << "EOF";
 
107
usage: $0 [-hod] <X file> <Y file> <alignment file>
 
108
      
 
109
      -h  : this (help) message
 
110
      -o  : print deletion and insertion (default is no).
 
111
      -d  : debug mode, prints alignment as well. (default is no)
 
112
 
 
113
EOF
 
114
 
 
115
       exit;
 
116
}