1 // Copyright (C) 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "icing/tokenization/tokenizer-factory.h" 16 17 #include <memory> 18 19 #include "icing/text_classifier/lib3/utils/base/statusor.h" 20 #include "icing/absl_ports/canonical_errors.h" 21 #include "icing/proto/schema.pb.h" 22 #include "icing/tokenization/language-segmenter.h" 23 #include "icing/tokenization/plain-tokenizer.h" 24 #include "icing/tokenization/raw-query-tokenizer.h" 25 #include "icing/tokenization/tokenizer.h" 26 #include "icing/util/status-macros.h" 27 28 namespace icing { 29 namespace lib { 30 31 namespace tokenizer_factory { 32 33 libtextclassifier3::StatusOr<std::unique_ptr<Tokenizer>> CreateIndexingTokenizer(StringIndexingConfig::TokenizerType::Code type,const LanguageSegmenter * lang_segmenter)34CreateIndexingTokenizer(StringIndexingConfig::TokenizerType::Code type, 35 const LanguageSegmenter* lang_segmenter) { 36 ICING_RETURN_ERROR_IF_NULL(lang_segmenter); 37 38 switch (type) { 39 case StringIndexingConfig::TokenizerType::PLAIN: 40 return std::make_unique<PlainTokenizer>(lang_segmenter); 41 case StringIndexingConfig::TokenizerType::NONE: 42 [[fallthrough]]; 43 default: 44 // This should never happen. 45 return absl_ports::InvalidArgumentError( 46 "Invalid tokenizer type for an indexed section"); 47 } 48 } 49 CreateQueryTokenizer(QueryTokenizerType query_tokenizer_type,const LanguageSegmenter * lang_segmenter)50libtextclassifier3::StatusOr<std::unique_ptr<Tokenizer>> CreateQueryTokenizer( 51 QueryTokenizerType query_tokenizer_type, 52 const LanguageSegmenter* lang_segmenter) { 53 ICING_RETURN_ERROR_IF_NULL(lang_segmenter); 54 55 switch (query_tokenizer_type) { 56 case RAW_QUERY: 57 return std::make_unique<RawQueryTokenizer>(lang_segmenter); 58 default: 59 // This should never happen. 60 return absl_ports::InvalidArgumentError( 61 "Invalid tokenizer type for query"); 62 } 63 } 64 65 } // namespace tokenizer_factory 66 67 } // namespace lib 68 } // namespace icing 69