blob: 5840c025f98aceba5a49f3929071a365c588b450 [file] [log] [blame]
Akronbaade942017-09-25 18:19:02 +02001package Krawfish::Koral::Result::Group::Fields;
Akrondc8dceb2017-08-22 20:25:39 +02002use Krawfish::Util::PatternList qw/pattern_list/;
3use Krawfish::Log;
4use strict;
5use warnings;
6
Akron0f8fb082017-10-09 15:54:41 +02007use constant DEBUG => 0;
Akrondc8dceb2017-08-22 20:25:39 +02008
Akron09ab24b2017-08-24 12:45:39 +02009# TODO:
10# In addition to the group name
11# create a signature that is universal for each group
12
Akrondc8dceb2017-08-22 20:25:39 +020013sub new {
14 my $class = shift;
15 bless {
16 field_keys => shift,
17 cache => undef,
18 group => {},
19 freq => 0
20 }, $class;
21};
22
23
24# Increment on a pattern, which is an ordered list of fields
25sub incr_doc {
26 my ($self, $pattern) = @_;
27
28 # Store all patterns in a cache
29 $self->{cache} = [
30 pattern_list(@$pattern)
31 ];
32};
33
34
35# increment on match
36sub incr_match {
37 $_[0]->{freq}++;
38
39 if (DEBUG) {
40 print_log('p_g_fields', 'Increment match frequency');
41 };
42};
43
44
45# Flush cache
46sub flush {
47 my $self = shift;
48
49 if ($self->{freq}) {
50
51 # Iterate over all lists
52 foreach my $group (@{$self->{cache}}) {
53
54 # This uses an array as a key ... probably should be realized differently
55 my $group_name = join('_',@$group);
56 my $freq = ($self->{group}->{$group_name} //= [0,0]);
57
58 if (DEBUG) {
59 print_log('p_g_fields', 'Group on name ' . $group_name);
60 };
61
62 $freq->[0]++;
63 $freq->[1] += $self->{freq};
64 };
65
66 $self->{cache} = undef;
67 $self->{freq} = 0;
68 };
69};
70
71
72sub inflate {
73 my ($self, $dict) = @_;
74 my $field_keys = $self->{field_keys};
75
76 $self->{field_terms} = [];
77 $self->{group_terms} = [];
78
79 # Inflate head line
80 foreach my $field_id (@{$field_keys}) {
81 my $field_term = $dict->term_by_term_id($field_id);
82
Akron0a57b2c2017-09-23 13:29:51 +020083 # $field_term =~ s/^!//;
84 # TODO:
85 # This may be a direct feature of the dictionary
86 $field_term = substr($field_term, 1);
Akrondc8dceb2017-08-22 20:25:39 +020087
88 push @{$self->{field_terms}}, $field_term;
89 };
90
91 # Inflate groups
92 foreach my $group (sort keys %{$self->{group}}) {
93 my @group = ();
94 my $i = 0;
95 foreach my $term_id (split('_', $group)) {
96
97 # Term is defined at the position
98 if ($term_id != 0) {
99
100 # Retrieve term
101 my $term = $dict->term_by_term_id($term_id);
102 my $field_term = $self->{field_terms}->[$i];
Akron0a57b2c2017-09-23 13:29:51 +0200103
104 # TODO:
105 # This may be a direct feature of the dictionary
106 $term =~ s/^.$field_term://;
Akrondc8dceb2017-08-22 20:25:39 +0200107
108 push @group, $term;
109 }
110 else {
111 push @group, '';
112 };
113
114 # Move to next field
115 $i++;
116 };
117
118 push @{$self->{group_terms}}, [\@group, @{$self->{group}->{$group}}];
119 };
120
121 return $self;
122};
123
124
125sub to_string {
126 my $self = shift;
127 my $str = 'gFields:[';
128
129 if ($self->{field_terms}) {
130 $str .= join(',', @{$self->{field_terms}}) . ':[';
131 foreach my $group (@{$self->{group_terms}}) {
132 $str .= join('|', @{$group->[0]}) . ':' . $group->[1] . ',' . $group->[2] . ';';
133 };
134 }
135 else {
136 $str .= join(',', map { '#' . $_ } @{$self->{field_keys}}) . ':[';
137 foreach my $group (sort keys %{$self->{group}}) {
138 $str .= $group .'=' . join(',', @{$self->{group}->{$group}}) . ';';
139 };
140 };
141 chop $str;
Akron40406212017-09-18 11:51:27 +0200142 return $str . ']';
Akrondc8dceb2017-08-22 20:25:39 +0200143};
144
145
Akron09ab24b2017-08-24 12:45:39 +0200146sub to_koral_query {
147 # Create groups like
148 # {
149 # "@type":"koral:collection",
150 # "groupedBy":"groupedBy:fields", # or "aggregatedBy, "sortedBy"
151 # "labels":[...],
152 # "items":[
153 # {
154 # "@type":"koral:item",
155 # // "signature":"ab47mhjhjgfjuizgtzurzt",
156 # "cols":[...]
157 # }
158 # ]
159 # }
160 ...
161};
162
Akrondc8dceb2017-08-22 20:25:39 +02001631;