aboutsummaryrefslogtreecommitdiff
path: root/icing/tokenization/icu/icu-language-segmenter.cc
blob: 8e0f789dd6799d32f82871e6875983d93e28b84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "icing/tokenization/icu/icu-language-segmenter.h"

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "icing/text_classifier/lib3/utils/base/status.h"
#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/absl_ports/canonical_errors.h"
#include "icing/legacy/core/icing-string-util.h"
#include "icing/util/character-iterator.h"
#include "icing/util/i18n-utils.h"
#include "icing/util/status-macros.h"
#include "unicode/ubrk.h"
#include "unicode/uchar.h"
#include "unicode/umachine.h"

namespace icing {
namespace lib {

namespace {
constexpr char kASCIISpace = ' ';
}  // namespace

class IcuLanguageSegmenterIterator : public LanguageSegmenter::Iterator {
 public:
  // Factory function to create a segment iterator based on the given locale.
  //
  // Returns:
  //   An iterator on success
  //   INTERNAL_ERROR if unable to create
  static libtextclassifier3::StatusOr<
      std::unique_ptr<LanguageSegmenter::Iterator>>
  Create(std::string_view text, std::string_view locale) {
    std::unique_ptr<IcuLanguageSegmenterIterator> iterator(
        new IcuLanguageSegmenterIterator(text, locale));
    if (iterator->Initialize()) {
      return iterator;
    }
    return absl_ports::InternalError("Unable to create a term iterator");
  }

  ~IcuLanguageSegmenterIterator() {
    ubrk_close(break_iterator_);
    utext_close(u_text_);
  }

  // Advances to the next term. Returns false if it has reached the end.
  bool Advance() override {
    while (true) {
      // Prerequisite check
      if (term_end_index_exclusive_ == UBRK_DONE) {
        return false;
      }

      if (term_end_index_exclusive_ == 0) {
        // First Advance() call
        term_start_index_ = ubrk_first(break_iterator_);
      } else {
        term_start_index_ = term_end_index_exclusive_;
      }
      term_end_index_exclusive_ = ubrk_next(break_iterator_);

      // Reached the end
      if (term_end_index_exclusive_ == UBRK_DONE) {
        MarkAsDone();
        return false;
      }

      if (IsValidSegment()) {
        return true;
      }
    }
  }

  // Returns the current term. It can be called only when Advance() returns
  // true.
  std::string_view GetTerm() const override {
    int term_length = term_end_index_exclusive_ - term_start_index_;
    if (term_end_index_exclusive_ == UBRK_DONE) {
      term_length = 0;
    } else if (text_[term_start_index_] == kASCIISpace) {
      // Rule 3: multiple continuous whitespaces are treated as one.
      term_length = 1;
    }
    return text_.substr(term_start_index_, term_length);
  }

  libtextclassifier3::StatusOr<CharacterIterator> CalculateTermStart()
      override {
    if (!offset_iterator_.MoveToUtf8(term_start_index_)) {
      return absl_ports::AbortedError(
          "Could not retrieve valid utf8 character!");
    }
    return offset_iterator_;
  }

  libtextclassifier3::StatusOr<CharacterIterator> CalculateTermEndExclusive()
      override {
    if (!offset_iterator_.MoveToUtf8(term_end_index_exclusive_)) {
      return absl_ports::AbortedError(
          "Could not retrieve valid utf8 character!");
    }
    return offset_iterator_;
  }

