File Coverage

blib/lib/Treex/Tool/Parser/MSTperl/Writer.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 Treex::Tool::Parser::MSTperl::Writer;
2             {
3             $Treex::Tool::Parser::MSTperl::Writer::VERSION = '0.11949';
4             }
5              
6 1     1   2386 use Moose;
  0            
  0            
7             use autodie;
8             use Carp;
9              
10             has config => (
11             isa => 'Treex::Tool::Parser::MSTperl::Config',
12             is => 'ro',
13             required => '1',
14             );
15              
16             sub write_tsv {
17              
18             # (Str $filename,
19             # ArrayRef[Treex::Tool::Parser::MSTperl::Sentence] $sentences)
20             my ( $self, $filename, $sentences ) = @_;
21              
22             open my $file, '>:encoding(utf8)', $filename;
23             foreach my $sentence ( @{$sentences} ) {
24             foreach my $node ( @{ $sentence->nodes } ) {
25             my @line = @{ $node->fields };
26              
27             # the parent_ord field contains -2 -> fill it with actual value
28             # which is stored in $node->parentOrd
29             $line[ $self->config->parent_ord_field_index ] =
30             $node->parentOrd;
31              
32             # the label field contains '_' -> fill it with actual value
33             # which is stored in $node->label
34             my $label_field_index = $self->config->label_field_index;
35             if ( defined $label_field_index ) {
36             $line[$label_field_index] = $node->label;
37             }
38              
39             print $file join "\t", @line;
40             print $file "\n";
41             }
42             print $file "\n";
43             }
44             close $file;
45              
46             if ( -e $filename ) {
47             return 1;
48             } else {
49             croak "MSTperl parser error:"
50             . "unable to create the output file '$filename'!";
51             }
52             }
53              
54             1;
55              
56             __END__
57              
58             =pod
59              
60             =for Pod::Coverage BUILD
61              
62             =encoding utf-8
63              
64             =head1 NAME
65              
66             Treex::Tool::Parser::MSTperl::Writer
67              
68             =head1 VERSION
69              
70             version 0.11949
71              
72             =head1 DESCRIPTION
73              
74             Writes L<Treex::Tool::Parser::MSTperl::Sentence> instances
75             to a CoNLL-like TSV file
76             (one line corresponds to one node, its features separated by tabs,
77             sentence boundary is represented by an empty line).
78              
79             =head1 METHODS
80              
81             =over 4
82              
83             =item $reader->write_tsv($filename, $sentences)
84              
85             Takes a reference to an array of sentences C<$sentences>
86             (instances of L<Treex::Tool::Parser::MSTperl::Sentence>)
87             and writes them to file C<$filename>.
88              
89             The structure of the file (the order of the fields)
90             is determined by the C<config> field
91             (instance of L<Treex::Tool::Parser::MSTperl::Config>),
92             specifically by the C<field_names> setting.
93              
94             =back
95              
96             =head1 AUTHORS
97              
98             Rudolf Rosa <rosa@ufal.mff.cuni.cz>
99              
100             =head1 COPYRIGHT AND LICENSE
101              
102             Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles
103             University in Prague
104              
105             This module is free software; you can redistribute it and/or modify it under
106             the same terms as Perl itself.