lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * 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.
 */
 
#ifndef LIBTEXTCLASSIFIER_ANNOTATOR_NUMBER_NUMBER_H_
#define LIBTEXTCLASSIFIER_ANNOTATOR_NUMBER_NUMBER_H_
 
#include <string>
#include <unordered_set>
#include <vector>
 
#include "annotator/feature-processor.h"
#include "annotator/model_generated.h"
#include "annotator/types.h"
#include "utils/utf8/unicodetext.h"
 
namespace libtextclassifier3 {
 
// Annotator of numbers in text.
//
// Only supports values in range [-999 999 999, 999 999 999] (inclusive).
//
// TODO(zilka): Add support for non-ASCII digits.
// TODO(zilka): Add support for written-out numbers.
class NumberAnnotator {
 public:
  explicit NumberAnnotator(const NumberAnnotatorOptions* options,
                           const FeatureProcessor* feature_processor)
      : options_(options),
        feature_processor_(feature_processor),
        allowed_prefix_codepoints_(
            FlatbuffersVectorToSet(options->allowed_prefix_codepoints())),
        allowed_suffix_codepoints_(
            FlatbuffersVectorToSet(options->allowed_suffix_codepoints())) {}
 
  // Classifies given text, and if it is a number, it passes the result in
  // 'classification_result' and returns true, otherwise returns false.
  bool ClassifyText(const UnicodeText& context, CodepointSpan selection_indices,
                    AnnotationUsecase annotation_usecase,
                    ClassificationResult* classification_result) const;
 
  // Finds all number instances in the input text.
  bool FindAll(const UnicodeText& context_unicode,
               AnnotationUsecase annotation_usecase,
               std::vector<AnnotatedSpan>* result) const;
 
 private:
  static std::unordered_set<int> FlatbuffersVectorToSet(
      const flatbuffers::Vector<int32_t>* codepoints);
 
  // Parses the text to an int64 value and returns true if succeeded, otherwise
  // false. Also returns the number of prefix/suffix codepoints that were
  // stripped from the number.
  bool ParseNumber(const UnicodeText& text, int64* result,
                   int* num_prefix_codepoints,
                   int* num_suffix_codepoints) const;
 
  const NumberAnnotatorOptions* options_;
  const FeatureProcessor* feature_processor_;
  const std::unordered_set<int> allowed_prefix_codepoints_;
  const std::unordered_set<int> allowed_suffix_codepoints_;
};
 
}  // namespace libtextclassifier3
 
#endif  // LIBTEXTCLASSIFIER_ANNOTATOR_NUMBER_NUMBER_H_