• 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 #include "annotator/datetime/utils.h"
18 
19 namespace libtextclassifier3 {
20 
FillInterpretations(const DatetimeParsedData & parse,const DatetimeGranularity & granularity,std::vector<DatetimeParsedData> * interpretations)21 void FillInterpretations(const DatetimeParsedData& parse,
22                          const DatetimeGranularity& granularity,
23                          std::vector<DatetimeParsedData>* interpretations) {
24   DatetimeParsedData modified_parse(parse);
25   // If the relation field is not set, but relation_type field *is*, assume
26   // the relation field is NEXT_OR_SAME. This is necessary to handle e.g.
27   // "monday 3pm" (otherwise only "this monday 3pm" would work).
28   if (parse.HasFieldType(DatetimeComponent::ComponentType::DAY_OF_WEEK)) {
29     DatetimeComponent::RelativeQualifier relative_value;
30     if (parse.GetRelativeValue(DatetimeComponent::ComponentType::DAY_OF_WEEK,
31                                &relative_value)) {
32       if (relative_value == DatetimeComponent::RelativeQualifier::UNSPECIFIED) {
33         modified_parse.SetRelativeValue(
34             DatetimeComponent::ComponentType::DAY_OF_WEEK,
35             DatetimeComponent::RelativeQualifier::THIS);
36       }
37     }
38   }
39 
40   // Multiple interpretations of ambiguous datetime expressions are generated
41   // here.
42   if (granularity > DatetimeGranularity::GRANULARITY_DAY &&
43       modified_parse.HasFieldType(DatetimeComponent::ComponentType::HOUR) &&
44       !modified_parse.HasRelativeValue(
45           DatetimeComponent::ComponentType::HOUR) &&
46       !modified_parse.HasFieldType(
47           DatetimeComponent::ComponentType::MERIDIEM)) {
48     int hour_value;
49     modified_parse.GetFieldValue(DatetimeComponent::ComponentType::HOUR,
50                                  &hour_value);
51     if (hour_value <= 12) {
52       modified_parse.SetAbsoluteValue(
53           DatetimeComponent::ComponentType::MERIDIEM, 0);
54       interpretations->push_back(modified_parse);
55       modified_parse.SetAbsoluteValue(
56           DatetimeComponent::ComponentType::MERIDIEM, 1);
57       interpretations->push_back(modified_parse);
58     } else {
59       interpretations->push_back(modified_parse);
60     }
61   } else {
62     // Otherwise just generate 1 variant.
63     interpretations->push_back(modified_parse);
64   }
65 }
66 
GetAdjustedYear(const int parsed_year)67 int GetAdjustedYear(const int parsed_year) {
68   if (parsed_year < 100) {
69     if (parsed_year < 50) {
70       return parsed_year + 2000;
71     } else {
72       return parsed_year + 1900;
73     }
74   }
75   return parsed_year;
76 }
77 
78 }  // namespace libtextclassifier3
79