blob: 111af25b520304af396592717619f01ecf705c0a [file] [log] [blame]
Akronf703f6f2017-08-25 21:20:52 +02001package Krawfish::Result::Segment::Sort::SubTerm;
Akronfb9e6522017-05-27 14:35:00 +02002use strict;
3use warnings;
4
Akronf703f6f2017-08-25 21:20:52 +02005# This will sort based on a pre-ranked subterm
6
Akronfb9e6522017-05-27 14:35:00 +02007sub new {
8 my $class = shift;
9
10 my $self = bless {
11 index => shift,
12 suffix => shift // 0,
13 descending => shift // 0,
14 }, $class;
15
16 # Get ranking
17 $self->{dict} = $self->{index}->dictionary or return;
18
19 # Get maximum rank if descending order
20 $self->{max} = $self->{ranks}->max if $self->{descending};
21
22 return $self;
23};
24
25sub get {
26 my $self = shift;
27 my $subterm_id = shift;
28 my $rank;
29 if ($self->{suffix}) {
30 $rank = $self->{dict}->prefix_rank_by_subterm_id($subterm_id);
31 }
32 else {
33 $rank = $self->{dict}->suffix_rank_by_subterm_id($subterm_id);
34 };
35
36 # Revert if maximum rank is set
37 return $max ? $max - $rank : $rank;
38};
39
40
411;