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_UTILS_TOKEN_FEATURE_EXTRACTOR_H_ 18 #define LIBTEXTCLASSIFIER_UTILS_TOKEN_FEATURE_EXTRACTOR_H_ 19 20 #include <memory> 21 #include <unordered_set> 22 #include <vector> 23 24 #include "annotator/types.h" 25 #include "utils/strings/stringpiece.h" 26 #include "utils/utf8/unilib.h" 27 28 namespace libtextclassifier3 { 29 30 struct TokenFeatureExtractorOptions { 31 // Number of buckets used for hashing charactergrams. 32 int num_buckets = 0; 33 34 // Orders of charactergrams to extract. E.g., 2 means character bigrams, 3 35 // character trigrams etc. 36 std::vector<int> chargram_orders; 37 38 // Whether to extract the token case feature. 39 bool extract_case_feature = false; 40 41 // If true, will use the unicode-aware functionality for extracting features. 42 bool unicode_aware_features = false; 43 44 // Whether to extract the selection mask feature. 45 bool extract_selection_mask_feature = false; 46 47 // Regexp features to extract. 48 std::vector<std::string> regexp_features; 49 50 // Whether to remap digits to a single number. 51 bool remap_digits = false; 52 53 // Whether to lowercase all tokens. 54 bool lowercase_tokens = false; 55 56 // Maximum length of a word. 57 int max_word_length = 20; 58 59 // List of allowed charactergrams. The extracted charactergrams are filtered 60 // using this list, and charactergrams that are not present are interpreted as 61 // out-of-vocabulary. 62 // If no allowed_chargrams are specified, all charactergrams are allowed. 63 std::unordered_set<std::string> allowed_chargrams; 64 }; 65 66 class TokenFeatureExtractor { 67 public: 68 // Des not take ownership of unilib, which must refer to a valid unilib 69 // instance that outlives this feature extractor. 70 explicit TokenFeatureExtractor(const TokenFeatureExtractorOptions& options, 71 const UniLib* unilib); 72 73 // Extracts both the sparse (charactergram) and the dense features from a 74 // token. is_in_span is a bool indicator whether the token is a part of the 75 // selection span (true) or not (false). 76 // The sparse_features output is optional. Fails and returns false if 77 // dense_fatures in a nullptr. 78 bool Extract(const Token& token, bool is_in_span, 79 std::vector<int>* sparse_features, 80 std::vector<float>* dense_features) const; 81 82 // Extracts the sparse (charactergram) features from the token. 83 std::vector<int> ExtractCharactergramFeatures(const Token& token) const; 84 85 // Extracts the dense features from the token. is_in_span is a bool indicator 86 // whether the token is a part of the selection span (true) or not (false). 87 std::vector<float> ExtractDenseFeatures(const Token& token, 88 bool is_in_span) const; 89 DenseFeaturesCount()90 int DenseFeaturesCount() const { 91 int feature_count = 92 options_.extract_case_feature + options_.extract_selection_mask_feature; 93 feature_count += regex_patterns_.size(); 94 return feature_count; 95 } 96 97 protected: 98 // Hashes given token to given number of buckets. 99 int HashToken(StringPiece token) const; 100 101 // Extracts the charactergram features from the token in a non-unicode-aware 102 // way. 103 std::vector<int> ExtractCharactergramFeaturesAscii(const Token& token) const; 104 105 // Extracts the charactergram features from the token in a unicode-aware way. 106 std::vector<int> ExtractCharactergramFeaturesUnicode( 107 const Token& token) const; 108 109 private: 110 TokenFeatureExtractorOptions options_; 111 std::vector<std::unique_ptr<UniLib::RegexPattern>> regex_patterns_; 112 const UniLib& unilib_; 113 }; 114 115 } // namespace libtextclassifier3 116 117 #endif // LIBTEXTCLASSIFIER_UTILS_TOKEN_FEATURE_EXTRACTOR_H_ 118