| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 1 | package Krawfish::Koral::Result; |
| Akron | 290f59f | 2017-08-17 21:55:07 +0200 | [diff] [blame] | 2 | use Role::Tiny::With; |
| Akron | 9642cf3 | 2017-10-30 12:42:14 +0100 | [diff] [blame] | 3 | with 'Krawfish::Koral::Report'; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 4 | with 'Krawfish::Koral::Result::Inflatable'; |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 5 | use strict; |
| 6 | use warnings; |
| 7 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 8 | # TODO: |
| 9 | # It may be beneficial to have |
| 10 | # - Aggregate |
| 11 | # - Group |
| 12 | # - Sort |
| 13 | # - Enrich |
| 14 | # on the same level as query and corpus |
| 15 | # and remove the intermediate compile |
| 16 | # directive! |
| 17 | |
| 18 | # Constructor |
| Akron | 8118151 | 2017-01-19 09:52:34 +0100 | [diff] [blame] | 19 | sub new { |
| 20 | my $class = shift; |
| 21 | bless { |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 22 | group => undef, |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 23 | aggregation => [], |
| 24 | matches => [] |
| Akron | b5809f4 | 2017-05-03 01:26:08 +0200 | [diff] [blame] | 25 | }, $class; |
| 26 | }; |
| 27 | |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 28 | |
| 29 | # Add matches to the result |
| 30 | sub add_match { |
| Akron | b5809f4 | 2017-05-03 01:26:08 +0200 | [diff] [blame] | 31 | my ($self, $match) = @_; |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 32 | push @{$self->{matches}}, $match; |
| Akron | b5809f4 | 2017-05-03 01:26:08 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 35 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 36 | # Get list of matches |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 37 | sub matches { |
| 38 | $_[0]->{matches}; |
| 39 | }; |
| 40 | |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 41 | |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 42 | # Add aggregated information |
| 43 | sub add_aggregation { |
| 44 | my ($self, $aggregation) = @_; |
| Akron | 4fdac71 | 2017-10-27 17:37:19 +0200 | [diff] [blame] | 45 | |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 46 | push(@{$self->{aggregation}}, $aggregation); |
| 47 | }; |
| 48 | |
| 49 | |
| Akron | c84f00c | 2017-12-03 17:24:21 +0100 | [diff] [blame] | 50 | # Merge aggregation |
| 51 | sub merge_aggregation { |
| 52 | my ($self, $result) = @_; |
| 53 | |
| 54 | my $aggregates = $self->{aggregation}; |
| 55 | |
| 56 | # Check all aggregations |
| 57 | AGGR: foreach my $new_aggr (@{$result->{aggregation}}) { |
| 58 | |
| 59 | # Merge with existing aggregation |
| 60 | foreach my $est_aggr (@$aggregates) { |
| 61 | |
| 62 | # Merge new aggregations with established aggregations |
| 63 | if ($new_aggr->key eq $est_aggr->key) { |
| 64 | |
| 65 | # Merge |
| 66 | $est_aggr->merge($new_aggr); |
| 67 | next AGGR; |
| 68 | }; |
| 69 | }; |
| 70 | |
| 71 | # Introduce aggregation |
| 72 | $self->add_aggregation($new_aggr); |
| 73 | }; |
| 74 | |
| 75 | return; |
| 76 | }; |
| 77 | |
| 78 | |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 79 | # Get aggregations |
| 80 | sub aggregation { |
| 81 | $_[0]->{aggregation}; |
| 82 | }; |
| 83 | |
| 84 | |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 85 | # Get or set group results |
| 86 | sub group { |
| 87 | my $self = shift; |
| 88 | if (@_) { |
| 89 | $self->{group} = shift; |
| 90 | return $self; |
| 91 | }; |
| 92 | |
| 93 | $self->{group}; |
| 94 | }; |
| 95 | |
| 96 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 97 | # Stringification |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 98 | sub to_string { |
| Akron | 5c5f273 | 2017-11-20 13:58:28 +0100 | [diff] [blame] | 99 | my ($self, $id) = @_; |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 100 | my $str = ''; |
| 101 | |
| 102 | # Add aggregation |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 103 | if (@{$self->{aggregation}}) { |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 104 | $str .= '[aggr='; |
| 105 | foreach (@{$self->{aggregation}}) { |
| Akron | 5c5f273 | 2017-11-20 13:58:28 +0100 | [diff] [blame] | 106 | $str .= $_->to_string($id); |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 107 | }; |
| 108 | $str .= ']'; |
| 109 | }; |
| 110 | |
| 111 | # Create matches |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 112 | if (@{$self->{matches}}) { |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 113 | $str .= '[matches='; |
| 114 | foreach (@{$self->{matches}}) { |
| Akron | 5c5f273 | 2017-11-20 13:58:28 +0100 | [diff] [blame] | 115 | $str .= $_->to_string($id); |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 116 | }; |
| 117 | $str .= ']'; |
| 118 | }; |
| 119 | |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 120 | if ($self->group) { |
| 121 | $str .= '[group='; |
| Akron | 5c5f273 | 2017-11-20 13:58:28 +0100 | [diff] [blame] | 122 | $str .= $self->group->to_string($id); |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 123 | $str .= ']'; |
| 124 | }; |
| 125 | |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 126 | return $str; |
| 127 | }; |
| 128 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 129 | # Inflate results |
| 130 | sub inflate { |
| 131 | my ($self, $dict) = @_; |
| 132 | foreach (@{$self->matches}) { |
| 133 | $_->inflate($dict); |
| 134 | }; |
| 135 | foreach (@{$self->aggregation}) { |
| 136 | $_->inflate($dict); |
| 137 | }; |
| 138 | |
| Akron | 15ce276 | 2017-11-07 17:34:39 +0100 | [diff] [blame] | 139 | if ($self->group) { |
| 140 | $self->group->inflate($dict); |
| 141 | }; |
| 142 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 143 | $self; |
| 144 | }; |
| 145 | |
| Akron | c11e7fe | 2017-10-26 13:05:18 +0200 | [diff] [blame] | 146 | |
| 147 | # Get koral result fragment |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 148 | sub to_koral_fragment { |
| 149 | my $self = shift; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 150 | my $result = { |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 151 | '@type' => 'koral:result', |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 152 | }; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 153 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 154 | # Add aggregation |
| Akron | f0768c7 | 2017-11-03 09:38:58 +0100 | [diff] [blame] | 155 | if (@{$self->{aggregation}}) { |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 156 | # It is beneficial to be able to point to, |
| 157 | # e.g. the field frequencies without iterating |
| 158 | # through all aggregations. |
| 159 | # Therefor it is probably better to use the ->key |
| 160 | # to add aggregations instead of arrays. |
| 161 | |
| 162 | my %aggr = (); |
| 163 | foreach (@{$self->{aggregation}}) { |
| 164 | $aggr{$_->key} = $_->to_koral_fragment; |
| 165 | }; |
| 166 | |
| 167 | $result->{aggregation} = \%aggr; |
| 168 | }; |
| 169 | |
| 170 | # Add matches |
| Akron | 2ee0ec9 | 2017-11-07 23:19:02 +0100 | [diff] [blame] | 171 | if (@{$self->{matches}}) { |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 172 | my @matches = (); |
| 173 | foreach (@{$self->{matches}}) { |
| 174 | push @matches, $_->to_koral_fragment; |
| 175 | }; |
| 176 | |
| 177 | $result->{matches} = \@matches; |
| 178 | }; |
| 179 | |
| Akron | 2ee0ec9 | 2017-11-07 23:19:02 +0100 | [diff] [blame] | 180 | if ($self->{group}) { |
| 181 | $result->{group} = $self->group->to_koral_fragment |
| 182 | }; |
| 183 | |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 184 | return $result; |
| Akron | 681ca23 | 2017-08-12 12:15:12 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
| Akron | b5809f4 | 2017-05-03 01:26:08 +0200 | [diff] [blame] | 188 | 1; |
| 189 | |
| 190 | __END__ |