1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_ANNOTATOR_VOCAB_VOCAB_ANNOTATOR_IMPL_H_
18 #define LIBTEXTCLASSIFIER_ANNOTATOR_VOCAB_VOCAB_ANNOTATOR_IMPL_H_
19 
20 #include "annotator/feature-processor.h"
21 #include "annotator/model_generated.h"
22 #include "annotator/types.h"
23 #include "annotator/vocab/vocab-level-table.h"
24 #include "utils/i18n/locale.h"
25 #include "utils/utf8/unicodetext.h"
26 #include "utils/utf8/unilib.h"
27 
28 namespace libtextclassifier3 {
29 
30 // Annotates vocabs of different levels which users may want to look them up
31 // in a dictionary.
32 class VocabAnnotator {
33  public:
34   static std::unique_ptr<VocabAnnotator> Create(
35       const VocabModel *model, const FeatureProcessor &feature_processor,
36       const UniLib &unilib);
37 
38   bool Annotate(const UnicodeText &context,
39                 const std::vector<Locale> detected_text_language_tags,
40                 bool trigger_on_beginner_words,
41                 std::vector<AnnotatedSpan> *results) const;
42 
43   bool ClassifyText(const UnicodeText &context, CodepointSpan click,
44                     const std::vector<Locale> detected_text_language_tags,
45                     bool trigger_on_beginner_words,
46                     ClassificationResult *result) const;
47 
48  private:
49   explicit VocabAnnotator(std::unique_ptr<VocabLevelTable> vocab_level_table,
50                           const std::vector<Locale> &triggering_locales,
51                           const FeatureProcessor &feature_processor,
52                           const UniLib &unilib, const VocabModel *model);
53 
54   bool ClassifyTextInternal(
55       const UnicodeText &context, const CodepointSpan click,
56       const std::vector<Locale> detected_text_language_tags,
57       bool trigger_on_beginner_words,
58       ClassificationResult *classification_result,
59       CodepointSpan *classified_span) const;
60   bool ShouldTriggerOnBeginnerVocabs() const;
61 
62   const std::unique_ptr<VocabLevelTable> vocab_level_table_;
63   // Locales for which this annotator triggers.
64   const std::vector<Locale> triggering_locales_;
65   const FeatureProcessor &feature_processor_;
66   const UniLib &unilib_;
67   const VocabModel *model_;
68 };
69 
70 }  // namespace libtextclassifier3
71 
72 #endif  // LIBTEXTCLASSIFIER_ANNOTATOR_VOCAB_VOCAB_ANNOTATOR_IMPL_H_
73