| Akron | 151676d | 2016-03-14 20:12:14 +0100 | [diff] [blame] | 1 | package KorAP::XML::Index::MultiTerm; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 2 | use strict; |
| 3 | use warnings; |
| Nils Diewald | ff6d078 | 2014-06-10 18:26:36 +0000 | [diff] [blame] | 4 | use MIME::Base64; |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 5 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 6 | use constant { |
| Akron | 39df7ce | 2020-08-04 15:55:26 +0200 | [diff] [blame] | 7 | TERM => 0, |
| 8 | O_START => 1, |
| 9 | O_END => 2, |
| 10 | P_START => 3, |
| 11 | P_END => 4, |
| Akron | 6a4cb16 | 2020-08-06 16:00:33 +0200 | [diff] [blame] | 12 | PTI => 5, |
| 13 | TUI => 6, |
| 14 | PAYLOAD => 7, |
| 15 | STORED_OFFSETS => 8, |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 16 | }; |
| 17 | |
| Akron | 129e441 | 2020-08-05 15:30:12 +0200 | [diff] [blame] | 18 | |
| 19 | # Construct a multiterm object by passing a term |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 20 | sub new { |
| Akron | 6a4cb16 | 2020-08-06 16:00:33 +0200 | [diff] [blame] | 21 | my $class = shift; |
| 22 | bless [@_], $class; |
| Akron | 4701d09 | 2020-08-04 15:20:19 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 25 | sub set_payload { |
| 26 | return $_[0]->[PAYLOAD] = $_[1]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 29 | sub get_payload { |
| 30 | $_[0]->[PAYLOAD]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 33 | sub set_p_start { |
| 34 | return $_[0]->[P_START] = $_[1]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 37 | sub get_p_start { |
| 38 | $_[0]->[P_START] // 0; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 41 | sub set_p_end { |
| 42 | $_[0]->[P_END] = $_[1]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 45 | sub get_p_end { |
| 46 | $_[0]->[P_END] // 0 |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 49 | sub set_o_start { |
| 50 | return $_[0]->[O_START] = $_[1]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 53 | sub get_o_start { |
| 54 | $_[0]->[O_START] // 0; |
| Akron | 1622dd9 | 2015-12-09 22:34:26 +0100 | [diff] [blame] | 55 | }; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 56 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 57 | sub set_o_end { |
| 58 | $_[0]->[O_END] = $_[1]; |
| Akron | 14ca9f0 | 2016-01-29 19:38:18 +0100 | [diff] [blame] | 59 | }; |
| Akron | 1622dd9 | 2015-12-09 22:34:26 +0100 | [diff] [blame] | 60 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 61 | sub get_o_end { |
| 62 | $_[0]->[O_END] // 0; |
| 63 | }; |
| 64 | |
| 65 | sub set_term { |
| 66 | return $_[0]->[TERM] = $_[1]; |
| 67 | }; |
| 68 | |
| 69 | sub get_term { |
| 70 | $_[0]->[TERM] // ''; |
| 71 | }; |
| 72 | |
| 73 | sub set_stored_offsets { |
| 74 | return $_[0]->[STORED_OFFSETS] = $_[1]; |
| 75 | }; |
| 76 | |
| 77 | sub get_stored_offsets { |
| 78 | $_[0]->[STORED_OFFSETS]; |
| 79 | }; |
| 80 | |
| 81 | sub set_pti { |
| 82 | return $_[0]->[PTI] = $_[1]; |
| 83 | }; |
| 84 | |
| 85 | sub get_pti { |
| 86 | $_[0]->[PTI]; |
| 87 | }; |
| 88 | |
| 89 | sub set_tui { |
| 90 | return $_[0]->[TUI] = $_[1]; |
| 91 | }; |
| 92 | |
| 93 | sub get_tui { |
| 94 | $_[0]->[TUI]; |
| 95 | }; |
| 96 | |
| 97 | |
| Akron | 1622dd9 | 2015-12-09 22:34:26 +0100 | [diff] [blame] | 98 | # To string based on array |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 99 | sub to_string { |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 100 | my $string = _escape_term($_[0]->[TERM]); |
| Nils Diewald | 6a2a14b | 2015-06-17 20:34:24 +0000 | [diff] [blame] | 101 | |
| Akron | d69836b | 2015-12-10 00:40:20 +0100 | [diff] [blame] | 102 | my $pre; |
| Akron | 1622dd9 | 2015-12-09 22:34:26 +0100 | [diff] [blame] | 103 | |
| 104 | # PTI |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 105 | $pre .= '<b>' . $_[0]->[PTI] if $_[0]->[PTI]; |
| Akron | 1622dd9 | 2015-12-09 22:34:26 +0100 | [diff] [blame] | 106 | |
| 107 | # Offsets |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 108 | if (defined $_[0]->[O_START]) { |
| 109 | $pre .= '<i>' .$_[0]->[O_START] . |
| 110 | '<i>' . $_[0]->[O_END]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 113 | # my $pl = $_[0]->[1] ? |
| 114 | # $_[0]->[1] - 1 : $_[0]->[0]; |
| Akron | 1622dd9 | 2015-12-09 22:34:26 +0100 | [diff] [blame] | 115 | |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 116 | if ($_[0]->[P_END] || $_[0]->[PAYLOAD]) { |
| Akron | 69a4a2f | 2016-01-17 12:55:50 +0100 | [diff] [blame] | 117 | |
| 118 | # p_end |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 119 | if (defined $_[0]->[P_END]) { |
| 120 | $pre .= '<i>' . $_[0]->[P_END]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 121 | }; |
| Akron | 4701d09 | 2020-08-04 15:20:19 +0200 | [diff] [blame] | 122 | if ($_[0]->[PAYLOAD]) { |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 123 | if (index($_[0]->[PAYLOAD], '<') == 0) { |
| 124 | $pre .= $_[0]->[PAYLOAD]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 125 | } |
| 126 | else { |
| Akron | 72e671f | 2020-08-04 11:35:40 +0200 | [diff] [blame] | 127 | $pre .= '<?>' . $_[0]->[PAYLOAD]; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 128 | }; |
| 129 | }; |
| 130 | }; |
| 131 | |
| Akron | d69836b | 2015-12-10 00:40:20 +0100 | [diff] [blame] | 132 | $string . ($pre ? '$' . $pre : ''); |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
| Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 136 | sub clone { |
| 137 | my $self = shift; |
| 138 | bless [@$self], __PACKAGE__; |
| 139 | }; |
| 140 | |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 141 | |
| Nils Diewald | 6a2a14b | 2015-06-17 20:34:24 +0000 | [diff] [blame] | 142 | sub _escape_term ($) { |
| Akron | 740bb54 | 2020-08-04 16:06:03 +0200 | [diff] [blame] | 143 | $_[0] =~ s/([\#\$\\])/\\$1/gr; |
| Nils Diewald | 6a2a14b | 2015-06-17 20:34:24 +0000 | [diff] [blame] | 144 | }; |
| Nils Diewald | 6d56507 | 2014-10-30 23:20:58 +0000 | [diff] [blame] | 145 | |
| 146 | |
| Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 147 | 1; |