blob: b72705ed2c94f51f0cdf5ba17327d640e20ad153 [file] [log] [blame]
Akron151676d2016-03-14 20:12:14 +01001package KorAP::XML::Index::MultiTerm;
Nils Diewald6d565072014-10-30 23:20:58 +00002use strict;
3use warnings;
Nils Diewaldff6d0782014-06-10 18:26:36 +00004use MIME::Base64;
Nils Diewald2db9ad02013-10-29 19:26:43 +00005
Akron72e671f2020-08-04 11:35:40 +02006use constant {
Akron39df7ce2020-08-04 15:55:26 +02007 TERM => 0,
8 O_START => 1,
9 O_END => 2,
10 P_START => 3,
11 P_END => 4,
Akron6a4cb162020-08-06 16:00:33 +020012 PTI => 5,
13 TUI => 6,
14 PAYLOAD => 7,
15 STORED_OFFSETS => 8,
Akron72e671f2020-08-04 11:35:40 +020016};
17
Akron129e4412020-08-05 15:30:12 +020018
19# Construct a multiterm object by passing a term
Nils Diewald6d565072014-10-30 23:20:58 +000020sub new {
Akron6a4cb162020-08-06 16:00:33 +020021 my $class = shift;
22 bless [@_], $class;
Akron4701d092020-08-04 15:20:19 +020023};
24
Akron72e671f2020-08-04 11:35:40 +020025sub set_payload {
26 return $_[0]->[PAYLOAD] = $_[1];
Nils Diewald6d565072014-10-30 23:20:58 +000027};
28
Akron72e671f2020-08-04 11:35:40 +020029sub get_payload {
30 $_[0]->[PAYLOAD];
Nils Diewald6d565072014-10-30 23:20:58 +000031};
32
Akron72e671f2020-08-04 11:35:40 +020033sub set_p_start {
34 return $_[0]->[P_START] = $_[1];
Nils Diewald6d565072014-10-30 23:20:58 +000035};
36
Akron72e671f2020-08-04 11:35:40 +020037sub get_p_start {
38 $_[0]->[P_START] // 0;
Nils Diewald6d565072014-10-30 23:20:58 +000039};
40
Akron72e671f2020-08-04 11:35:40 +020041sub set_p_end {
42 $_[0]->[P_END] = $_[1];
Nils Diewald6d565072014-10-30 23:20:58 +000043};
44
Akron72e671f2020-08-04 11:35:40 +020045sub get_p_end {
46 $_[0]->[P_END] // 0
Nils Diewald6d565072014-10-30 23:20:58 +000047};
48
Akron72e671f2020-08-04 11:35:40 +020049sub set_o_start {
50 return $_[0]->[O_START] = $_[1];
Nils Diewald6d565072014-10-30 23:20:58 +000051};
52
Akron72e671f2020-08-04 11:35:40 +020053sub get_o_start {
54 $_[0]->[O_START] // 0;
Akron1622dd92015-12-09 22:34:26 +010055};
Nils Diewald6d565072014-10-30 23:20:58 +000056
Akron72e671f2020-08-04 11:35:40 +020057sub set_o_end {
58 $_[0]->[O_END] = $_[1];
Akron14ca9f02016-01-29 19:38:18 +010059};
Akron1622dd92015-12-09 22:34:26 +010060
Akron72e671f2020-08-04 11:35:40 +020061sub get_o_end {
62 $_[0]->[O_END] // 0;
63};
64
65sub set_term {
66 return $_[0]->[TERM] = $_[1];
67};
68
69sub get_term {
70 $_[0]->[TERM] // '';
71};
72
73sub set_stored_offsets {
74 return $_[0]->[STORED_OFFSETS] = $_[1];
75};
76
77sub get_stored_offsets {
78 $_[0]->[STORED_OFFSETS];
79};
80
81sub set_pti {
82 return $_[0]->[PTI] = $_[1];
83};
84
85sub get_pti {
86 $_[0]->[PTI];
87};
88
89sub set_tui {
90 return $_[0]->[TUI] = $_[1];
91};
92
93sub get_tui {
94 $_[0]->[TUI];
95};
96
97
Akron1622dd92015-12-09 22:34:26 +010098# To string based on array
Nils Diewald2db9ad02013-10-29 19:26:43 +000099sub to_string {
Akron72e671f2020-08-04 11:35:40 +0200100 my $string = _escape_term($_[0]->[TERM]);
Nils Diewald6a2a14b2015-06-17 20:34:24 +0000101
Akrond69836b2015-12-10 00:40:20 +0100102 my $pre;
Akron1622dd92015-12-09 22:34:26 +0100103
104 # PTI
Akron72e671f2020-08-04 11:35:40 +0200105 $pre .= '<b>' . $_[0]->[PTI] if $_[0]->[PTI];
Akron1622dd92015-12-09 22:34:26 +0100106
107 # Offsets
Akron72e671f2020-08-04 11:35:40 +0200108 if (defined $_[0]->[O_START]) {
109 $pre .= '<i>' .$_[0]->[O_START] .
110 '<i>' . $_[0]->[O_END];
Nils Diewald6d565072014-10-30 23:20:58 +0000111 };
112
Akron3741f8b2016-12-21 19:55:21 +0100113 # my $pl = $_[0]->[1] ?
114 # $_[0]->[1] - 1 : $_[0]->[0];
Akron1622dd92015-12-09 22:34:26 +0100115
Akron72e671f2020-08-04 11:35:40 +0200116 if ($_[0]->[P_END] || $_[0]->[PAYLOAD]) {
Akron69a4a2f2016-01-17 12:55:50 +0100117
118 # p_end
Akron72e671f2020-08-04 11:35:40 +0200119 if (defined $_[0]->[P_END]) {
120 $pre .= '<i>' . $_[0]->[P_END];
Nils Diewald6d565072014-10-30 23:20:58 +0000121 };
Akron4701d092020-08-04 15:20:19 +0200122 if ($_[0]->[PAYLOAD]) {
Akron72e671f2020-08-04 11:35:40 +0200123 if (index($_[0]->[PAYLOAD], '<') == 0) {
124 $pre .= $_[0]->[PAYLOAD];
Nils Diewald6d565072014-10-30 23:20:58 +0000125 }
126 else {
Akron72e671f2020-08-04 11:35:40 +0200127 $pre .= '<?>' . $_[0]->[PAYLOAD];
Nils Diewald6d565072014-10-30 23:20:58 +0000128 };
129 };
130 };
131
Akrond69836b2015-12-10 00:40:20 +0100132 $string . ($pre ? '$' . $pre : '');
Nils Diewald6d565072014-10-30 23:20:58 +0000133};
134
135
Akron3741f8b2016-12-21 19:55:21 +0100136sub clone {
137 my $self = shift;
138 bless [@$self], __PACKAGE__;
139};
140
Nils Diewald2db9ad02013-10-29 19:26:43 +0000141
Nils Diewald6a2a14b2015-06-17 20:34:24 +0000142sub _escape_term ($) {
Akron740bb542020-08-04 16:06:03 +0200143 $_[0] =~ s/([\#\$\\])/\\$1/gr;
Nils Diewald6a2a14b2015-06-17 20:34:24 +0000144};
Nils Diewald6d565072014-10-30 23:20:58 +0000145
146
Nils Diewald2db9ad02013-10-29 19:26:43 +00001471;