• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "annotator/grammar/utils.h"
18 
19 #include "utils/grammar/utils/rules.h"
20 
21 namespace libtextclassifier3 {
22 namespace {
23 
24 using ::libtextclassifier3::GrammarModel_::RuleClassificationResultT;
25 
26 }  // namespace
27 
BuildTokenizer(const UniLib * unilib,const GrammarTokenizerOptions * options)28 Tokenizer BuildTokenizer(const UniLib* unilib,
29                          const GrammarTokenizerOptions* options) {
30   TC3_CHECK(options != nullptr);
31 
32   std::vector<const TokenizationCodepointRange*> codepoint_config;
33   if (options->tokenization_codepoint_config() != nullptr) {
34     codepoint_config.insert(codepoint_config.end(),
35                             options->tokenization_codepoint_config()->begin(),
36                             options->tokenization_codepoint_config()->end());
37   }
38   std::vector<const CodepointRange*> internal_codepoint_config;
39   if (options->internal_tokenizer_codepoint_ranges() != nullptr) {
40     internal_codepoint_config.insert(
41         internal_codepoint_config.end(),
42         options->internal_tokenizer_codepoint_ranges()->begin(),
43         options->internal_tokenizer_codepoint_ranges()->end());
44   }
45 
46   const bool tokenize_on_script_change =
47       options->tokenization_codepoint_config() != nullptr &&
48       options->tokenize_on_script_change();
49   return Tokenizer(options->tokenization_type(), unilib, codepoint_config,
50                    internal_codepoint_config, tokenize_on_script_change,
51                    /*icu_preserve_whitespace_tokens=*/false);
52 }
53 
AddRuleClassificationResult(const std::string & collection,const ModeFlag & enabled_modes,float priority_score,GrammarModelT * model)54 int AddRuleClassificationResult(const std::string& collection,
55                                 const ModeFlag& enabled_modes,
56                                 float priority_score, GrammarModelT* model) {
57   const int result_id = model->rule_classification_result.size();
58   model->rule_classification_result.emplace_back(new RuleClassificationResultT);
59   RuleClassificationResultT* result =
60       model->rule_classification_result.back().get();
61   result->collection_name = collection;
62   result->enabled_modes = enabled_modes;
63   result->priority_score = priority_score;
64   return result_id;
65 }
66 
67 }  // namespace libtextclassifier3
68