Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_SMARTSELECT_TOKEN_FEATURE_EXTRACTOR_H_ |
| 18 | #define LIBTEXTCLASSIFIER_SMARTSELECT_TOKEN_FEATURE_EXTRACTOR_H_ |
| 19 | |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 20 | #include <memory> |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 21 | #include <unordered_set> |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 24 | #include "smartselect/types.h" |
Lukas Zilka | 26e8c2e | 2017-04-06 15:54:24 +0200 | [diff] [blame] | 25 | #include "util/strings/stringpiece.h" |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 26 | #ifndef LIBTEXTCLASSIFIER_DISABLE_ICU_SUPPORT |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 27 | #include "unicode/regex.h" |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 28 | #endif |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 29 | |
| 30 | namespace libtextclassifier { |
| 31 | |
| 32 | struct TokenFeatureExtractorOptions { |
| 33 | // Number of buckets used for hashing charactergrams. |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 34 | int num_buckets = 0; |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 35 | |
| 36 | // Orders of charactergrams to extract. E.g., 2 means character bigrams, 3 |
| 37 | // character trigrams etc. |
| 38 | std::vector<int> chargram_orders; |
| 39 | |
| 40 | // Whether to extract the token case feature. |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 41 | bool extract_case_feature = false; |
| 42 | |
| 43 | // If true, will use the unicode-aware functionality for extracting features. |
| 44 | bool unicode_aware_features = false; |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 45 | |
| 46 | // Whether to extract the selection mask feature. |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 47 | bool extract_selection_mask_feature = false; |
| 48 | |
| 49 | // Regexp features to extract. |
| 50 | std::vector<std::string> regexp_features; |
| 51 | |
| 52 | // Whether to remap digits to a single number. |
| 53 | bool remap_digits = false; |
| 54 | |
Matt Sharifi | deb722d | 2017-04-24 13:30:47 +0200 | [diff] [blame] | 55 | // Whether to lowercase all tokens. |
| 56 | bool lowercase_tokens = false; |
| 57 | |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 58 | // Maximum length of a word. |
| 59 | int max_word_length = 20; |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 60 | |
| 61 | // List of allowed charactergrams. The extracted charactergrams are filtered |
| 62 | // using this list, and charactergrams that are not present are interpreted as |
| 63 | // out-of-vocabulary. |
| 64 | // If no allowed_chargrams are specified, all charactergrams are allowed. |
| 65 | std::unordered_set<std::string> allowed_chargrams; |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | class TokenFeatureExtractor { |
| 69 | public: |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 70 | explicit TokenFeatureExtractor(const TokenFeatureExtractorOptions& options); |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 71 | |
| 72 | // Extracts features from a token. |
Lukas Zilka | 6bb39a8 | 2017-04-07 19:55:11 +0200 | [diff] [blame] | 73 | // - is_in_span is a bool indicator whether the token is a part of the |
| 74 | // selection span (true) or not (false). |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 75 | // - sparse_features are indices into a sparse feature vector of size |
| 76 | // options.num_buckets which are set to 1.0 (others are implicitly 0.0). |
| 77 | // - dense_features are values of a dense feature vector of size 0-2 |
| 78 | // (depending on the options) for the token |
Lukas Zilka | 6bb39a8 | 2017-04-07 19:55:11 +0200 | [diff] [blame] | 79 | bool Extract(const Token& token, bool is_in_span, |
| 80 | std::vector<int>* sparse_features, |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 81 | std::vector<float>* dense_features) const; |
| 82 | |
Lukas Zilka | 6bb39a8 | 2017-04-07 19:55:11 +0200 | [diff] [blame] | 83 | int DenseFeaturesCount() const { |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 84 | int feature_count = |
| 85 | options_.extract_case_feature + options_.extract_selection_mask_feature; |
| 86 | #ifndef LIBTEXTCLASSIFIER_DISABLE_ICU_SUPPORT |
| 87 | feature_count += regex_patterns_.size(); |
| 88 | #else |
| 89 | if (enable_all_caps_feature_) { |
| 90 | feature_count += 1; |
| 91 | } |
| 92 | #endif |
| 93 | return feature_count; |
Lukas Zilka | 6bb39a8 | 2017-04-07 19:55:11 +0200 | [diff] [blame] | 94 | } |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | // Hashes given token to given number of buckets. |
Lukas Zilka | 26e8c2e | 2017-04-06 15:54:24 +0200 | [diff] [blame] | 98 | int HashToken(StringPiece token) const; |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 99 | |
| 100 | // Extracts the charactergram features from the token. |
| 101 | std::vector<int> ExtractCharactergramFeatures(const Token& token) const; |
| 102 | |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 103 | // Extracts the charactergram features from the token in a non-unicode-aware |
| 104 | // way. |
| 105 | std::vector<int> ExtractCharactergramFeaturesAscii(const Token& token) const; |
| 106 | |
| 107 | // Extracts the charactergram features from the token in a unicode-aware way. |
| 108 | std::vector<int> ExtractCharactergramFeaturesUnicode( |
| 109 | const Token& token) const; |
| 110 | |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 111 | private: |
| 112 | TokenFeatureExtractorOptions options_; |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 113 | #ifndef LIBTEXTCLASSIFIER_DISABLE_ICU_SUPPORT |
Lukas Zilka | d3bc59a | 2017-04-03 17:32:27 +0200 | [diff] [blame] | 114 | std::vector<std::unique_ptr<icu::RegexPattern>> regex_patterns_; |
Lukas Zilka | e5ea2ab | 2017-10-11 10:50:05 +0200 | [diff] [blame^] | 115 | #else |
| 116 | bool enable_all_caps_feature_ = false; |
| 117 | #endif |
Matt Sharifi | bda09f1 | 2017-03-10 12:29:15 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace libtextclassifier |
| 121 | |
| 122 | #endif // LIBTEXTCLASSIFIER_SMARTSELECT_TOKEN_FEATURE_EXTRACTOR_H_ |