File Coverage

blib/lib/XML/XCES.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package XML::XCES;
2              
3 1     1   77358 use XML::DT;
  0            
  0            
4              
5             use warnings;
6             no warnings 'recursion';
7             use strict;
8              
9             =head1 NAME
10              
11             XML::XCES - Perl module to handle XCES xml files
12              
13             =head1 VERSION
14              
15             Version 0.01
16              
17             =cut
18              
19             our $VERSION = '0.01';
20              
21             =head1 SYNOPSIS
22              
23             use XML::XCES;
24              
25             XML::XCES->align2pair("File.xml", "prefix");
26              
27             =head1 FUNCTIONS
28              
29             XML::XCES provides the following functions:
30              
31             =head2 align2pair
32              
33             This function receives an XCES xml filename with sentence alignment
34             content, and, optionally, a prefix for the output files.
35              
36             Note that the aligned files paths should be absolute or relative to
37             the command issue directory.
38              
39             =cut
40              
41             sub align2pair {
42             shift if ($_[0] eq "XML::XCES");
43             my $xces = shift;
44             my $prefix = shift || $xces;
45              
46             my $tuCount = 0;
47              
48             open S, ">$prefix-source.nat" or die;
49             open T, ">$prefix-target.nat" or die;
50              
51             my %handler = (
52             -type => { linkGrp => 'SEQ' },
53              
54             'link' => sub {
55             my ($s, $t) = split /\s*;\s*/, $v{xtargets};
56             my @s = grep { /./ } split /\s+/, $s;
57             my @t = grep { /./ } split /\s+/, $t;
58             return [[@s],[@t],$v{certainty}];
59             },
60              
61             'linkGrp' => sub {
62             my ($source,$target) = ($v{fromDoc},$v{toDoc});
63             return unless -f $source and -f $target;
64              
65             my $cont = $c;
66             printf STDERR "+ %s * %s ", _last26($source), _last26($target);
67              
68             my (%s,%t);
69             my $ACTIVE;
70             my %h2 = (
71             -type => { linkGrp => 'SEQ' },
72              
73             -outputenc => 'iso-8859-1',
74              
75             -default => sub {
76             $c = _trim($c);
77             if ($v{id} && exists($ACTIVE->{$v{id}})) {
78             $ACTIVE->{$v{id}} = $c;
79             }
80             $c
81             });
82              
83             my $tu = 0;
84             for my $link (@$cont) {
85             $tu++;
86             @s{@{$link->[0]}} = 1 x @{$link->[0]};
87             @t{@{$link->[1]}} = 1 x @{$link->[1]};
88             }
89             print STDERR "($tu TUs)\n";
90             $tuCount+=$tu;
91              
92             $ACTIVE = \%s;
93             dt($source, %h2);
94              
95             $ACTIVE = \%t;
96             dt($target, %h2);
97              
98             for my $link (@$cont) {
99             print S (map { "$_\n" } (@s{@{$link->[0]}},'$'));
100             print T (map { "$_\n" } (@t{@{$link->[1]}},'$'));
101             }
102             },
103             );
104              
105             dt($xces, %handler);
106              
107             return $tuCount;
108             }
109              
110              
111             sub _trim {
112             my $x = shift;
113             $x =~ s/\s+/ /g;
114             $x =~ s/^\s+//;
115             $x =~ s/\s+$//;
116             return $x;
117             }
118              
119             sub _last26 {
120             my $x = shift;
121             if (length($x)>26) {
122             return "...".substr($x,-23,23);
123             } else {
124             return $x
125             }
126             }
127              
128              
129              
130             =head1 AUTHOR
131              
132             Alberto Simoes, C<< >>
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to
137             C, or through the web interface at
138             L. I will be notified, and then you'll automatically
139             be notified of progress on your bug as I make changes.
140              
141             =head1 ACKNOWLEDGEMENTS
142              
143             =head1 COPYRIGHT & LICENSE
144              
145             Copyright 2004-2005 Alberto Simoes, All Rights Reserved.
146              
147             This program is free software; you can redistribute it and/or modify it
148             under the same terms as Perl itself.
149              
150             =cut
151              
152             1; # End of XML::XCES