Lines 794-823
operands and double quoted strings.
Link Here
|
794 |
|
794 |
|
795 |
sub _truncate_terms { |
795 |
sub _truncate_terms { |
796 |
my ( $self, $query ) = @_; |
796 |
my ( $self, $query ) = @_; |
797 |
my @stops = qw/and or not/; |
797 |
|
798 |
my @new_terms; |
798 |
# '"donald duck" "the mouse" and peter" get split into |
799 |
my @quote_split = split /(["])([^"]+)\1/, $query; |
799 |
# ['', '"donald duck"', '', ' ', '', '"the mouse"', '', ' ', 'and', ' ', 'pete'] |
800 |
#Above splits the string based on matching pairs of double quotes |
800 |
my @tokens = split /("[^"]+"|\s+)/, $query; |
801 |
#In practice we get ('','"','donald duck',' ','"','the mouse',' and pete') |
801 |
|
802 |
#given the string '"donald duck" "the mouse" and pete' |
802 |
# Filter out empty tokens |
803 |
#so we ignore empties, quote the ones after a '"' and split the rest on spaces |
803 |
my @words = grep { $_ !~ /^\s*$/ } @tokens; |
804 |
for (my $i=0; $i < @quote_split; $i++ ) { |
804 |
|
805 |
next if ( $quote_split[$i] eq '' || $quote_split[$i] eq ' ' ); |
805 |
# Append '*' to words if needed, ie. if it's not surrounded by quotes, not |
806 |
if ( $quote_split[$i] eq '"' ){ |
806 |
# terminated by '*' and not a keyword |
807 |
$i++; |
807 |
my @terms = map { |
808 |
$quote_split[$i] = '"'.$quote_split[$i].'"'; |
808 |
my $w = $_; |
809 |
push @new_terms, $quote_split[$i] |
809 |
(/^"/ or /\*$/ or grep {lc($w) eq $_} qw/and or not/) ? $_ : "$_*"; |
810 |
} else { |
810 |
} @words; |
811 |
my @space_split = split /[\(\s\)]/, $quote_split[$i]; |
811 |
|
812 |
foreach my $term (@space_split) { |
812 |
return join ' ', @terms; |
813 |
next if ( $term eq '' || $term eq ' ' ); |
|
|
814 |
$term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) ); |
815 |
push @new_terms, $term; |
816 |
} |
817 |
} |
818 |
} |
819 |
$query = join ' ', @new_terms; |
820 |
return $query; |
821 |
} |
813 |
} |
822 |
|
814 |
|
823 |
1; |
815 |
1; |
824 |
- |
|
|