| 1 | |||||||||||||||||||||||||||||
| 2 | package Treex::PML::Seq; | ||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | $VERSION='2.29'; # version template | |||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Treex::PML::List; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Treex::PML::Seq::Element; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Treex::PML::Seq - sequence of PML values of various types | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | This class implements the data type 'sequence'. A sequence contains of | ||||||||||||||||||||||||||||
| 22 | zero or more elements (L<Treex::PML::Seq::Element>), each consisting of | ||||||||||||||||||||||||||||
| 23 | a name and value. The ordering of elements in a sequence may be | ||||||||||||||||||||||||||||
| 24 | constrained by a regular-expression-like pattern operating on element | ||||||||||||||||||||||||||||
| 25 | names. Validation of a sequence against this constraint pattern is not | ||||||||||||||||||||||||||||
| 26 | automatic but can be performed at any time on demand. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item Treex::PML::Seq->new (element_array_ref?, content_pattern?,$reuse?) | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | NOTE: Don't call this constructor directly, use Treex::PML::Factory->createSeq() instead! | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Create a new sequence (optionally populated with elements from a given | ||||||||||||||||||||||||||||
| 35 | array_ref). Each element should be a Treex::PML::Element::Seq object. The | ||||||||||||||||||||||||||||
| 36 | second optional argument is a regular expression constraint which can | ||||||||||||||||||||||||||||
| 37 | be stored in the object and used later for validating content (see | ||||||||||||||||||||||||||||
| 38 | validate() method below). The C<$reuse> argument is a boolean flag | ||||||||||||||||||||||||||||
| 39 | indicating whether the passed array reference can be used directly (if | ||||||||||||||||||||||||||||
| 40 | C<$reuse> is true) or copied (if C<$reuse> ise false). | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 673 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 673 | my ($class,$array,$content_pattern,$reuse) = @_; | |||||||||||||||||||||||||||
| 46 | 673 | ▶ | $array = [] unless defined($array); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 673 | return bless [Treex::PML::List->new_from_ref($array,$reuse), # a list consisting of [name,value] pairs | |||||||||||||||||||||||||||
| 48 | $content_pattern # a content_pattern constraint | ||||||||||||||||||||||||||||
| 49 | ],$class; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item $seq->elements ($name?) | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Return a list of [ name, value ] pairs representing the sequence | ||||||||||||||||||||||||||||
| 55 | elements. If the optional $name argument is given, select | ||||||||||||||||||||||||||||
| 56 | only elements whose name is $name. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 37 | ▶ | sub elements { | ||||||||||||||||||||||||||
|
Subroutine
elements: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 37 | my ($self,$name)=@_; | |||||||||||||||||||||||||||
| 62 | 37 | ▶ | if (defined $name and $name ne '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and $name ne "*"
MC/DC50%1/2
defined $name and $name ne "*"
defined $name
$name ne "*"
Truth table66%2/3
A and B
A= defined $nameB= $name ne "*"
| |||||||||||||||||||||||||||||
| 63 | 1 | return grep { $_->[0] eq $name } @{$_[0]->[0]}; | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 36 | return @{$_[0]->[0]}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item $seq->elements_list () | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Like C<elements> without a name, only this method returns directly the | ||||||||||||||||||||||||||||
| 72 | Treex::PML::List object associated with this sequence. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | sub elements_list { | ||||||||||||||||||||||||||
|
Subroutine
elements_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 4 | return $_[0]->[0]; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item $seq->content_pattern () | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Return the regular expression constraint stored in the sequence object (if any). | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub content_pattern { | ||||||||||||||||||||||||||
|
Subroutine
content_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 1 | return $_[0]->[1]; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item $seq->set_content_pattern () | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Store a regular expression constraint in the sequence object. This | ||||||||||||||||||||||||||||
| 94 | expression can be used later to validate sequence content (see | ||||||||||||||||||||||||||||
| 95 | validate() method). | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub set_content_pattern { | ||||||||||||||||||||||||||
|
Subroutine
set_content_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 1 | $_[0]->[1] = $_[1]; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item $seq->values (name?) | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | If no name is given, return a list of values of all elements of the | ||||||||||||||||||||||||||||
| 107 | sequence. If a name is given, return a list consisting of values of | ||||||||||||||||||||||||||||
| 108 | elements with the given name. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | In array context, the returned value is a list, in scalar | ||||||||||||||||||||||||||||
| 111 | context the result is a Treex::PML::List object. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 25 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 25 | my ($self,$name)=@_; | |||||||||||||||||||||||||||
| 117 | 5 | ▶ | my @values = map { $_->[1] } ((defined($name) and length($name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
MC/DC50%1/2
defined $name and length $name
defined $name
length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||
| 118 | 1 | ? (grep $_->[0] eq $name, @{$self->[0]}) | |||||||||||||||||||||||||||
| 119 | 25 | : @{$self->[0]}); | |||||||||||||||||||||||||||
| 120 | 25 | ▶ | return wantarray ? @values : bless \@values, 'Treex::PML::List'; #->new_from_ref(\@values,1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item $seq->names () | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Return a list of names of all elements of the sequence. In array | ||||||||||||||||||||||||||||
| 126 | context, the returned value is a list, in scalar context the result is | ||||||||||||||||||||||||||||
| 127 | a Treex::PML::List object. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | ▶ | sub names { | ||||||||||||||||||||||||||
|
Subroutine
names: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 5 | my @names = map { $_->[0] } $_[0][0]->values; | |||||||||||||||||||||||||||
| 133 | 5 | ▶ | return wantarray ? @names : bless \@names, 'Treex::PML::List'; #Treex::PML::List->new_from_ref(\@names,1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =item $seq->element_at (index) | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Return the element of the sequence on the position specified by a | ||||||||||||||||||||||||||||
| 139 | given index. Elements in the sequence are indexed as elements in Perl | ||||||||||||||||||||||||||||
| 140 | arrays, i.e. starting from $[, which defaults to 0 and nobody sane | ||||||||||||||||||||||||||||
| 141 | should ever want to change it. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 10 | ▶ | sub element_at { | ||||||||||||||||||||||||||
|
Subroutine
element_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 10 | my ($self, $index)=@_; | |||||||||||||||||||||||||||
| 147 | 10 | return $self->[0][$index]; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item $seq->name_at (index) | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Return the name of the element on a given position. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | ▶ | sub name_at { | ||||||||||||||||||||||||||
|
Subroutine
name_at: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 1 | my ($self, $index)=@_; | |||||||||||||||||||||||||||
| 159 | 1 | my $el = $self->[0][$index]; | |||||||||||||||||||||||||||
| 160 | 1 | ▶ | return $el->[0] if $el; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item $seq->value_at (index) | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Return the value of the element on a given position. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 3 | ▶ | sub value_at { | ||||||||||||||||||||||||||
|
Subroutine
value_at: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | 3 | my ($self, $index)=@_; | |||||||||||||||||||||||||||
| 171 | 3 | my $el = $self->[0][$index]; | |||||||||||||||||||||||||||
| 172 | 3 | ▶ | return $el->[1] if $el; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =item $seq->delegate_names (key?) | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | If all element values are HASH-references, then it is possible to | ||||||||||||||||||||||||||||
| 178 | store each element's name in its value under a given key (that is, to | ||||||||||||||||||||||||||||
| 179 | delegate the name to the HASH value). The default value for key is | ||||||||||||||||||||||||||||
| 180 | C<#name>. It is a fatal error to try to delegate names if some of the | ||||||||||||||||||||||||||||
| 181 | values is not a HASH reference. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | sub delegate_names { | ||||||||||||||||||||||||||
|
Subroutine
delegate_names: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 2 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 187 | 2 | ▶ | $key = '#name' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 2 | ▶ | if (grep { !UNIVERSAL::isa($_->[1],'HASH') } @{$self->[0]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | croak("Error: sequence contains a non-HASH element (Treex::PML::Seq can only delegate names to values if all values are HASH refs)!"); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 2 | foreach my $element (@{$self->[0]}) { | |||||||||||||||||||||||||||
| 192 | 4 | $element->[1]{$key} = $element->[0]; # store element's name in key $key of its value | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =item $seq->validate (content_pattern?) | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Check that content of the sequence satisfies a constraint specified | ||||||||||||||||||||||||||||
| 200 | by means of a regular expression C<content_pattern>. If no content_pattern is | ||||||||||||||||||||||||||||
| 201 | given, the one stored with the object is used (if any; otherwise undef | ||||||||||||||||||||||||||||
| 202 | is returned). | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Returns: 1 if the content satisfies the constraint, 0 otherwise. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 3 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 209 | 3 | my ($self,$re) = @_; | |||||||||||||||||||||||||||
| 210 | 3 | ▶ | $re = $self->content_pattern if !defined($re); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 3 | ▶ | return unless defined $re; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 3 | my $content = join "",map { "<$_>"} $self->names; | |||||||||||||||||||||||||||
| 213 | 3 | $re=~s/\#/\\\#/g; | |||||||||||||||||||||||||||
| 214 | 3 | $re=~s/,/ /g; | |||||||||||||||||||||||||||
| 215 | 3 | $re=~s/\s+/ /g; | |||||||||||||||||||||||||||
| 216 | 3 | $re=~s/([^()?+*|,\s]+)/(?:<$1>)/g; | |||||||||||||||||||||||||||
| 217 | # warn "'$content' VERSUS /$re/\n"; | ||||||||||||||||||||||||||||
| 218 | 3 | ▶ | return $content=~m/^$re$/x ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =item $seq->push_element (name, value) | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Append a given name-value pair to the sequence. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =cut | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 14 | ▶ | sub push_element { | ||||||||||||||||||||||||||
|
Subroutine
push_element: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 14 | my ($self,$name,$value)=@_; | |||||||||||||||||||||||||||
| 229 | 14 | push @{$self->[0]},Treex::PML::Seq::Element->new($name,$value); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =item $seq->push_element_obj (obj) | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Append a given Treex::PML::Seq::Element object to the sequence. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 30 | ▶ | sub push_element_obj { | ||||||||||||||||||||||||||
|
Subroutine
push_element_obj: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | 30 | my ($self,$obj)=@_; | |||||||||||||||||||||||||||
| 240 | 30 | push @{$self->[0]},$obj; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item $seq->unshift_element (name, value) | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | Prepend a given name-value pair to the sequence. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 1 | ▶ | sub unshift_element { | ||||||||||||||||||||||||||
|
Subroutine
unshift_element: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 250 | 1 | my ($self,$name,$value)=@_; | |||||||||||||||||||||||||||
| 251 | 1 | unshift @{$self->[0]},Treex::PML::Seq::Element->new($name,$value); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =item $seq->unshift_element_obj (obj) | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Unshift a given Treex::PML::Seq::Element object to the sequence. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =cut | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 2 | ▶ | sub unshift_element_obj { | ||||||||||||||||||||||||||
|
Subroutine
unshift_element_obj: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 261 | 2 | my ($self,$obj)=@_; | |||||||||||||||||||||||||||
| 262 | 2 | unshift @{$self->[0]},$obj; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =item $seq->delete_element (element) | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | Find and remove (all occurences) of a given Treex::PML::Seq::Element object | ||||||||||||||||||||||||||||
| 268 | in the sequence. Returns the number of elements removed. | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =cut | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 2 | ▶ | sub delete_element { | ||||||||||||||||||||||||||
|
Subroutine
delete_element: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 273 | 2 | my ($self,$element)=@_; | |||||||||||||||||||||||||||
| 274 | 2 | my $start = @{$self->[0]}; | |||||||||||||||||||||||||||
| 275 | 2 | @{$self->[0]} = grep { $_ != $element } @{$self->[0]}; | |||||||||||||||||||||||||||
| 276 | 2 | my $end = @{$self->[0]}; | |||||||||||||||||||||||||||
| 277 | 2 | return $start-$end; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =item $seq->delete_value (value) | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | Find and remove all elements with a given value. Returns the number of | ||||||||||||||||||||||||||||
| 283 | elements removed. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 1 | ▶ | sub delete_value { | ||||||||||||||||||||||||||
|
Subroutine
delete_value: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||||||||||||||||
| 288 | 1 | my ($self,$value)=@_; | |||||||||||||||||||||||||||
| 289 | 1 | my $start = @{$self->[0]}; | |||||||||||||||||||||||||||
| 290 | 1 | my $v; | |||||||||||||||||||||||||||
| 291 | 1 | ▶ | if (ref($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | @{$self->[0]} = grep { $v = $_->value; ref($v) and ($v != $value) } @{$self->[0]}; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $v && $v != $value
MC/DC0%0/2
ref $v && $v != $value
ref $v
$v != $value
Truth table0%0/3
A && B
A= ref $vB= $v != $value
| |||||||||||||||||||||||||||||
| 293 | } else { | ||||||||||||||||||||||||||||
| 294 | 1 | ▶ | @{$self->[0]} = grep { $v = $_->value; !ref($v) and ($v ne $value) } @{$self->[0]}; | ||||||||||||||||||||||||||
|
Condition66%2/3
!ref($v) && $v ne $value
MC/DC50%1/2
!ref($v) && $v ne $value
!ref($v)
$v ne $value
Truth table66%2/3
not(A) && B
A= !ref($v)B= $v ne $value
| |||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | 1 | my $end = @{$self->[0]}; | |||||||||||||||||||||||||||
| 297 | 1 | return $start-$end; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =item $seq->index_of ($value) | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Search the sequence for a particular value | ||||||||||||||||||||||||||||
| 303 | and return the index of its first occurence in the sequence. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | Note: Use $seq->elements_list->index_of($element) to search for a Treex::PML::Seq::Element. | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 2 | ▶ | sub index_of { | ||||||||||||||||||||||||||
|
Subroutine
index_of: called CC=9 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 2 | my ($self,$value)=@_; | |||||||||||||||||||||||||||
| 311 | 2 | ▶ | die 'Usage: Treex::PML::Seq->index_of($value) (wrong number of arguments!)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | if @_!=2; | ||||||||||||||||||||||||||||
| 313 | 2 | my $list = $self->[0]; | |||||||||||||||||||||||||||
| 314 | 2 | ▶ | if (ref($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | my $v; | |||||||||||||||||||||||||||
| 316 | 0 | for my $i (0..$#$list) { | |||||||||||||||||||||||||||
| 317 | 0 | $v = $list->[$i]->value; | |||||||||||||||||||||||||||
| 318 | 0 | ▶ | return $i if ref($v) and $value == $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $v and $value == $v
MC/DC0%0/2
ref $v and $value == $v
ref $v
$value == $v
Truth table0%0/3
A and B
A= ref $vB= $value == $v
| |||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } else { | ||||||||||||||||||||||||||||
| 321 | 2 | my $v; | |||||||||||||||||||||||||||
| 322 | 2 | for my $i (0..$#$list) { | |||||||||||||||||||||||||||
| 323 | 13 | $v = $list->[$i]->value; | |||||||||||||||||||||||||||
| 324 | 13 | ▶ | return $i if !ref($v) and $value eq $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $v and $value eq $v
MC/DC50%1/2
not ref $v and $value eq $v
not ref $v
$value eq $v
Truth table66%2/3
not(A) and B
A= not ref $vB= $value eq $v
| |||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 1 | return; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =item $seq->delete_element_at($index) | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | Delete the element at the given index. Returns 1 if successful, 0 if not | ||||||||||||||||||||||||||||
| 333 | (element at the index didn't exist). | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =cut | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 2 | ▶ | sub delete_element_at { | ||||||||||||||||||||||||||
|
Subroutine
delete_element_at: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | 2 | my ($self, $index) = @_; | |||||||||||||||||||||||||||
| 339 | 2 | ▶ | return 0 unless $self->element_at($index); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 1 | splice @{ $self->[0] }, $index, 1; | |||||||||||||||||||||||||||
| 342 | 1 | return 1 | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =item $seq->replace_element_at($index, $name, $value) | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Replace the element at the given index with a new element with the given name | ||||||||||||||||||||||||||||
| 348 | and value. Returns 1 if successful, 0 if not (element at the index didn't | ||||||||||||||||||||||||||||
| 349 | exist). | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =cut | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 1 | ▶ | sub replace_element_at { | ||||||||||||||||||||||||||
|
Subroutine
replace_element_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 354 | 1 | my ($self, $index, $name, $value) = @_; | |||||||||||||||||||||||||||
| 355 | 1 | return $self->replace_element_obj_at($index, | |||||||||||||||||||||||||||
| 356 | 'Treex::PML::Seq::Element'->new($name, $value)) | ||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =item $seq->replace_element_obj_at($index, $element) | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | Replace the element at the given index with the given element. Returns 1 if | ||||||||||||||||||||||||||||
| 362 | successful, 0 if not (element at the index didn't exist). | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =cut | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 2 | ▶ | sub replace_element_obj_at { | ||||||||||||||||||||||||||
|
Subroutine
replace_element_obj_at: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 367 | 2 | my ($self, $index, $element) = @_; | |||||||||||||||||||||||||||
| 368 | 2 | ▶ | defined $self->element_at($index) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 2 | splice @{ $self->[0] }, $index, 1, $element; | |||||||||||||||||||||||||||
| 371 | 2 | return 1 | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # sub splice { | ||||||||||||||||||||||||||||
| 375 | # # TODO | ||||||||||||||||||||||||||||
| 376 | # } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =item $list->empty () | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Remove all values from the sequence. | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 1 | ▶ | sub empty { | ||||||||||||||||||||||||||
|
Subroutine
empty: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 1 | ▶ | die 'Usage: Treex::PML::Seq->empty() (wrong number of arguments!)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | if @_!=1; | ||||||||||||||||||||||||||||
| 387 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 388 | 1 | $self->[0]->empty; | |||||||||||||||||||||||||||
| 389 | 1 | return $self; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =back | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =head1 AUXILIARY FUNCTIONS | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | =over 5 | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =item Treex::PML::Seq::content_pattern2regexp($pattern) | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | This utility function converts a given sequence content pattern string | ||||||||||||||||||||||||||||
| 401 | into a Perl regular expression. The resulting expression matches | ||||||||||||||||||||||||||||
| 402 | a list of element 'tags', where a tag is an element name surrounded by < and >. | ||||||||||||||||||||||||||||
| 403 | For example, the content pattern 'A,#TEXT,(B+|C)*' translates roughly | ||||||||||||||||||||||||||||
| 404 | to '<A><\#TEXT>(?:(?:<B>)+(?:<C>))*' and matches (a substring of) each of the following strings: | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | '<A><#TEXT>' | ||||||||||||||||||||||||||||
| 407 | 'foo<A><#TEXT><B><B><C>bar' | ||||||||||||||||||||||||||||
| 408 | '<A><#TEXT><B><C><D>' | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =back | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =cut | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 9 | ▶ | sub content_pattern2regexp { | ||||||||||||||||||||||||||
|
Subroutine
content_pattern2regexp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 416 | 9 | my ($re)=@_; | |||||||||||||||||||||||||||
| 417 | 9 | $re=~s/[\${}\\]//g; # sanity | |||||||||||||||||||||||||||
| 418 | 9 | $re=~s/\(\?//g; # safety | |||||||||||||||||||||||||||
| 419 | 9 | $re=~s/\#/\\\#/g; | |||||||||||||||||||||||||||
| 420 | 9 | $re=~s/,/ /g; | |||||||||||||||||||||||||||
| 421 | 9 | $re=~s/\s+/ /g; | |||||||||||||||||||||||||||
| 422 | 9 | $re=~s/([^()?+*|,\s]+)/(?:<$1>)/g; | |||||||||||||||||||||||||||
| 423 | 9 | $re=~s/ //g; | |||||||||||||||||||||||||||
| 424 | 9 | return $re; | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | L<Treex::PML>, L<Treex::PML::Factory>, L<Treex::PML::Schema>, L<Treex::PML::Seq::Element>, L<Treex::PML::List> | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Copyright (C) 2006-2010 by Petr Pajas, 2010-2024 Jan Stepanek | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 437 | it under the same terms as Perl itself, either Perl version 5.8.2 or, | ||||||||||||||||||||||||||||
| 438 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =cut | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 9 | 1; | |||||||||||||||||||||||||||