blob: ef9a3df2bf764a4637618b3dc3ead418c0d2d97b [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
17// Feature processing for FFModel (feed-forward SmartSelection model).
18
19#ifndef LIBTEXTCLASSIFIER_SMARTSELECT_FEATURE_PROCESSOR_H_
20#define LIBTEXTCLASSIFIER_SMARTSELECT_FEATURE_PROCESSOR_H_
21
22#include <memory>
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +020023#include <set>
Matt Sharifibda09f12017-03-10 12:29:15 +010024#include <string>
25#include <vector>
26
Lukas Zilka6bb39a82017-04-07 19:55:11 +020027#include "smartselect/cached-features.h"
Matt Sharifibda09f12017-03-10 12:29:15 +010028#include "smartselect/text-classification-model.pb.h"
29#include "smartselect/token-feature-extractor.h"
30#include "smartselect/tokenizer.h"
31#include "smartselect/types.h"
Lukas Zilka6bb39a82017-04-07 19:55:11 +020032#include "util/base/logging.h"
Matt Sharifif95c3bd2017-04-25 18:41:11 +020033#include "util/utf8/unicodetext.h"
Matt Sharifibda09f12017-03-10 12:29:15 +010034
35namespace libtextclassifier {
36
37constexpr int kInvalidLabel = -1;
38
Lukas Zilka6bb39a82017-04-07 19:55:11 +020039// Maps a vector of sparse features and a vector of dense features to a vector
40// of features that combines both.
41// The output is written to the memory location pointed to by the last float*
42// argument.
43// Returns true on success false on failure.
44using FeatureVectorFn = std::function<bool(const std::vector<int>&,
45 const std::vector<float>&, float*)>;
46
Matt Sharifibda09f12017-03-10 12:29:15 +010047namespace internal {
48
49// Parses the serialized protocol buffer.
50FeatureProcessorOptions ParseSerializedOptions(
51 const std::string& serialized_options);
52
53TokenFeatureExtractorOptions BuildTokenFeatureExtractorOptions(
54 const FeatureProcessorOptions& options);
55
Matt Sharifibda09f12017-03-10 12:29:15 +010056// Splits tokens that contain the selection boundary inside them.
57// E.g. "foo{bar}@google.com" -> "foo", "bar", "@google.com"
58void SplitTokensOnSelectionBoundaries(CodepointSpan selection,
59 std::vector<Token>* tokens);
60
Matt Sharifibe876dc2017-03-17 17:02:43 +010061// Returns the index of token that corresponds to the codepoint span.
62int CenterTokenFromClick(CodepointSpan span, const std::vector<Token>& tokens);
63
64// Returns the index of token that corresponds to the middle of the codepoint
65// span.
66int CenterTokenFromMiddleOfSelection(
67 CodepointSpan span, const std::vector<Token>& selectable_tokens);
68
Lukas Zilka6bb39a82017-04-07 19:55:11 +020069// Strips the tokens from the tokens vector that are not used for feature
70// extraction because they are out of scope, or pads them so that there is
71// enough tokens in the required context_size for all inferences with a click
72// in relative_click_span.
73void StripOrPadTokens(TokenSpan relative_click_span, int context_size,
74 std::vector<Token>* tokens, int* click_pos);
75
Matt Sharifibda09f12017-03-10 12:29:15 +010076} // namespace internal
77
Lukas Zilka40c18de2017-04-10 17:22:22 +020078// Converts a codepoint span to a token span in the given list of tokens.
Lukas Zilka726b4d22017-12-13 16:37:03 +010079// If snap_boundaries_to_containing_tokens is set to true, it is enough for a
80// token to overlap with the codepoint range to be considered part of it.
81// Otherwise it must be fully included in the range.
82TokenSpan CodepointSpanToTokenSpan(
83 const std::vector<Token>& selectable_tokens, CodepointSpan codepoint_span,
84 bool snap_boundaries_to_containing_tokens = false);
Matt Sharifibda09f12017-03-10 12:29:15 +010085
Lukas Zilka40c18de2017-04-10 17:22:22 +020086// Converts a token span to a codepoint span in the given list of tokens.
87CodepointSpan TokenSpanToCodepointSpan(
88 const std::vector<Token>& selectable_tokens, TokenSpan token_span);
89
Lukas Zilka6bb39a82017-04-07 19:55:11 +020090// Takes care of preparing features for the span prediction model.
Matt Sharifibda09f12017-03-10 12:29:15 +010091class FeatureProcessor {
92 public:
93 explicit FeatureProcessor(const FeatureProcessorOptions& options)
Lukas Zilka6bb39a82017-04-07 19:55:11 +020094 : feature_extractor_(
Matt Sharifibda09f12017-03-10 12:29:15 +010095 internal::BuildTokenFeatureExtractorOptions(options)),
Lukas Zilka6bb39a82017-04-07 19:55:11 +020096 options_(options),
Matt Sharifibda09f12017-03-10 12:29:15 +010097 tokenizer_({options.tokenization_codepoint_config().begin(),
Lukas Zilka6bb39a82017-04-07 19:55:11 +020098 options.tokenization_codepoint_config().end()}) {
Matt Sharifibda09f12017-03-10 12:29:15 +010099 MakeLabelMaps();
Matt Sharifif95c3bd2017-04-25 18:41:11 +0200100 PrepareCodepointRanges({options.supported_codepoint_ranges().begin(),
101 options.supported_codepoint_ranges().end()},
102 &supported_codepoint_ranges_);
103 PrepareCodepointRanges(
104 {options.internal_tokenizer_codepoint_ranges().begin(),
105 options.internal_tokenizer_codepoint_ranges().end()},
106 &internal_tokenizer_codepoint_ranges_);
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +0200107 PrepareIgnoredSpanBoundaryCodepoints();
Matt Sharifibda09f12017-03-10 12:29:15 +0100108 }
109
110 explicit FeatureProcessor(const std::string& serialized_options)
111 : FeatureProcessor(internal::ParseSerializedOptions(serialized_options)) {
112 }
113
Matt Sharifibda09f12017-03-10 12:29:15 +0100114 // Tokenizes the input string using the selected tokenization method.
115 std::vector<Token> Tokenize(const std::string& utf8_text) const;
116
Matt Sharifibda09f12017-03-10 12:29:15 +0100117 // Converts a label into a token span.
118 bool LabelToTokenSpan(int label, TokenSpan* token_span) const;
119
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200120 // Gets the total number of selection labels.
121 int GetSelectionLabelCount() const { return label_to_selection_.size(); }
122
Matt Sharifibda09f12017-03-10 12:29:15 +0100123 // Gets the string value for given collection label.
124 std::string LabelToCollection(int label) const;
125
126 // Gets the total number of collections of the model.
127 int NumCollections() const { return collection_to_label_.size(); }
128
129 // Gets the name of the default collection.
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200130 std::string GetDefaultCollection() const;
131
132 const FeatureProcessorOptions& GetOptions() const { return options_; }
133
134 // Tokenizes the context and input span, and finds the click position.
135 void TokenizeAndFindClick(const std::string& context,
136 CodepointSpan input_span,
137 std::vector<Token>* tokens, int* click_pos) const;
138
139 // Extracts features as a CachedFeatures object that can be used for repeated
140 // inference over token spans in the given context.
Lukas Zilka726b4d22017-12-13 16:37:03 +0100141 // When input_span == {kInvalidIndex, kInvalidIndex} then, relative_click_span
142 // is ignored, and all tokens extracted from context will be considered.
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200143 bool ExtractFeatures(const std::string& context, CodepointSpan input_span,
144 TokenSpan relative_click_span,
145 const FeatureVectorFn& feature_vector_fn,
146 int feature_vector_size, std::vector<Token>* tokens,
147 int* click_pos,
148 std::unique_ptr<CachedFeatures>* cached_features) const;
149
150 // Fills selection_label_spans with CodepointSpans that correspond to the
151 // selection labels. The CodepointSpans are based on the codepoint ranges of
152 // given tokens.
153 bool SelectionLabelSpans(
154 VectorSpan<Token> tokens,
155 std::vector<CodepointSpan>* selection_label_spans) const;
156
157 int DenseFeaturesCount() const {
158 return feature_extractor_.DenseFeaturesCount();
Matt Sharifibda09f12017-03-10 12:29:15 +0100159 }
160
Lukas Zilka726b4d22017-12-13 16:37:03 +0100161 // Splits context to several segments according to configuration.
162 std::vector<UnicodeTextRange> SplitContext(
163 const UnicodeText& context_unicode) const;
164
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +0200165 // Strips boundary codepoints from the span in context and returns the new
166 // start and end indices. If the span comprises entirely of boundary
167 // codepoints, the first index of span is returned for both indices.
168 CodepointSpan StripBoundaryCodepoints(const std::string& context,
169 CodepointSpan span) const;
170
Matt Sharifibda09f12017-03-10 12:29:15 +0100171 protected:
Lukas Zilka26e8c2e2017-04-06 15:54:24 +0200172 // Represents a codepoint range [start, end).
173 struct CodepointRange {
174 int32 start;
175 int32 end;
176
177 CodepointRange(int32 arg_start, int32 arg_end)
178 : start(arg_start), end(arg_end) {}
179 };
180
Matt Sharifibda09f12017-03-10 12:29:15 +0100181 // Returns the class id corresponding to the given string collection
182 // identifier. There is a catch-all class id that the function returns for
183 // unknown collections.
184 int CollectionToLabel(const std::string& collection) const;
185
186 // Prepares mapping from collection names to labels.
187 void MakeLabelMaps();
188
189 // Gets the number of spannable tokens for the model.
190 //
191 // Spannable tokens are those tokens of context, which the model predicts
192 // selection spans over (i.e., there is 1:1 correspondence between the output
193 // classes of the model and each of the spannable tokens).
194 int GetNumContextTokens() const { return options_.context_size() * 2 + 1; }
195
196 // Converts a label into a span of codepoint indices corresponding to it
197 // given output_tokens.
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200198 bool LabelToSpan(int label, const VectorSpan<Token>& output_tokens,
Matt Sharifibda09f12017-03-10 12:29:15 +0100199 CodepointSpan* span) const;
200
201 // Converts a span to the corresponding label given output_tokens.
202 bool SpanToLabel(const std::pair<CodepointIndex, CodepointIndex>& span,
203 const std::vector<Token>& output_tokens, int* label) const;
204
205 // Converts a token span to the corresponding label.
206 int TokenSpanToLabel(const std::pair<TokenIndex, TokenIndex>& span) const;
207
Matt Sharifif95c3bd2017-04-25 18:41:11 +0200208 void PrepareCodepointRanges(
Lukas Zilka26e8c2e2017-04-06 15:54:24 +0200209 const std::vector<FeatureProcessorOptions::CodepointRange>&
Matt Sharifif95c3bd2017-04-25 18:41:11 +0200210 codepoint_ranges,
211 std::vector<CodepointRange>* prepared_codepoint_ranges);
Lukas Zilka26e8c2e2017-04-06 15:54:24 +0200212
213 // Returns the ratio of supported codepoints to total number of codepoints in
214 // the input context around given click position.
215 float SupportedCodepointsRatio(int click_pos,
216 const std::vector<Token>& tokens) const;
217
Matt Sharifif95c3bd2017-04-25 18:41:11 +0200218 // Returns true if given codepoint is covered by the given sorted vector of
219 // codepoint ranges.
220 bool IsCodepointInRanges(
221 int codepoint, const std::vector<CodepointRange>& codepoint_ranges) const;
Lukas Zilka26e8c2e2017-04-06 15:54:24 +0200222
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +0200223 void PrepareIgnoredSpanBoundaryCodepoints();
224
225 // Counts the number of span boundary codepoints. If count_from_beginning is
226 // True, the counting will start at the span_start iterator (inclusive) and at
227 // maximum end at span_end (exclusive). If count_from_beginning is True, the
228 // counting will start from span_end (exclusive) and end at span_start
229 // (inclusive).
230 int CountIgnoredSpanBoundaryCodepoints(
231 const UnicodeText::const_iterator& span_start,
232 const UnicodeText::const_iterator& span_end,
233 bool count_from_beginning) const;
234
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200235 // Finds the center token index in tokens vector, using the method defined
236 // in options_.
237 int FindCenterToken(CodepointSpan span,
238 const std::vector<Token>& tokens) const;
239
Lukas Zilka40c18de2017-04-10 17:22:22 +0200240 // Tokenizes the input text using ICU tokenizer.
241 bool ICUTokenize(const std::string& context,
242 std::vector<Token>* result) const;
243
Matt Sharifif95c3bd2017-04-25 18:41:11 +0200244 // Takes the result of ICU tokenization and retokenizes stretches of tokens
245 // made of a specific subset of characters using the internal tokenizer.
246 void InternalRetokenize(const std::string& context,
247 std::vector<Token>* tokens) const;
248
249 // Tokenizes a substring of the unicode string, appending the resulting tokens
250 // to the output vector. The resulting tokens have bounds relative to the full
251 // string. Does nothing if the start of the span is negative.
252 void TokenizeSubstring(const UnicodeText& unicode_text, CodepointSpan span,
253 std::vector<Token>* result) const;
254
Lukas Zilka726b4d22017-12-13 16:37:03 +0100255 // Removes all tokens from tokens that are not on a line (defined by calling
256 // SplitContext on the context) to which span points.
257 void StripTokensFromOtherLines(const std::string& context, CodepointSpan span,
258 std::vector<Token>* tokens) const;
259
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200260 const TokenFeatureExtractor feature_extractor_;
261
Matt Sharifif95c3bd2017-04-25 18:41:11 +0200262 // Codepoint ranges that define what codepoints are supported by the model.
263 // NOTE: Must be sorted.
264 std::vector<CodepointRange> supported_codepoint_ranges_;
265
266 // Codepoint ranges that define which tokens (consisting of which codepoints)
267 // should be re-tokenized with the internal tokenizer in the mixed
268 // tokenization mode.
269 // NOTE: Must be sorted.
270 std::vector<CodepointRange> internal_tokenizer_codepoint_ranges_;
271
Matt Sharifibda09f12017-03-10 12:29:15 +0100272 private:
Lukas Zilkae5ea2ab2017-10-11 10:50:05 +0200273 // Set of codepoints that will be stripped from beginning and end of
274 // predicted spans.
275 std::set<int32> ignored_span_boundary_codepoints_;
276
Lukas Zilka6bb39a82017-04-07 19:55:11 +0200277 const FeatureProcessorOptions options_;
Matt Sharifibda09f12017-03-10 12:29:15 +0100278
279 // Mapping between token selection spans and labels ids.
280 std::map<TokenSpan, int> selection_to_label_;
281 std::vector<TokenSpan> label_to_selection_;
282
283 // Mapping between collections and labels.
284 std::map<std::string, int> collection_to_label_;
285
286 Tokenizer tokenizer_;
Matt Sharifibda09f12017-03-10 12:29:15 +0100287};
288
289} // namespace libtextclassifier
290
291#endif // LIBTEXTCLASSIFIER_SMARTSELECT_FEATURE_PROCESSOR_H_