/usr/share/perl5/DateTime/Format/Natural/Extract.pm is in libdatetime-format-natural-perl 1.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | package DateTime::Format::Natural::Extract;
use strict;
use warnings;
use base qw(
DateTime::Format::Natural::Duration::Checks
DateTime::Format::Natural::Formatted
);
use boolean qw(true false);
use constant DATE_TYPE => 0x01;
use constant GRAMMAR_TYPE => 0x02;
use constant DURATION_TYPE => 0x04;
our $VERSION = '0.11';
my %grammar_durations = map { $_ => true } qw(for_count_unit);
my $get_range = sub
{
my ($aref, $index) = @_;
return [ grep defined, @$aref[$index, $index + 1] ];
};
my $extract_duration = sub
{
my ($skip, $indexes, $index) = @_;
return false unless defined $indexes->[$index] && defined $indexes->[$index + 1];
my ($left_index, $right_index) = ($indexes->[$index][1], $indexes->[$index + 1][0]);
return ($skip->{$left_index} || $skip->{$right_index}) ? false : true;
};
sub _extract_expressions
{
my $self = shift;
my ($extract_string) = @_;
$extract_string =~ s/^\s*[,;.]?//;
$extract_string =~ s/[,;.]?\s*$//;
while ($extract_string =~ /([,;.])/g) {
my $mark = $1;
my %patterns = (
',' => qr/(?!\d{4})/,
';' => qr/(?=\w)/,
'.' => qr/(?=\w)/,
);
my $pattern = $patterns{$mark};
$extract_string =~ s/\Q$mark\E \s+? $pattern/ [token] /x; # pretend punctuation marks are tokens
}
$self->_rewrite(\$extract_string);
my @tokens = split /\s+/, $extract_string;
my %entries = %{$self->{data}->__grammar('')};
my (@expressions, %skip);
my $timespan_sep = $self->{data}->__timespan('literal');
if ($extract_string =~ /\s+ $timespan_sep \s+/ix) {
my $trim = sub { local $_ = shift; s/^\s+//; s/\s+$//; $_ };
my @strings = grep /\S/, map $trim->($_), split /\b$timespan_sep\b/i, do {
local $_ = $extract_string;
1 while s/^$timespan_sep\s+//i;
1 while s/\s+$timespan_sep$//i;
$_
};
if (@strings) {
my $index = 0;
$index++ while $extract_string =~ /\G$timespan_sep\s+/gi;
my @indexes;
for (my $i = 0; $i < @strings; $i++) {
my @string_tokens = split /\s+/, $strings[$i];
push @indexes, [ $index, $index + $#string_tokens ];
$index += $#string_tokens + 1;
$index++ while defined $tokens[$index] && $tokens[$index] =~ /^$timespan_sep$/i;
}
my $duration = $self->{data}->{duration};
DURATION: {
for (my $i = 0; $i < @strings - 1; $i++) {
next unless $extract_duration->(\%skip, \@indexes, $i);
my $save_expression = false;
my @chunks;
foreach my $extract (qw(_first_to_last_extract _from_count_to_count_extract)) {
if ($self->$extract($duration, $get_range->(\@strings, $i), $get_range->(\@indexes, $i), \@tokens, \@chunks)) {
$save_expression = true;
last;
}
}
if ($save_expression) {
my $timespan_sep_index = $chunks[0]->[0][1] + 1;
my $expression = join ' ', ($chunks[0]->[1], $tokens[$timespan_sep_index], $chunks[1]->[1]);
my @indexes = ($chunks[0]->[0][0], $chunks[1]->[0][1]);
push @expressions, [ [ @indexes ], $expression, { flags => DURATION_TYPE } ];
$skip{$_} = true foreach ($indexes[0] .. $indexes[1]);
redo DURATION;
}
}
}
}
}
my (%expand, %lengths);
foreach my $keyword (keys %entries) {
$expand{$keyword} = $self->_expand_for($keyword);
$lengths{$keyword} = @{$entries{$keyword}->[0]};
}
my $seen_expression;
do {
$seen_expression = false;
my $date_index;
for (my $i = 0; $i < @tokens; $i++) {
next if $skip{$i};
if ($self->_check_for_date($tokens[$i], $i, \$date_index)) {
last;
}
}
GRAMMAR:
foreach my $keyword (sort { $lengths{$b} <=> $lengths{$a} } grep { $lengths{$_} <= @tokens } keys %entries) {
my @grammar = @{$entries{$keyword}};
my $types_entry = shift @grammar;
my @grammars = [ [ @grammar ], false ];
if ($expand{$keyword} && @$types_entry + 1 <= @tokens) {
@grammar = $self->_expand($keyword, $types_entry, \@grammar);
unshift @grammars, [ [ @grammar ], true ];
}
foreach my $grammar (@grammars) {
my $expanded = $grammar->[1];
my $length = $lengths{$keyword};
$length++ if $expanded;
foreach my $entry (@{$grammar->[0]}) {
my ($types, $expression) = $expanded ? @$entry : ($types_entry, $entry);
my $definition = $expression->[0];
my $matched = false;
my $pos = 0;
my @indexes;
my $date_index;
for (my $i = 0; $i < @tokens; $i++) {
next if $skip{$i};
last unless defined $types->[$pos];
if ($self->_check_for_date($tokens[$i], $i, \$date_index)) {
next;
}
if ($types->[$pos] eq 'SCALAR' && defined $definition->{$pos} && $tokens[$i] =~ /^$definition->{$pos}$/i
or $types->[$pos] eq 'REGEXP' && $tokens[$i] =~ $definition->{$pos}
&& (@indexes ? ($i - $indexes[-1] == 1) : true)
) {
$matched = true;
push @indexes, $i;
$pos++;
}
elsif ($matched) {
last;
}
}
if ($matched
&& @indexes == $length
&& (defined $date_index ? ($indexes[0] - $date_index == 1) : true)
) {
my $expression = join ' ', (defined $date_index ? $tokens[$date_index] : (), @tokens[@indexes]);
my $start_index = defined $date_index ? $indexes[0] - 1 : $indexes[0];
my $type = $grammar_durations{$keyword} ? DURATION_TYPE : GRAMMAR_TYPE;
push @expressions, [ [ $start_index, $indexes[-1] ], $expression, { flags => $type } ];
$skip{$_} = true foreach (defined $date_index ? $date_index : (), @indexes);
$seen_expression = true;
last GRAMMAR;
}
}
}
}
if (defined $date_index && !$seen_expression) {
push @expressions, [ [ ($date_index) x 2 ], $tokens[$date_index], { flags => DATE_TYPE } ];
$skip{$date_index} = true;
$seen_expression = true;
}
} while ($seen_expression);
return $self->_finalize_expressions(\@expressions, \@tokens);
}
sub _finalize_expressions
{
my $self = shift;
my ($expressions, $tokens) = @_;
my $timespan_sep = $self->{data}->__timespan('literal');
my (@duration_indexes, @final_expressions);
my $seen_duration = false;
my @expressions = sort { $a->[0][0] <=> $b->[0][0] } @$expressions;
for (my $i = 0; $i < @expressions; $i++) {
my $expression = $expressions[$i];
my $prev = $expression->[0][0] - 1;
my $next = $expression->[0][1] + 1;
if ($expression->[2]->{flags} & DATE_TYPE
|| $expression->[2]->{flags} & GRAMMAR_TYPE
) {
if (!$seen_duration
&& defined $tokens->[$next]
&& $tokens->[$next] =~ /^$timespan_sep$/i
&& defined $expressions[$i + 1]
&& ($expressions[$i + 1]->[2]->{flags} & DATE_TYPE
|| $expressions[$i + 1]->[2]->{flags} & GRAMMAR_TYPE)
&& $expressions[$i + 1]->[0][0] - $next == 1
) {
push @duration_indexes, ($expression->[0][0] .. $expression->[0][1]);
$seen_duration = true;
}
elsif ($seen_duration) {
push @duration_indexes, ($prev, $expression->[0][0] .. $expression->[0][1]);
push @final_expressions, join ' ', @$tokens[@duration_indexes];
@duration_indexes = ();
$seen_duration = false;
}
else {
push @final_expressions, $expression->[1];
}
}
elsif ($expression->[2]->{flags} & DURATION_TYPE) {
push @final_expressions, $expression->[1];
}
}
my $exclude = sub { $_[0] =~ /^\d{1,2}$/ };
return grep !$exclude->($_), @final_expressions;
}
sub _check_for_date
{
my $self = shift;
my ($token, $index, $date_index) = @_;
my ($formatted) = $token =~ $self->{data}->__regexes('format');
my %count = $self->_count_separators($formatted);
if ($self->_check_formatted('ymd', \%count)) {
$$date_index = $index;
return true;
}
else {
return false;
}
}
1;
__END__
=head1 NAME
DateTime::Format::Natural::Extract - Extract parsable expressions from strings
=head1 SYNOPSIS
Please see the DateTime::Format::Natural documentation.
=head1 DESCRIPTION
C<DateTime::Format::Natural::Extract> extracts expressions from strings to be
processed by the parse methods.
=head1 SEE ALSO
L<DateTime::Format::Natural>
=head1 AUTHOR
Steven Schubiger <schubiger@cpan.org>
=head1 LICENSE
This program is free software; you may redistribute it and/or
modify it under the same terms as Perl itself.
See L<http://dev.perl.org/licenses/>
=cut
|