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 "utils/grammar/analyzer.h"
18 
19 #include "utils/grammar/testing/utils.h"
20 #include "utils/grammar/types.h"
21 #include "utils/grammar/utils/rules.h"
22 #include "utils/utf8/unicodetext.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 
26 namespace libtextclassifier3::grammar {
27 namespace {
28 
29 using ::testing::SizeIs;
30 
31 class AnalyzerTest : public GrammarTest {};
32 
TEST_F(AnalyzerTest,ParsesTextWithGrammar)33 TEST_F(AnalyzerTest, ParsesTextWithGrammar) {
34   RulesSetT model;
35 
36   // Add semantic values schema.
37   model.semantic_values_schema.assign(semantic_values_schema_.buffer().begin(),
38                                       semantic_values_schema_.buffer().end());
39 
40   // Define rules and semantics.
41   grammar::LocaleShardMap locale_shard_map =
42       grammar::LocaleShardMap::CreateLocaleShardMap({""});
43   Rules rules(locale_shard_map);
44   rules.Add("<month>", {"january"},
45             static_cast<CallbackId>(DefaultCallback::kSemanticExpression),
46             /*callback_param=*/model.semantic_expression.size());
47   model.semantic_expression.push_back(CreatePrimitiveConstExpression(1));
48 
49   rules.Add("<month>", {"february"},
50             static_cast<CallbackId>(DefaultCallback::kSemanticExpression),
51             /*callback_param=*/model.semantic_expression.size());
52   model.semantic_expression.push_back(CreatePrimitiveConstExpression(2));
53 
54   const int kMonth = 0;
55   rules.Add("<month_rule>", {"<month>"},
56             static_cast<CallbackId>(DefaultCallback::kRootRule), kMonth);
57   rules.Finalize().Serialize(/*include_debug_information=*/false, &model);
58   const std::string model_buffer = PackFlatbuffer<RulesSet>(&model);
59 
60   Analyzer analyzer(unilib_.get(),
61                     flatbuffers::GetRoot<RulesSet>(model_buffer.data()));
62 
63   {
64     auto maybe_results = analyzer.Parse(
65         UTF8ToUnicodeText("The month is January 2020", /*do_copy=*/false),
66         /*locales=*/{}, &arena_);
67     EXPECT_TRUE(maybe_results.ok());
68 
69     const std::vector<EvaluatedDerivation> results = maybe_results.ValueOrDie();
70     EXPECT_THAT(results, SizeIs(1));
71 
72     // Check parse tree.
73     EXPECT_THAT(results[0], IsDerivation(kMonth /* rule_id */, 13 /* begin */,
74                                          20 /* end */));
75 
76     // Check semantic result.
77     EXPECT_EQ(results[0].value->Value<int32>(), 1);
78   }
79 
80   {
81     auto maybe_results =
82         analyzer.Parse(UTF8ToUnicodeText("february", /*do_copy=*/false),
83                        /*locales=*/{}, &arena_);
84     EXPECT_TRUE(maybe_results.ok());
85 
86     const std::vector<EvaluatedDerivation> results = maybe_results.ValueOrDie();
87     EXPECT_THAT(results, SizeIs(1));
88 
89     // Check parse tree.
90     EXPECT_THAT(results[0],
91                 IsDerivation(kMonth /* rule_id */, 0 /* begin */, 8 /* end */));
92 
93     // Check semantic result.
94     EXPECT_EQ(results[0].value->Value<int32>(), 2);
95   }
96 }
97 
98 }  // namespace
99 }  // namespace libtextclassifier3::grammar
100