File Coverage

blib/lib/CWB/CQP/More/Parallel.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package CWB::CQP::More::Parallel;
2             $CWB::CQP::More::Parallel::VERSION = '0.08';
3 1     1   1065 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         21  
5 1     1   4 use warnings;
  1         3  
  1         30  
6              
7 1     1   5 use base 'CWB::CQP::More';
  1         3  
  1         131  
8             use Try::Tiny;
9              
10             =encoding utf-8
11              
12             =head1 NAME
13              
14             CWB::CQP::More::Parallel - CWP::CQP::More tweaked for parallel corpora
15              
16             =head1 SYNOPSIS
17              
18             use CWB::CQP::More::Parallel;
19              
20             my $cwb = CWB::CQP::More::Parallel->new( { utf8 => 1} );
21              
22              
23             =head1 DESCRIPTION
24              
25             CWB::CQP::More prepared for parallel corpora.
26              
27             =cut
28              
29             sub new {
30             shift; # class
31             my $ops = ref $_[0] eq "HASH" ? shift : {};
32             $ops->{parallel} = 1;
33             __PACKAGE__->SUPER::new($ops, @_);
34             }
35              
36             =head2 change_corpus
37              
38             Change current active parallel corpus. Pass the corpus name as the
39             first argument, and the target corpus as second argument. If this one
40             is ommited, the target language will be automatically selected (can
41             misbehave for multilanguage corpora).
42              
43             =cut
44              
45             sub change_corpus {
46             my ($self, $cname, $tname) = @_;
47              
48             my $details = $self->corpora_details($cname);
49             die "Can not find details for corpus $cname." unless defined $details;
50              
51             my $target;
52             if (defined($tname)) {
53             if (exists($details->{attribute}{a}{lc $tname})) {
54             $target = lc $tname;
55             } elsif (exists($details->{attribute}{a}{uc $tname})) {
56             $target = uc $tname;
57             } else {
58             die "Can't find an aligned corpus named $tname";
59             }
60             } else {
61             ($target) = keys %{ $details->{attribute}{a} };
62             }
63              
64             die "This does not seems a parallel corpus." unless defined $target;
65              
66             $cname = uc $cname;
67             $self->exec("$cname;");
68             $self->annotation_show($target);
69             }
70              
71              
72             =head2 cat
73              
74             This method uses the C method to return a result set. The first
75             mandatory argument is the name of the result set.
76              
77             B Second and Third
78             arguments are optional, and correspond to the interval of matches to
79             return.
80              
81             Returns empty list on any error.
82             On success returns list of pairs.
83              
84             =cut
85              
86             sub cat {
87             my ($self, $id, $from, $to) = @_; # , $from, $to) = @_;
88             my $extra = "";
89             $extra = "$from $to" if defined($from) && defined($to);
90             my @ans;
91             try {
92             @ans = $self->exec("cat $id $extra;");
93             } catch {
94             @ans = ();
95             };
96              
97             my @fans;
98             while (@ans) {
99             my $left = shift @ans;
100             my $right = shift @ans;
101             push @fans, [$left,$right];
102             }
103              
104             return @fans;
105             }
106              
107             =head1 SEE ALSO
108              
109             CWB::CQP::More
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             Copyright (C) 2014 by Alberto Manuel Brandão Simões
114              
115             This library is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself, either Perl version 5.8.2 or,
117             at your option, any later version of Perl 5 you may have available.
118              
119             =cut
120              
121              
122              
123             1;
124             __END__