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_ANNOTATOR_DATETIME_GRAMMAR_PARSER_H_ 18 #define LIBTEXTCLASSIFIER_ANNOTATOR_DATETIME_GRAMMAR_PARSER_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include "annotator/datetime/datetime-grounder.h" 24 #include "annotator/datetime/parser.h" 25 #include "annotator/model_generated.h" 26 #include "annotator/types.h" 27 #include "utils/base/statusor.h" 28 #include "utils/grammar/analyzer.h" 29 #include "utils/i18n/locale-list.h" 30 #include "utils/utf8/unicodetext.h" 31 32 namespace libtextclassifier3 { 33 34 // Parses datetime expressions in the input and resolves them to actual absolute 35 // time. 36 class GrammarDatetimeParser : public DatetimeParser { 37 public: 38 explicit GrammarDatetimeParser(const grammar::Analyzer& analyzer, 39 const DatetimeGrounder& datetime_grounder, 40 const float target_classification_score, 41 const float priority_score, 42 ModeFlag enabled_modes); 43 44 // Parses the dates in 'input' and fills result. Makes sure that the results 45 // do not overlap. 46 // If 'anchor_start_end' is true the extracted results need to start at the 47 // beginning of 'input' and end at the end of it. 48 StatusOr<std::vector<DatetimeParseResultSpan>> Parse( 49 const std::string& input, int64 reference_time_ms_utc, 50 const std::string& reference_timezone, const LocaleList& locale_list, 51 ModeFlag mode, AnnotationUsecase annotation_usecase, 52 bool anchor_start_end) const override; 53 54 // Same as above but takes UnicodeText. 55 StatusOr<std::vector<DatetimeParseResultSpan>> Parse( 56 const UnicodeText& input, int64 reference_time_ms_utc, 57 const std::string& reference_timezone, const LocaleList& locale_list, 58 ModeFlag mode, AnnotationUsecase annotation_usecase, 59 bool anchor_start_end) const override; 60 61 private: 62 const grammar::Analyzer& analyzer_; 63 const DatetimeGrounder& datetime_grounder_; 64 const float target_classification_score_; 65 const float priority_score_; 66 const ModeFlag enabled_modes_; 67 }; 68 69 } // namespace libtextclassifier3 70 71 #endif // LIBTEXTCLASSIFIER_ANNOTATOR_DATETIME_GRAMMAR_PARSER_H_ 72