  libtextclassifier3::StatusOr<int32_t> ResetToTermStartingAfterUtf32(
      int32_t offset) override {
    if (offset < 0) {
      // Very simple. The first term start after a negative offset is the first
      // term. So just reset to start and Advance.
      return ResetToStartUtf32();
    }

    // 1. Find the unicode character that contains the byte at offset.
    if (!offset_iterator_.MoveToUtf32(offset)) {
      // An error occurred. Mark as DONE
      if (offset_iterator_.utf8_index() != text_.length()) {
        // We returned false for some reason other than hitting the end. This is
        // a real error. Just return.
        MarkAsDone();
        return absl_ports::AbortedError(
            "Could not retrieve valid utf8 character!");
      }
    }
    if (offset_iterator_.utf8_index() == text_.length()) {
      return absl_ports::InvalidArgumentError(IcingStringUtil::StringPrintf(
          "Illegal offset provided! Offset utf-32:%d, utf-8:%d is not within "
          "bounds of string of length %zu",
          offset_iterator_.utf32_index(), offset_iterator_.utf8_index(),
          text_.length()));
    }

    // 2. We've got the unicode character containing byte offset. Now, we need
    // to point to the segment that starts after this character.
    int following_utf8_index =
        ubrk_following(break_iterator_, offset_iterator_.utf8_index());
    if (following_utf8_index == UBRK_DONE) {
      MarkAsDone();
      return absl_ports::NotFoundError(IcingStringUtil::StringPrintf(
          "No segments begin after provided offset %d.", offset));
    }
    term_end_index_exclusive_ = following_utf8_index;

    // 3. The term_end_exclusive_ points to the start of the term that we want
    // to return. We need to Advance so that term_start_ will now point to this
    // term.
    if (!Advance()) {
      return absl_ports::NotFoundError(IcingStringUtil::StringPrintf(
          "No segments begin after provided offset %d.", offset));
    }
    if (!offset_iterator_.MoveToUtf8(term_start_index_)) {
      return absl_ports::AbortedError(
          "Could not retrieve valid utf8 character!");
    }
    return offset_iterator_.utf32_index();
  }

  libtextclassifier3::StatusOr<int32_t> ResetToTermEndingBeforeUtf32(
      int32_t offset) override {
    if (offset < 0) {
      return absl_ports::InvalidArgumentError(IcingStringUtil::StringPrintf(
          "Illegal offset provided! Offset %d is not within bounds of string "
          "of length %zu",
          offset, text_.length()));
    }

    if (!offset_iterator_.MoveToUtf32(offset)) {
      // An error occurred. Mark as DONE
      if (offset_iterator_.utf8_index() != text_.length()) {
        // We returned false for some reason other than hitting the end. This is
        // a real error. Just return.
        MarkAsDone();
        return absl_ports::AbortedError(
            "Could not retrieve valid utf8 character!");
      }
      // If it returned false because we hit the end. Then that's fine. We'll
      // just treat it as if the request was for the end.
    }

    // 2. We've got the unicode character containing byte offset. Now, we need
    // to point to the segment that ends before this character.
    int starting_utf8_index =
        ubrk_preceding(break_iterator_, offset_iterator_.utf8_index());
    if (starting_utf8_index == UBRK_DONE) {
      // Rewind the end indices.
      MarkAsDone();
      return absl_ports::NotFoundError(IcingStringUtil::StringPrintf(
          "No segments end before provided offset %d.", offset));
    }
    term_start_index_ = starting_utf8_index;

    // 3. We've correctly set the start index and the iterator currently points
    // to that position. Now we need to find the correct end position and
    // advance the iterator to that position.
    int ending_utf8_index = ubrk_next(break_iterator_);
    if (ending_utf8_index == UBRK_DONE) {
      // This shouldn't ever happen.
      MarkAsDone();
      return absl_ports::AbortedError(IcingStringUtil::StringPrintf(
          "No segments end before provided offset %d.", offset));
    }
    term_end_index_exclusive_ = ending_utf8_index;

    // 4. The start and end indices point to a segment, but we need to ensure
    // that this segment is 1) valid and 2) ends before offset. Otherwise, we'll
    // need a segment prior to this one.
    CharacterIterator term_start_iterator = offset_iterator_;
    if (!term_start_iterator.MoveToUtf8(term_start_index_)) {
      return absl_ports::AbortedError(
          "Could not retrieve valid utf8 character!");
    }
    if (term_end_index_exclusive_ > offset_iterator_.utf8_index() ||
        !IsValidSegment()) {
      return ResetToTermEndingBeforeUtf32(term_start_iterator.utf32_index());
    }
    return term_start_iterator.utf32_index();
  }

  libtextclassifier3::StatusOr<int32_t> ResetToStartUtf32() override {
    term_start_index_ = 0;
    term_end_index_exclusive_ = 0;
    if (!Advance()) {
      return absl_ports::NotFoundError(
          "Unable to find any valid terms in text.");
    }
    if (!offset_iterator_.MoveToUtf8(term_start_index_)) {
      return absl_ports::AbortedError(
          "Could not retrieve valid utf8 character!");
    }
    return offset_iterator_.utf32_index();
  }

