|
| 1 | +/**@constructor*/ |
| 2 | +BaseStemmer = function() { |
| 3 | + this.setCurrent = function(value) { |
| 4 | + this.current = value; |
| 5 | + this.cursor = 0; |
| 6 | + this.limit = this.current.length; |
| 7 | + this.limit_backward = 0; |
| 8 | + this.bra = this.cursor; |
| 9 | + this.ket = this.limit; |
| 10 | + }; |
| 11 | + |
| 12 | + this.getCurrent = function() { |
| 13 | + return this.current; |
| 14 | + }; |
| 15 | + |
| 16 | + this.copy_from = function(other) { |
| 17 | + this.current = other.current; |
| 18 | + this.cursor = other.cursor; |
| 19 | + this.limit = other.limit; |
| 20 | + this.limit_backward = other.limit_backward; |
| 21 | + this.bra = other.bra; |
| 22 | + this.ket = other.ket; |
| 23 | + }; |
| 24 | + |
| 25 | + this.in_grouping = function(s, min, max) { |
| 26 | + if (this.cursor >= this.limit) return false; |
| 27 | + var ch = this.current.charCodeAt(this.cursor); |
| 28 | + if (ch > max || ch < min) return false; |
| 29 | + ch -= min; |
| 30 | + if ((s[ch >>> 3] & (0x1 << (ch & 0x7))) == 0) return false; |
| 31 | + this.cursor++; |
| 32 | + return true; |
| 33 | + }; |
| 34 | + |
| 35 | + this.in_grouping_b = function(s, min, max) { |
| 36 | + if (this.cursor <= this.limit_backward) return false; |
| 37 | + var ch = this.current.charCodeAt(this.cursor - 1); |
| 38 | + if (ch > max || ch < min) return false; |
| 39 | + ch -= min; |
| 40 | + if ((s[ch >>> 3] & (0x1 << (ch & 0x7))) == 0) return false; |
| 41 | + this.cursor--; |
| 42 | + return true; |
| 43 | + }; |
| 44 | + |
| 45 | + this.out_grouping = function(s, min, max) { |
| 46 | + if (this.cursor >= this.limit) return false; |
| 47 | + var ch = this.current.charCodeAt(this.cursor); |
| 48 | + if (ch > max || ch < min) { |
| 49 | + this.cursor++; |
| 50 | + return true; |
| 51 | + } |
| 52 | + ch -= min; |
| 53 | + if ((s[ch >>> 3] & (0X1 << (ch & 0x7))) == 0) { |
| 54 | + this.cursor++; |
| 55 | + return true; |
| 56 | + } |
| 57 | + return false; |
| 58 | + }; |
| 59 | + |
| 60 | + this.out_grouping_b = function(s, min, max) { |
| 61 | + if (this.cursor <= this.limit_backward) return false; |
| 62 | + var ch = this.current.charCodeAt(this.cursor - 1); |
| 63 | + if (ch > max || ch < min) { |
| 64 | + this.cursor--; |
| 65 | + return true; |
| 66 | + } |
| 67 | + ch -= min; |
| 68 | + if ((s[ch >>> 3] & (0x1 << (ch & 0x7))) == 0) { |
| 69 | + this.cursor--; |
| 70 | + return true; |
| 71 | + } |
| 72 | + return false; |
| 73 | + }; |
| 74 | + |
| 75 | + this.eq_s = function(s) |
| 76 | + { |
| 77 | + if (this.limit - this.cursor < s.length) return false; |
| 78 | + if (this.current.slice(this.cursor, this.cursor + s.length) != s) |
| 79 | + { |
| 80 | + return false; |
| 81 | + } |
| 82 | + this.cursor += s.length; |
| 83 | + return true; |
| 84 | + }; |
| 85 | + |
| 86 | + this.eq_s_b = function(s) |
| 87 | + { |
| 88 | + if (this.cursor - this.limit_backward < s.length) return false; |
| 89 | + if (this.current.slice(this.cursor - s.length, this.cursor) != s) |
| 90 | + { |
| 91 | + return false; |
| 92 | + } |
| 93 | + this.cursor -= s.length; |
| 94 | + return true; |
| 95 | + }; |
| 96 | + |
| 97 | + /** @return {number} */ this.find_among = function(v) |
| 98 | + { |
| 99 | + var i = 0; |
| 100 | + var j = v.length; |
| 101 | + |
| 102 | + var c = this.cursor; |
| 103 | + var l = this.limit; |
| 104 | + |
| 105 | + var common_i = 0; |
| 106 | + var common_j = 0; |
| 107 | + |
| 108 | + var first_key_inspected = false; |
| 109 | + |
| 110 | + while (true) |
| 111 | + { |
| 112 | + var k = i + ((j - i) >>> 1); |
| 113 | + var diff = 0; |
| 114 | + var common = common_i < common_j ? common_i : common_j; // smaller |
| 115 | + // w[0]: string, w[1]: substring_i, w[2]: result, w[3]: function (optional) |
| 116 | + var w = v[k]; |
| 117 | + var i2; |
| 118 | + for (i2 = common; i2 < w[0].length; i2++) |
| 119 | + { |
| 120 | + if (c + common == l) |
| 121 | + { |
| 122 | + diff = -1; |
| 123 | + break; |
| 124 | + } |
| 125 | + diff = this.current.charCodeAt(c + common) - w[0].charCodeAt(i2); |
| 126 | + if (diff != 0) break; |
| 127 | + common++; |
| 128 | + } |
| 129 | + if (diff < 0) |
| 130 | + { |
| 131 | + j = k; |
| 132 | + common_j = common; |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + i = k; |
| 137 | + common_i = common; |
| 138 | + } |
| 139 | + if (j - i <= 1) |
| 140 | + { |
| 141 | + if (i > 0) break; // v->s has been inspected |
| 142 | + if (j == i) break; // only one item in v |
| 143 | + |
| 144 | + // - but now we need to go round once more to get |
| 145 | + // v->s inspected. This looks messy, but is actually |
| 146 | + // the optimal approach. |
| 147 | + |
| 148 | + if (first_key_inspected) break; |
| 149 | + first_key_inspected = true; |
| 150 | + } |
| 151 | + } |
| 152 | + do { |
| 153 | + var w = v[i]; |
| 154 | + if (common_i >= w[0].length) |
| 155 | + { |
| 156 | + this.cursor = c + w[0].length; |
| 157 | + if (w.length < 4) return w[2]; |
| 158 | + var res = w[3](this); |
| 159 | + this.cursor = c + w[0].length; |
| 160 | + if (res) return w[2]; |
| 161 | + } |
| 162 | + i = w[1]; |
| 163 | + } while (i >= 0); |
| 164 | + return 0; |
| 165 | + }; |
| 166 | + |
| 167 | + // find_among_b is for backwards processing. Same comments apply |
| 168 | + this.find_among_b = function(v) |
| 169 | + { |
| 170 | + var i = 0; |
| 171 | + var j = v.length |
| 172 | + |
| 173 | + var c = this.cursor; |
| 174 | + var lb = this.limit_backward; |
| 175 | + |
| 176 | + var common_i = 0; |
| 177 | + var common_j = 0; |
| 178 | + |
| 179 | + var first_key_inspected = false; |
| 180 | + |
| 181 | + while (true) |
| 182 | + { |
| 183 | + var k = i + ((j - i) >> 1); |
| 184 | + var diff = 0; |
| 185 | + var common = common_i < common_j ? common_i : common_j; |
| 186 | + var w = v[k]; |
| 187 | + var i2; |
| 188 | + for (i2 = w[0].length - 1 - common; i2 >= 0; i2--) |
| 189 | + { |
| 190 | + if (c - common == lb) |
| 191 | + { |
| 192 | + diff = -1; |
| 193 | + break; |
| 194 | + } |
| 195 | + diff = this.current.charCodeAt(c - 1 - common) - w[0].charCodeAt(i2); |
| 196 | + if (diff != 0) break; |
| 197 | + common++; |
| 198 | + } |
| 199 | + if (diff < 0) |
| 200 | + { |
| 201 | + j = k; |
| 202 | + common_j = common; |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + i = k; |
| 207 | + common_i = common; |
| 208 | + } |
| 209 | + if (j - i <= 1) |
| 210 | + { |
| 211 | + if (i > 0) break; |
| 212 | + if (j == i) break; |
| 213 | + if (first_key_inspected) break; |
| 214 | + first_key_inspected = true; |
| 215 | + } |
| 216 | + } |
| 217 | + do { |
| 218 | + var w = v[i]; |
| 219 | + if (common_i >= w[0].length) |
| 220 | + { |
| 221 | + this.cursor = c - w[0].length; |
| 222 | + if (w.length < 4) return w[2]; |
| 223 | + var res = w[3](this); |
| 224 | + this.cursor = c - w[0].length; |
| 225 | + if (res) return w[2]; |
| 226 | + } |
| 227 | + i = w[1]; |
| 228 | + } while (i >= 0); |
| 229 | + return 0; |
| 230 | + }; |
| 231 | + |
| 232 | + /* to replace chars between c_bra and c_ket in this.current by the |
| 233 | + * chars in s. |
| 234 | + */ |
| 235 | + this.replace_s = function(c_bra, c_ket, s) |
| 236 | + { |
| 237 | + var adjustment = s.length - (c_ket - c_bra); |
| 238 | + this.current = this.current.slice(0, c_bra) + s + this.current.slice(c_ket); |
| 239 | + this.limit += adjustment; |
| 240 | + if (this.cursor >= c_ket) this.cursor += adjustment; |
| 241 | + else if (this.cursor > c_bra) this.cursor = c_bra; |
| 242 | + return adjustment; |
| 243 | + }; |
| 244 | + |
| 245 | + this.slice_check = function() |
| 246 | + { |
| 247 | + if (this.bra < 0 || |
| 248 | + this.bra > this.ket || |
| 249 | + this.ket > this.limit || |
| 250 | + this.limit > this.current.length) |
| 251 | + { |
| 252 | + return false; |
| 253 | + } |
| 254 | + return true; |
| 255 | + }; |
| 256 | + |
| 257 | + this.slice_from = function(s) |
| 258 | + { |
| 259 | + var result = false; |
| 260 | + if (this.slice_check()) |
| 261 | + { |
| 262 | + this.replace_s(this.bra, this.ket, s); |
| 263 | + result = true; |
| 264 | + } |
| 265 | + return result; |
| 266 | + }; |
| 267 | + |
| 268 | + this.slice_del = function() |
| 269 | + { |
| 270 | + return this.slice_from(""); |
| 271 | + }; |
| 272 | + |
| 273 | + this.insert = function(c_bra, c_ket, s) |
| 274 | + { |
| 275 | + var adjustment = this.replace_s(c_bra, c_ket, s); |
| 276 | + if (c_bra <= this.bra) this.bra += adjustment; |
| 277 | + if (c_bra <= this.ket) this.ket += adjustment; |
| 278 | + }; |
| 279 | + |
| 280 | + this.slice_to = function() |
| 281 | + { |
| 282 | + var result = ''; |
| 283 | + if (this.slice_check()) |
| 284 | + { |
| 285 | + result = this.current.slice(this.bra, this.ket); |
| 286 | + } |
| 287 | + return result; |
| 288 | + }; |
| 289 | + |
| 290 | + this.assign_to = function() |
| 291 | + { |
| 292 | + return this.current.slice(0, this.limit); |
| 293 | + }; |
| 294 | +}; |
0 commit comments