blob: fee1355dcff60c52896c58542824b5a9e49249f0 [file] [log] [blame]
Matt Sharifibda09f12017-03-10 12:29:15 +01001/*
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
Lukas Zilkab23e2122018-02-09 10:25:19 +010017#ifndef LIBTEXTCLASSIFIER_TOKEN_FEATURE_EXTRACTOR_H_
18#define LIBTEXTCLASSIFIER_TOKEN_FEATURE_EXTRACTOR_H_
Matt Sharifibda09f12017-03-10 12:29:15 +010019
Lukas Zilkad3bc59a2017-04-03 17:32:27 +020020#include <memory>
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +020021#include <unordered_set>
Matt Sharifibda09f12017-03-10 12:29:15 +010022#include <vector>
23
Lukas Zilka21d8c982018-01-24 11:11:20 +010024#include "types.h"
Lukas Zilka26e8c2e2017-04-06 15:54:24 +020025#include "util/strings/stringpiece.h"
Lukas Zilka21d8c982018-01-24 11:11:20 +010026#include "util/utf8/unilib.h"
Matt Sharifibda09f12017-03-10 12:29:15 +010027
Lukas Zilka21d8c982018-01-24 11:11:20 +010028namespace libtextclassifier2 {
Matt Sharifibda09f12017-03-10 12:29:15 +010029
30struct TokenFeatureExtractorOptions {
31 // Number of buckets used for hashing charactergrams.
Lukas Zilkad3bc59a2017-04-03 17:32:27 +020032 int num_buckets = 0;
Matt Sharifibda09f12017-03-10 12:29:15 +010033
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.
Lukas Zilkad3bc59a2017-04-03 17:32:27 +020039 bool extract_case_feature = false;
40
41 // If true, will use the unicode-aware functionality for extracting features.
42 bool unicode_aware_features = false;
Matt Sharifibda09f12017-03-10 12:29:15 +010043
44 // Whether to extract the selection mask feature.
Lukas Zilkad3bc59a2017-04-03 17:32:27 +020045 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
Matt Sharifideb722d2017-04-24 13:30:47 +020053 // Whether to lowercase all tokens.
54 bool lowercase_tokens = false;
55
Lukas Zilkad3bc59a2017-04-03 17:32:27 +020056 // Maximum length of a word.
57 int max_word_length = 20;
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +020058
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;
Matt Sharifibda09f12017-03-10 12:29:15 +010064};
65
66class TokenFeatureExtractor {
67 public:
Lukas Zilka21d8c982018-01-24 11:11:20 +010068 TokenFeatureExtractor(const TokenFeatureExtractorOptions& options,
69 const UniLib& unilib);
Matt Sharifibda09f12017-03-10 12:29:15 +010070
Lukas Zilkab23e2122018-02-09 10:25:19 +010071 // Extracts both the sparse (charactergram) and the dense features from a
72 // token. is_in_span is a bool indicator whether the token is a part of the
73 // selection span (true) or not (false).
Lukas Zilkaba849e72018-03-08 14:48:21 +010074 // The sparse_features output is optional. Fails and returns false if
75 // dense_fatures in a nullptr.
Lukas Zilka6bb39a82017-04-07 19:55:11 +020076 bool Extract(const Token& token, bool is_in_span,
77 std::vector<int>* sparse_features,
Matt Sharifibda09f12017-03-10 12:29:15 +010078 std::vector<float>* dense_features) const;
79
Lukas Zilkab23e2122018-02-09 10:25:19 +010080 // Extracts the sparse (charactergram) features from the token.
81 std::vector<int> ExtractCharactergramFeatures(const Token& token) const;
82
83 // Extracts the dense features from the token. is_in_span is a bool indicator
84 // whether the token is a part of the selection span (true) or not (false).
85 std::vector<float> ExtractDenseFeatures(const Token& token,
86 bool is_in_span) const;
87
Lukas Zilka6bb39a82017-04-07 19:55:11 +020088 int DenseFeaturesCount() const {
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +020089 int feature_count =
90 options_.extract_case_feature + options_.extract_selection_mask_feature;
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +020091 feature_count += regex_patterns_.size();
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +020092 return feature_count;
Lukas Zilka6bb39a82017-04-07 19:55:11 +020093 }
Matt Sharifibda09f12017-03-10 12:29:15 +010094
95 protected:
96 // Hashes given token to given number of buckets.
Lukas Zilka26e8c2e2017-04-06 15:54:24 +020097 int HashToken(StringPiece token) const;
Matt Sharifibda09f12017-03-10 12:29:15 +010098
Lukas Zilkad3bc59a2017-04-03 17:32:27 +020099 // Extracts the charactergram features from the token in a non-unicode-aware
100 // way.
101 std::vector<int> ExtractCharactergramFeaturesAscii(const Token& token) const;
102
103 // Extracts the charactergram features from the token in a unicode-aware way.
104 std::vector<int> ExtractCharactergramFeaturesUnicode(
105 const Token& token) const;
106
Matt Sharifibda09f12017-03-10 12:29:15 +0100107 private:
108 TokenFeatureExtractorOptions options_;
Lukas Zilka21d8c982018-01-24 11:11:20 +0100109 std::vector<std::unique_ptr<UniLib::RegexPattern>> regex_patterns_;
110 const UniLib& unilib_;
Matt Sharifibda09f12017-03-10 12:29:15 +0100111};
112
Lukas Zilka21d8c982018-01-24 11:11:20 +0100113} // namespace libtextclassifier2
Matt Sharifibda09f12017-03-10 12:29:15 +0100114
Lukas Zilkab23e2122018-02-09 10:25:19 +0100115#endif // LIBTEXTCLASSIFIER_TOKEN_FEATURE_EXTRACTOR_H_