blob: 59a07528e0e31c7f3a8d85d2f646fb63c44bdf8f [file] [log] [blame]
Akron349747d2016-12-05 11:05:53 +01001package Krawfish::Koral::Result;
Akron290f59f2017-08-17 21:55:07 +02002use Role::Tiny::With;
Akron9642cf32017-10-30 12:42:14 +01003with 'Krawfish::Koral::Report';
Akron71fc0ec2017-11-02 17:34:21 +01004with 'Krawfish::Koral::Result::Inflatable';
Akron349747d2016-12-05 11:05:53 +01005use strict;
6use warnings;
7
Akron71fc0ec2017-11-02 17:34:21 +01008# 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
Akron81181512017-01-19 09:52:34 +010019sub new {
20 my $class = shift;
21 bless {
Akron15ce2762017-11-07 17:34:39 +010022 group => undef,
Akronc11e7fe2017-10-26 13:05:18 +020023 aggregation => [],
24 matches => []
Akronb5809f42017-05-03 01:26:08 +020025 }, $class;
26};
27
Akron681ca232017-08-12 12:15:12 +020028
29# Add matches to the result
30sub add_match {
Akronb5809f42017-05-03 01:26:08 +020031 my ($self, $match) = @_;
Akron681ca232017-08-12 12:15:12 +020032 push @{$self->{matches}}, $match;
Akronb5809f42017-05-03 01:26:08 +020033};
34
Akron681ca232017-08-12 12:15:12 +020035
Akron71fc0ec2017-11-02 17:34:21 +010036# Get list of matches
Akronc11e7fe2017-10-26 13:05:18 +020037sub matches {
38 $_[0]->{matches};
39};
40
Akron681ca232017-08-12 12:15:12 +020041
Akronc11e7fe2017-10-26 13:05:18 +020042# Add aggregated information
43sub add_aggregation {
44 my ($self, $aggregation) = @_;
Akron4fdac712017-10-27 17:37:19 +020045
Akronc11e7fe2017-10-26 13:05:18 +020046 push(@{$self->{aggregation}}, $aggregation);
47};
48
49
Akronc84f00c2017-12-03 17:24:21 +010050# Merge aggregation
51sub 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
Akronc11e7fe2017-10-26 13:05:18 +020079# Get aggregations
80sub aggregation {
81 $_[0]->{aggregation};
82};
83
84
Akron15ce2762017-11-07 17:34:39 +010085# Get or set group results
86sub group {
87 my $self = shift;
88 if (@_) {
89 $self->{group} = shift;
90 return $self;
91 };
92
93 $self->{group};
94};
95
96
Akron71fc0ec2017-11-02 17:34:21 +010097# Stringification
Akronc11e7fe2017-10-26 13:05:18 +020098sub to_string {
Akron5c5f2732017-11-20 13:58:28 +010099 my ($self, $id) = @_;
Akronc11e7fe2017-10-26 13:05:18 +0200100 my $str = '';
101
102 # Add aggregation
Akron15ce2762017-11-07 17:34:39 +0100103 if (@{$self->{aggregation}}) {
Akronc11e7fe2017-10-26 13:05:18 +0200104 $str .= '[aggr=';
105 foreach (@{$self->{aggregation}}) {
Akron5c5f2732017-11-20 13:58:28 +0100106 $str .= $_->to_string($id);
Akronc11e7fe2017-10-26 13:05:18 +0200107 };
108 $str .= ']';
109 };
110
111 # Create matches
Akron15ce2762017-11-07 17:34:39 +0100112 if (@{$self->{matches}}) {
Akronc11e7fe2017-10-26 13:05:18 +0200113 $str .= '[matches=';
114 foreach (@{$self->{matches}}) {
Akron5c5f2732017-11-20 13:58:28 +0100115 $str .= $_->to_string($id);
Akronc11e7fe2017-10-26 13:05:18 +0200116 };
117 $str .= ']';
118 };
119
Akron15ce2762017-11-07 17:34:39 +0100120 if ($self->group) {
121 $str .= '[group=';
Akron5c5f2732017-11-20 13:58:28 +0100122 $str .= $self->group->to_string($id);
Akron15ce2762017-11-07 17:34:39 +0100123 $str .= ']';
124 };
125
Akronc11e7fe2017-10-26 13:05:18 +0200126 return $str;
127};
128
Akron71fc0ec2017-11-02 17:34:21 +0100129# Inflate results
130sub inflate {
131 my ($self, $dict) = @_;
132 foreach (@{$self->matches}) {
133 $_->inflate($dict);
134 };
135 foreach (@{$self->aggregation}) {
136 $_->inflate($dict);
137 };
138
Akron15ce2762017-11-07 17:34:39 +0100139 if ($self->group) {
140 $self->group->inflate($dict);
141 };
142
Akron71fc0ec2017-11-02 17:34:21 +0100143 $self;
144};
145
Akronc11e7fe2017-10-26 13:05:18 +0200146
147# Get koral result fragment
Akron681ca232017-08-12 12:15:12 +0200148sub to_koral_fragment {
149 my $self = shift;
Akron71fc0ec2017-11-02 17:34:21 +0100150 my $result = {
Akron681ca232017-08-12 12:15:12 +0200151 '@type' => 'koral:result',
Akron681ca232017-08-12 12:15:12 +0200152 };
Akron71fc0ec2017-11-02 17:34:21 +0100153
Akron71fc0ec2017-11-02 17:34:21 +0100154 # Add aggregation
Akronf0768c72017-11-03 09:38:58 +0100155 if (@{$self->{aggregation}}) {
Akron71fc0ec2017-11-02 17:34:21 +0100156 # 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
Akron2ee0ec92017-11-07 23:19:02 +0100171 if (@{$self->{matches}}) {
Akron71fc0ec2017-11-02 17:34:21 +0100172 my @matches = ();
173 foreach (@{$self->{matches}}) {
174 push @matches, $_->to_koral_fragment;
175 };
176
177 $result->{matches} = \@matches;
178 };
179
Akron2ee0ec92017-11-07 23:19:02 +0100180 if ($self->{group}) {
181 $result->{group} = $self->group->to_koral_fragment
182 };
183
Akron71fc0ec2017-11-02 17:34:21 +0100184 return $result;
Akron681ca232017-08-12 12:15:12 +0200185};
186
187
Akronb5809f42017-05-03 01:26:08 +02001881;
189
190__END__