• 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_UTILS_GRAMMAR_SEMANTICS_COMPOSER_H_
18 #define LIBTEXTCLASSIFIER_UTILS_GRAMMAR_SEMANTICS_COMPOSER_H_
19 
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "utils/base/arena.h"
24 #include "utils/base/status.h"
25 #include "utils/base/statusor.h"
26 #include "utils/flatbuffers/flatbuffers.h"
27 #include "utils/grammar/parsing/derivation.h"
28 #include "utils/grammar/parsing/parse-tree.h"
29 #include "utils/grammar/semantics/eval-context.h"
30 #include "utils/grammar/semantics/evaluator.h"
31 #include "utils/grammar/semantics/expression_generated.h"
32 #include "utils/grammar/semantics/value.h"
33 #include "utils/grammar/text-context.h"
34 
35 namespace libtextclassifier3::grammar {
36 
37 // Semantic value composer.
38 // It evaluates a semantic expression of a syntactic parse tree as a semantic
39 // value.
40 // It evaluates the constituents of a rule match and applies them to semantic
41 // expression, calling out to semantic functions that implement the basic
42 // building blocks.
43 class SemanticComposer : public SemanticExpressionEvaluator {
44  public:
45   // Expects a flatbuffer schema that describes the possible result values of
46   // an evaluation.
47   explicit SemanticComposer(const reflection::Schema* semantic_values_schema);
48 
49   // Evaluates a semantic expression that is associated with the root of a parse
50   // tree.
51   StatusOr<const SemanticValue*> Eval(const TextContext& text_context,
52                                       const Derivation& derivation,
53                                       UnsafeArena* arena) const;
54 
55   // Applies a semantic expression to a list of constituents and
56   // produces an output semantic value.
57   StatusOr<const SemanticValue*> Apply(const EvalContext& context,
58                                        const SemanticExpression* expression,
59                                        UnsafeArena* arena) const override;
60 
61  private:
62   // Evaluates a semantic expression against a parse tree.
63   StatusOr<const SemanticValue*> Eval(const TextContext& text_context,
64                                       const SemanticExpressionNode* derivation,
65                                       UnsafeArena* arena) const;
66 
67   std::unordered_map<SemanticExpression_::Expression,
68                      std::unique_ptr<SemanticExpressionEvaluator>>
69       evaluators_;
70 };
71 
72 }  // namespace libtextclassifier3::grammar
73 
74 #endif  // LIBTEXTCLASSIFIER_UTILS_GRAMMAR_SEMANTICS_COMPOSER_H_
75