• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GRAMMAR_DATES_PARSER_H_
18 #define LIBTEXTCLASSIFIER_ANNOTATOR_GRAMMAR_DATES_PARSER_H_
19 
20 #include <vector>
21 
22 #include "annotator/grammar/dates/annotations/annotation-options.h"
23 #include "annotator/grammar/dates/annotations/annotation.h"
24 #include "annotator/grammar/dates/dates_generated.h"
25 #include "annotator/grammar/dates/utils/date-match.h"
26 #include "utils/grammar/lexer.h"
27 #include "utils/grammar/rules-utils.h"
28 #include "utils/i18n/locale.h"
29 #include "utils/strings/stringpiece.h"
30 #include "utils/utf8/unilib.h"
31 
32 namespace libtextclassifier3::dates {
33 
34 // Parses datetime expressions in the input with the datetime grammar and
35 // constructs, validates, deduplicates and normalizes date time annotations.
36 class DateParser {
37  public:
DateParser(const UniLib * unilib,const DatetimeRules * datetime_rules)38   explicit DateParser(const UniLib* unilib, const DatetimeRules* datetime_rules)
39       : unilib_(*unilib),
40         lexer_(unilib, datetime_rules->rules()),
41         datetime_rules_(datetime_rules),
42         rules_locales_(ParseRulesLocales(datetime_rules->rules())) {}
43 
44   // Parses the dates in the input. Makes sure that the results do not
45   // overlap.
46   std::vector<DatetimeParseResultSpan> Parse(
47       StringPiece text, const std::vector<Token>& tokens,
48       const std::vector<Locale>& locales,
49       const DateAnnotationOptions& options) const;
50 
51  private:
52   const UniLib& unilib_;
53   const grammar::Lexer lexer_;
54 
55   // The datetime grammar.
56   const DatetimeRules* datetime_rules_;
57 
58   // Pre-parsed locales of the rules.
59   const std::vector<std::vector<Locale>> rules_locales_;
60 };
61 
62 }  // namespace libtextclassifier3::dates
63 
64 #endif  // LIBTEXTCLASSIFIER_ANNOTATOR_GRAMMAR_DATES_PARSER_H_
65