 private:
  explicit IcuLanguageSegmenterIterator(std::string_view text,
                                        std::string_view locale)
      : break_iterator_(nullptr),
        text_(text),
        locale_(locale),
        u_text_(nullptr),
        offset_iterator_(text),
        term_start_index_(0),
        term_end_index_exclusive_(0) {}

  // Returns true on success
  bool Initialize() {
    UErrorCode status = U_ZERO_ERROR;
    u_text_ = utext_openUTF8(nullptr, text_.data(), text_.length(), &status);
    if (u_text_ == nullptr) {
      return false;
    }
    break_iterator_ = ubrk_open(UBRK_WORD, locale_.data(), /*text=*/nullptr,
                                /*textLength=*/0, &status);
    ubrk_setUText(break_iterator_, u_text_, &status);
    return !U_FAILURE(status);
  }

  libtextclassifier3::Status ResetToTermStartingBefore(int32_t offset) {
    term_start_index_ = ubrk_preceding(break_iterator_, offset);
    if (term_start_index_ == UBRK_DONE) {
      MarkAsDone();
      return absl_ports::NotFoundError("");
    }
    term_end_index_exclusive_ = ubrk_next(break_iterator_);
    if (term_end_index_exclusive_ == UBRK_DONE) {
      MarkAsDone();
      return absl_ports::NotFoundError("");
    }
    return libtextclassifier3::Status::OK;
  }

  // Ensures that all members are consistent with the 'Done' state.
  // In the 'Done' state, term_start_index_ will point to the first character
  // and term_end_index_exclusive_ will be marked with the kDone value.
  // break_iterator_ may be in any state.
  void MarkAsDone() {
    term_end_index_exclusive_ = UBRK_DONE;
    term_start_index_ = 0;
  }

  bool IsValidSegment() const {
    // Rule 1: all ASCII terms will be returned.
    // We know it's a ASCII term by checking the first char.
    if (i18n_utils::IsAscii(text_[term_start_index_])) {
      return true;
    }

    UChar32 uchar32 = i18n_utils::GetUChar32At(text_.data(), text_.length(),
                                               term_start_index_);
    // Rule 2: for non-ASCII terms, only the alphanumeric terms are returned.
    // We know it's an alphanumeric term by checking the first unicode
    // character.
    if (i18n_utils::IsAlphaNumeric(uchar32)) {
      return true;
    }
    return false;
  }

  // The underlying class that does the segmentation, ubrk_close() must be
  // called after using.
  UBreakIterator* break_iterator_;

  // Text to be segmented
  std::string_view text_;

  // Locale of the input text, used to help segment more accurately. If a
  // wrong locale is set, text could probably still be segmented correctly
  // because the default break iterator behavior is used for most locales.
  std::string_view locale_;

  // A thin wrapper around the input UTF8 text, needed by break_iterator_.
  // Allocated by calling utext_openUtf8() and freed by calling utext_close().
  UText* u_text_;

  // Offset iterator. This iterator is not guaranteed to point to any particular
  // character, but is guaranteed to point to a valid UTF character sequence.
  //
  // This iterator is used to save some amount of linear traversal when seeking
  // to a specific UTF-32 offset. Each function that uses it could just create
  // a CharacterIterator starting at the beginning of the text and traverse
  // forward from there.
  CharacterIterator offset_iterator_;

  // The start and end indices are used to track the positions of current
  // term.
  int term_start_index_;
  int term_end_index_exclusive_;
};

IcuLanguageSegmenter::IcuLanguageSegmenter(std::string locale)
    : locale_(std::move(locale)) {}

libtextclassifier3::StatusOr<std::unique_ptr<LanguageSegmenter::Iterator>>
IcuLanguageSegmenter::Segment(const std::string_view text) const {
  return IcuLanguageSegmenterIterator::Create(text, locale_);
}

libtextclassifier3::StatusOr<std::vector<std::string_view>>
IcuLanguageSegmenter::GetAllTerms(const std::string_view text) const {
  ICING_ASSIGN_OR_RETURN(std::unique_ptr<LanguageSegmenter::Iterator> iterator,
                         Segment(text));
  std::vector<std::string_view> terms;
  while (iterator->Advance()) {
    terms.push_back(iterator->GetTerm());
  }
  return terms;
}

}  // namespace lib
}  // namespace icing