blob: a3f2548b289cd2728d2081805b832a9ff8b7844b [file] [log] [blame]
Akron0a29cd22017-02-06 10:58:02 +01001package Krawfish::Query::Length;
Akronbcbe2682017-02-05 13:05:55 +01002use strict;
3use warnings;
Akron71fc0ec2017-11-02 17:34:21 +01004use Role::Tiny::With;
5use Krawfish::Log;
6
7with 'Krawfish::Query';
Akronbcbe2682017-02-05 13:05:55 +01008
Akron0a29cd22017-02-06 10:58:02 +01009use constant DEBUG => 0;
10
Akrona588d072017-10-13 14:45:34 +020011# TODO:
12# This should respect different tokenizations!
Akronbcbe2682017-02-05 13:05:55 +010013
Akron0a29cd22017-02-06 10:58:02 +010014# Constructor
Akronbcbe2682017-02-05 13:05:55 +010015sub new {
16 my $class = shift;
17 bless {
Akron0a29cd22017-02-06 10:58:02 +010018 span => shift,
19 min => shift // 0,
20 max => shift,
21 tokens => shift,
22 current => undef
Akronbcbe2682017-02-05 13:05:55 +010023 }, $class;
24};
25
Akrona588d072017-10-13 14:45:34 +020026
27# Clone query
Akronb7653672017-08-07 14:34:14 +020028sub clone {
29 my $self = shift;
30 __PACKAGE__->new(
31 $self->{span}->clone,
32 $self->{min},
33 $self->{max},
34 $self->{tokens},
35 );
36};
37
Akrona588d072017-10-13 14:45:34 +020038
39# Move to next posting
Akronbcbe2682017-02-05 13:05:55 +010040sub next {
41 my $self = shift;
42
43 my $span = $self->{span};
44
45 # Check if the length is between the given boundaries
46 while ($span->next) {
47
Akron0a29cd22017-02-06 10:58:02 +010048 # Get current span
Akronbcbe2682017-02-05 13:05:55 +010049 my $current = $span->current;
50
Akron0a29cd22017-02-06 10:58:02 +010051 my $length = $current->end - $current->start;
52
53 print_log('length', "Check length $length") if DEBUG;
54
55 # Max is given
56 if ($self->{max}) {
57
58 # min and max are identical
59 if ($self->{min} == $self->{max} && $length == $self->{min}) {
60
Akrona588d072017-10-13 14:45:34 +020061 if (DEBUG) {
62 print_log(
63 'length',
64 "! Length $length has the length " . $self->{min}
65 );
66 };
Akron0a29cd22017-02-06 10:58:02 +010067
68 $self->{current} = $current;
69 return 1;
70 }
71
72 # in min and max
73 elsif ($length >= $self->{min} && $length <= $self->{max}) {
74
75 if (DEBUG) {
76 print_log(
77 'length',
78 "! Length $length is between " . $self->{min} . '-' . $self->{max}
79 );
80 };
81
82 $self->{current} = $current;
83 return 1;
84 };
85 }
86
87 # length >= min
88 elsif ($length > $self->{min}) {
89
90 print_log('length', '! Length is larger than ' . $self->{min}) if DEBUG;
91
Akronbcbe2682017-02-05 13:05:55 +010092 $self->{current} = $current;
93 return 1;
94 };
95 };
96
97 $self->{current} = undef;
98 return 0;
99};
100
101
Akron0a29cd22017-02-06 10:58:02 +0100102# Get current posting
Akronbcbe2682017-02-05 13:05:55 +0100103sub current {
104 return $_[0]->{current};
105};
106
Akronbcbe2682017-02-05 13:05:55 +0100107
Akrona588d072017-10-13 14:45:34 +0200108# Get maximum frequency
Akronfaf76852017-07-19 17:37:07 +0200109sub max_freq {
110 $_[0]->{span}->max_freq;
Akron0c998cc2017-07-19 03:29:37 +0200111};
112
Akron15fc1972017-07-20 22:53:00 +0200113
Akrona588d072017-10-13 14:45:34 +0200114# Filter query by VC
Akron15fc1972017-07-20 22:53:00 +0200115sub filter_by {
116 my ($self, $corpus) = @_;
117 $self->{span} = $self->{span}->filter_by($corpus);
118 $self;
119};
120
121
Akron2bc94da2017-10-27 15:20:36 +0200122# Requires filter
123sub requires_filter {
124 $_[0]->{span}->requires_filter;
125};
126
127
Akron0a29cd22017-02-06 10:58:02 +0100128# Stringification
Akronbcbe2682017-02-05 13:05:55 +0100129sub to_string {
130 my $self = shift;
131 my $str = 'length(';
132 $str .= $self->{min} . '-' . $self->{max};
133 $str .= ';' . $self->{token} if $self->{token};
134 $str .= ':' . $self->{span}->to_string;
135 return $str . ')';
136};
137
1381;