Parse/Simple.pm

filter: Statementstmt 93.0% 94 / 101 Branchbran 80.0% 32 / 40 Conditioncond 75.0% 15 / 20 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 86.1% 161 / 187 SCAR 35.3
CC
32
Cov
88%
parse_fh
13.2
parse_string
13.0
parse_file
5.9
CRAP 34.0
« Lookup.pm ↑ Summary Sense.pm »
1 package WordLists::Parse::Simple;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use WordLists::Common qw (@sDefaultAttList @sDefiningAttlist);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use WordLists::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 our $VERSION = $WordLists::Base::VERSION;
8
9 7 my $canUseFileBOM=0;
10 7 eval { require File::BOM; File::BOM->import(); };
11 7 ▶unless ($@)
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 7 7
12 {
13 0 $canUseFileBOM = 1;
14 }
15
16 sub parse_string
17 {
18 31 ▶ my ($self, $string, $args) = @_;
Subroutine parse_string: called CC=13 SCAR=25.7
Pod documented
19 31 foreach (grep {defined $self->{$_};} qw(attlist field_sep header_marker))
20 {
21 91 ▶ $args->{$_} = $self->{$_} unless defined $args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$_} 28 63 91
22 }
23 31 ▶ $args->{'line_sep'} = $self->_get_line_sep unless defined $args->{'line_sep'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"line_sep"} 7 24 31
24 31 my @sAttList;
25 31 ▶ @sAttList = ($args->{'attlist'} ? @{$args->{'attlist'}} : @{$self->{'default_attlist'}});
Branch: 1 branch
BranchTrueFalseTotal
$args->{'attlist'} ? : 29 2 31
26 31 my $LS = $args->{'line_sep'};
27 31 my @sLines = split (/$LS/,$string);
28 31 my @senseList;
29 31 foreach my $sLine (grep {m/\w/} @sLines) # todo: make this condition changeable
30 {
31
32 36 chomp $sLine;
33 36 my $FS = $args->{'field_sep'};
34 36 my @sCols = split (/$FS/, $sLine);
35 36 ▶ if ($args->{'is_header'} or (!defined ($args->{'is_header'}) and $sLine =~ m/^$args->{'header_marker'}/))
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'is_header'} or not defined $args->{'is_header'} and $sLine =~ /^$args->{'header_marker'}/) { } 9 27 36
Condition100%6/6
$args->{"is_header"} or not defined $args->{"is_header"} and $sLine =~ /^$args->{"header_marker"}/
l!l&&r!l&&!r
2727
not defined $args->{"is_header"} and $sLine =~ /^$args->{"header_marker"}/
!ll&&!rl&&r
2257
MC/DC100%3/3
$args->{"is_header"} or not defined $args->{"is_header"} and $sLine =~ /^$args->{"header_marker"}/
$args->{"is_header"} not defined $args->{"is_header"} $sLine =~ /^$args->{"header_marker"}/
Truth table100%4/4
A or not(B) and C
A= $args->{"is_header"}B= not defined $args->{"is_header"}C= $sLine =~ /^$args->{"header_marker"}/
ABCresult
1XX1
0111
00X0
0100
36 {
37 9 ▶ $sCols[0] =~s/^$args->{'header_marker'}// unless $args->{'is_header'};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"is_header"} 7 2 9
38 9 @sAttList = @sCols;
39 9 @{$self->{'attlist'}} = @sCols;
40 9 @{$args->{'attlist'}} = @sCols;
41 }
42 else
43 {
44 27 my %sAttr = ();
45 27 foreach (0..$#sAttList )
46 {
47 71 ▶ $sAttr{$sAttList[$_]} = $sCols[$_] if $sAttList[$_] =~ m/^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $sAttList[$_] =~ /^\w+$/ 71 0 71
48 71 ▶ if ($sAttList[$_] =~ m/^(\w+)\[(\d+)\]$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($sAttList[$_] =~ /^(\w+)\[(\d+)\]$/) 0 71 71
49 {
50 0 $sAttr{$1}[$2] = $sCols[$_];
51 }
52 }
53 27 push @senseList, \%sAttr;
54 }
55 }
56 31 return \@senseList;
57 }
58 sub new
59 {
60 4 ▶ my ($class, $args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
61 4 my $self = {
62 'field_sep' => "\t",
63 'default_attlist'=> [@sDefaultAttList],
64 'header_marker' => quotemeta '#*',
65 'line_sep' => \$/,
66 };
67 4 $self->{$_} = $args->{$_} foreach grep { defined $args->{$_}; }(qw(field_sep attlist default_attlist header_marker line_sep));
68 4 bless $self, $class;
69 }
70
71 sub parse_file
72 {
73 3 ▶ my ($self, $fn, $enc, $args) = @_;
Subroutine parse_file: called CC=5 SCAR=17.8
Pod documented
74 3 my $fh;
75 3 my $structure = [];
76 3 ▶ if (defined $enc)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $enc) { } 1 2 3
elsif ($canUseFileBOM) { } 0 2 2
77 {
78 1 $fh = IO::File->new($fn, "<:encoding($enc)");
79 }
80 elsif ($canUseFileBOM)
81 {
82 0 $fh = IO::File->new($fn, "<:via(File::BOM)");
83 }
84 else
85 {
86 2 $fh = IO::File->new($fn, "<");
87 }
88 3 ▶ if (defined $fh)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 3 0 3
89 {
90 3 $structure= $self->parse_fh($fh, $args);
91 3 undef $fh;
92 }
93 else
94 {
95 0 ▶ $enc ||= 'undefined';
Condition0%0/2
$enc ||= "undefined"
l!l
00
MC/DC0%0/1
$enc ||= "undefined"
$enc
Truth table0%0/2
A
A= $enc
Aresult
11
00
96 0 warn "Open $fn with encoding $enc failed!";
97 }
98 3 return $structure;
99 }
100 sub _get_line_sep
101 {
102 7 ▶ my $self = shift;
Subroutine _get_line_sep: called CC=3 SCAR=11.2
103 7 ▶ if (ref $self->{'line_sep'} eq ref \'')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'line_sep'} eq ref \'') { } 6 1 7
elsif (not ref $self->{'line_sep'}) { } 1 0 1
104 {
105 6 return ${$self->{'line_sep'}};
106 }
107 elsif (!ref $self->{'line_sep'})
108 {
109 1 return $self->{'line_sep'};
110 }
111 else
112 {
113 0 return $/;
114 }
115 }
116 sub parse_fh
117 {
118 3 ▶ my ($self, $fh, $args) = @_;
Subroutine parse_fh: called CC=13 SCAR=25.8
Pod documented
119 3 ▶ $args = {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 3 3
120 3 $self->{'attlist'} = $self->{'default_attlist'};
121 3 my $iLine=0;
122 3 my @senses;
123 3 ▶ $args->{'header_marker'} = $self->{'header_marker'} unless defined $args->{'header_marker'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"header_marker"} 1 2 3
124 3 if (0)
125 {
126 local $/ = $/;
127 unless (ref $self->{'line_sep'} eq ref \'' and ${$self->{'line_sep'}} eq $/)
128 {
129 $/ = $self->_get_line_sep;
130 }
131 if (defined ($args->{'line_sep'}))
132 {
133 $/ = $args->{'line_sep'};
134 }
135 }
136 3 ▶ while (my $sLine = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sLine = readline $fh)) 24 3 27
137 {
138 24 ▶ if ($iLine == 0 and ($sLine=~ s/^\x{feff}// or $sLine=~ s/^\xef\xbb\xbf//))
Branch: 1 branch
BranchTrueFalseTotal
if ($iLine == 0 and $sLine =~ s/^\x{feff}// || $sLine =~ s/^\xef\xbb\xbf//) 3 21 24
Condition83%5/6
$iLine == 0 and $sLine =~ s/^\x{feff}// || $sLine =~ s/^\xef\xbb\xbf//
!ll&&!rl&&r
2112
$sLine =~ s/^\x{feff}// or $sLine =~ s/^\xef\xbb\xbf//
l!l&&r!l&&!r
120
MC/DC33%1/3
$iLine == 0 and $sLine =~ s/^\x{feff}// || $sLine =~ s/^\xef\xbb\xbf//
$iLine == 0 $sLine =~ s/^\x{feff}// $sLine =~ s/^\xef\xbb\xbf//
Truth table50%2/4
A and B or C
A= $iLine == 0B= $sLine =~ s/^\x{feff}//C= $sLine =~ s/^\xef\xbb\xbf//
ABCresult
0XX0
1000
11X1
1011
139 {
140 3 ▶ binmode $fh, ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 }
142 24 my $senses_per_line =[];
143 24 ▶ if ($args->{'header_marker'} =~ m/^\d+$/ and $iLine == $args->{'header_marker'})
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'header_marker'} =~ /^\d+$/ and $iLine == $args->{'header_marker'}) { } 2 22 24
elsif ($args->{'header_marker'} =~ /^\d+$/) { } 6 16 22
Condition100%3/3
$args->{"header_marker"} =~ /^\d+$/ and $iLine == $args->{"header_marker"}
!ll&&!rl&&r
1662
MC/DC100%2/2
$args->{"header_marker"} =~ /^\d+$/ and $iLine == $args->{"header_marker"}
$args->{"header_marker"} =~ /^\d+$/ $iLine == $args->{"header_marker"}
Truth table100%3/3
A and B
A= $args->{"header_marker"} =~ /^\d+$/B= $iLine == $args->{"header_marker"}
ABresult
0X0
100
111
144 {
145 2 $senses_per_line = $self->parse_string($sLine, {%$args, is_header=>1}); # a header
146 }
147 elsif ($args->{'header_marker'} =~ m/^\d+$/)
148 {
149 6 $senses_per_line = $self->parse_string($sLine, {%$args, is_header=>0}); # not a header
150 }
151 else
152 {
153 16 $senses_per_line = $self->parse_string($sLine, $args); # could be a header
154 }
155 24 ▶ if (defined $senses_per_line and ref $senses_per_line eq ref [])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $senses_per_line and ref $senses_per_line eq ref []) 24 0 24
Condition33%1/3
defined $senses_per_line and ref $senses_per_line eq ref []
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $senses_per_line and ref $senses_per_line eq ref []
defined $senses_per_line ref $senses_per_line eq ref []
Truth table33%1/3
A and B
A= defined $senses_per_lineB= ref $senses_per_line eq ref []
ABresult
0X0
100
111
156 {
157 24 push @senses, $_ foreach @{$senses_per_line} ;
158 }
159 24 $iLine++;
160 }
161 3 return \@senses;
162 }
163 7 1;
164
165 =pod
166
167 =head1 NAME
168
169 WordLists::Parse::Simple
170
171 =head1 SYNOPSIS
172
173 my $parser = WordLists::Parse::Simple->new;
174 my @senses = @{ $parser->parse_string('#*hw\tpos\tdef\nhead\tnoun\tnoggin') };
175
176 =head1 DESCRIPTION
177
178 This is a simple parser for CSV/TSV files. It doesn't do any quoted values or anything like that - the delimiter must simply never occur in the text.
179
180 The parser aims to return each row as a hashref where the keys are the column names. It needs to be given information about how to identify the header, as there is no standardised way of representing a header. (The default is to treat lines beginning C<#*> as headers).
181
182 If the parser is passed several rows, it will return an arrayref.
183
184 =head1 OPTIONS
185
186 On creation, a hashref may be passed with configuration options.
187
188 =head1 METHODS
189
190 =head3 parse_fh
191
192 =head3 parse_file
193
194 When the module is loaded, it checks if L<File::BOM> can be used. If it can, then it will try to use it to guess the encoding when the user does not specify it.
195
196 =head3 parse_string
197
198 =head1 BUGS
199
200 Please use the Github issues tracker.
201
202 =head1 LICENSE
203
204 Copyright 2011-2012 © Cambridge University Press. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
205
206 =cut
« Lookup.pm ↑ Summary Sense.pm »